Skip to content
Myr

map.ts

This file

Path

src/map.ts

190 lines · 4.3 KiB · TypeScript

Last changed by

Introduced proper state typesMichel Weststratebefore adoption3842f85

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:

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 4why, one step back
4import {assertUnrevoked} from "./es5"

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 7why, one step back
7import {AnyMap, Drafted} from "./types"

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 8–12why, one step back
8
9// TODO: create own states10// TODO: clean up the maps and such from ES5 / Proxy states1112export interface MapState {

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 13why, one step back
13 type: "map"

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 14–18why, one step back
14 parent: any // TODO: type
15 scope: ImmerScope16 modified: boolean17 finalizing: boolean18 finalized: boolean

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 19why, one step back
19 copy: AnyMap | undefined

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 20why, one step back
20 assigned: Map<any, boolean> | undefined

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 21why, one step back
21 base: AnyMap

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 22why, one step back
22 revoke(): void

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 23why, one step back
23 draft: Drafted<AnyMap, MapState>

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 24–28why, one step back
24}
2526function prepareCopy(state: MapState) {27 if (!state.copy) {28 state.assigned = new Map()

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 36–42why, one step back
36
37// TODO: fix types for drafts38export class DraftMap<K, V> extends MapBase implements Map<K, V> {39 [DRAFT_STATE]: MapState40 constructor(target, parent) {41 super()42 this[DRAFT_STATE] = {

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 43why, one step back
43 type: "map",

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 44–55why, one step back
44 parent,
45 scope: parent ? parent.scope : ImmerScope.current,46 modified: false,47 finalized: false,48 finalizing: false,49 copy: undefined,50 assigned: undefined,51 base: target,52 draft: this as any, // TODO: fix typing53 revoke() {54 // TODO: make sure this marks the Map as revoked, and assert everywhere55 }

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 56why, one step back
56 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 57–67why, one step back
57 }
5859 get size(): number {60 return latest(this[DRAFT_STATE]).size61 }6263 has(key: K): boolean {64 return latest(this[DRAFT_STATE]).has(key)65 }6667 set(key: K, value: V): this {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 75–80why, one step back
75 }
76 return this77 }7879 delete(key: K): boolean {80 if (!this.has(key)) {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 81why, one step back
81 return false

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 82–83why, one step back
82 }
83

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 85–92why, one step back
85 prepareCopy(state)
86 state.scope.immer.markChanged(state)87 state.assigned!.set(key, false)88 state.copy!.delete(key)89 return true90 }9192 clear() {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 113–119why, one step back
113 const value = latest(state).get(key)
114 if (state.finalizing || state.finalized || !isDraftable(value)) {115 return value116 }117 if (value !== state.base.get(key)) {118 return value // either already drafted or reassigned119 }

fixed most testsMichel Weststratebefore adoption385c0a4

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 121–127why, one step back
121 const draft = state.scope.immer.createProxy(value, state)
122 prepareCopy(state)123 state.copy!.set(key, draft)124 return draft125 }126127 keys() {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 129–130why, one step back
129 }
130

fixed most testsMichel Weststratebefore adoption385c0a4

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

fixed most testsMichel Weststratebefore adoption385c0a4

line 139why, one step back
139 return {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 140–141why, one step back
140 done: false,
141 value

fixed most testsMichel Weststratebefore adoption385c0a4

lines 142–152why, one step back
142 }
143 }144 } as any145 }146147 entries() {148 const iterator = this.keys()149 return {150 [Symbol.iterator]: () => this.entries(), // TODO: don't use symbol directly151 next: () => {152 const r = iterator.next()

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

fixed most testsMichel Weststratebefore adoption385c0a4

line 155why, one step back
155 return {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 156–157why, one step back
156 done: false,
157 value: [r.value, value]

fixed most testsMichel Weststratebefore adoption385c0a4

lines 158–162why, one step back
158 }
159 }160 } as any161 }162

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

fixed most testsMichel Weststratebefore adoption385c0a4

lines 165–166why, one step back
165 return this.entries()
166 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 172–190why, one step back
172 }
173 return new DraftMap(target, parent)174}175176export function hasMapChanges(state) {177 const {base, draft} = state178179 if (base.size !== draft.size) return true180181 // IE11 supports only forEach iteration182 let hasChanges = false183 // TODO: optimize: break on first difference184 draft.forEach(function(value, key) {185 if (!hasChanges) {186 hasChanges = isDraftable(value) ? value.modified : value !== base.get(key)187 }188 })189 return hasChanges190}