Skip to content
Myr

map.ts

This file

Path

src/map.ts

187 lines · 4.2 KiB · TypeScript

Last changed by

Some initial cleanupMichel Weststratebefore adoptionc53a75f

History of this file

Take it

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 1why, one step back
1import {isDraftable, DRAFT_STATE, latest} from "./common"

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 2–3why, one step back
2
3// TODO: kill:

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 4–6why, one step back
4import {assertUnrevoked, ES5Draft} from "./es5"
5import {ImmerScope} from "./scope"6import {Immer} from "./immer"

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 7–11why, one step back
7
8// TODO: create own states9// TODO: clean up the maps and such from ES5 / Proxy states1011export interface MapState {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 12–21why, one step back
12 parent: any // TODO: type
13 scope: ImmerScope14 modified: boolean15 finalizing: boolean16 finalized: boolean17 copy: Map<any, any> | undefined18 assigned: Map<any, boolean> | undefined19 base: Map<any, any>20 revoke(): void21 draft: ES5Draft

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 22–26why, one step back
22}
2324function prepareCopy(state: MapState) {25 if (!state.copy) {26 state.assigned = new Map()

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 27why, one step back
27 state.copy = new Map(state.base)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 28–31why, one step back
28 }
29}3031// Make sure DraftMap declarion doesn't die if Map is not avialable...

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 32–33why, one step back
32const MapBase: MapConstructor =
33 typeof Map !== "undefined" ? Map : (function FakeMap() {} as any)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 34–52why, one step back
34
35// TODO: fix types for drafts36export class DraftMap<K, V> extends MapBase implements Map<K, V> {37 [DRAFT_STATE]: MapState38 constructor(target, parent) {39 super()40 this[DRAFT_STATE] = {41 parent,42 scope: parent ? parent.scope : ImmerScope.current,43 modified: false,44 finalized: false,45 finalizing: false,46 copy: undefined,47 assigned: undefined,48 base: target,49 draft: this as any, // TODO: fix typing50 revoke() {51 // TODO: make sure this marks the Map as revoked, and assert everywhere52 }

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 53why, one step back
53 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 54–64why, one step back
54 }
5556 get size(): number {57 return latest(this[DRAFT_STATE]).size58 }5960 has(key: K): boolean {61 return latest(this[DRAFT_STATE]).has(key)62 }6364 set(key: K, value: V): this {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 65why, one step back
65 const state = this[DRAFT_STATE]

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 66–70why, one step back
66 if (latest(state).get(key) !== value) {
67 prepareCopy(state)68 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly69 state.assigned!.set(key, true)70 state.copy!.set(key, value)

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 71why, one step back
71 state.assigned!.set(key, true)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 72–77why, one step back
72 }
73 return this74 }7576 delete(key: K): boolean {77 if (!this.has(key)) {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 78why, one step back
78 return false

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 79–80why, one step back
79 }
80

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 81why, one step back
81 const state = this[DRAFT_STATE]

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 82–89why, one step back
82 prepareCopy(state)
83 state.scope.immer.markChanged(state)84 state.assigned!.set(key, false)85 state.copy!.delete(key)86 return true87 }8889 clear() {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 90why, one step back
90 const state = this[DRAFT_STATE]

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 91–101why, one step back
91 prepareCopy(state)
92 state.scope.immer.markChanged(state)93 state.assigned = new Map()94 for (const key of latest(state).keys()) {95 state.assigned.set(key, false)96 }97 return state.copy!.clear()98 }99100 // @ts-ignore TODO:101 forEach(cb) {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 102why, one step back
102 const state = this[DRAFT_STATE]

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 103–107why, one step back
103 latest(state).forEach((_value, key, _map) => {
104 cb(this.get(key), key, this)105 })106 }107

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 108–109why, one step back
108 get(key: K): V /* TODO: Draft<V> */ {
109 const state = this[DRAFT_STATE]

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 110–116why, one step back
110 const value = latest(state).get(key)
111 if (state.finalizing || state.finalized || !isDraftable(value)) {112 return value113 }114 if (value !== state.base.get(key)) {115 return value // either already drafted or reassigned116 }

fixed most testsMichel Weststratebefore adoption385c0a4

line 117why, one step back
117 // despite what it looks, this creates a draft only once, see above condition

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 118–124why, one step back
118 const draft = state.scope.immer.createProxy(value, state)
119 prepareCopy(state)120 state.copy!.set(key, draft)121 return draft122 }123124 keys() {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 125why, one step back
125 return latest(this[DRAFT_STATE]).keys()

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 126–127why, one step back
126 }
127

fixed most testsMichel Weststratebefore adoption385c0a4

lines 128–133why, one step back
128 values() {
129 const iterator = this.keys()130 return {131 [Symbol.iterator]: () => this.values(), // TODO: don't use symbol directly132 next: () => {133 const r = iterator.next()

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 134–135why, one step back
134 if (r.done) return r
135 const value = this.get(r.value)

fixed most testsMichel Weststratebefore adoption385c0a4

line 136why, one step back
136 return {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 137–138why, one step back
137 done: false,
138 value

fixed most testsMichel Weststratebefore adoption385c0a4

lines 139–149why, one step back
139 }
140 }141 } as any142 }143144 entries() {145 const iterator = this.keys()146 return {147 [Symbol.iterator]: () => this.entries(), // TODO: don't use symbol directly148 next: () => {149 const r = iterator.next()

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 150–151why, one step back
150 if (r.done) return r
151 const value = this.get(r.value)

fixed most testsMichel Weststratebefore adoption385c0a4

line 152why, one step back
152 return {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 153–154why, one step back
153 done: false,
154 value: [r.value, value]

fixed most testsMichel Weststratebefore adoption385c0a4

lines 155–159why, one step back
155 }
156 }157 } as any158 }159

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 160–161why, one step back
160 [Symbol.iterator]() {
161 // TODO: don't use symbol directly

fixed most testsMichel Weststratebefore adoption385c0a4

lines 162–163why, one step back
162 return this.entries()
163 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 164–167why, one step back
164}
165166export function proxyMap(target, parent) {167 if (target instanceof DraftMap) {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 168why, one step back
168 return target // TODO: or copy?

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 169–187why, one step back
169 }
170 return new DraftMap(target, parent)171}172173export function hasMapChanges(state) {174 const {base, draft} = state175176 if (base.size !== draft.size) return true177178 // IE11 supports only forEach iteration179 let hasChanges = false180 // TODO: optimize: break on first difference181 draft.forEach(function(value, key) {182 if (!hasChanges) {183 hasChanges = isDraftable(value) ? value.modified : value !== base.get(key)184 }185 })186 return hasChanges187}