Skip to content
Myr

map.ts

This file

Path

src/map.ts

160 lines · 3.6 KiB · TypeScript

Last changed by

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

History of this file

Take it

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 2why, one step back
2

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 3–5why, one step back
3import {ImmerScope} from "./scope"
4import {AnyMap, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"5import {assertUnrevoked} from "./es5"

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 6why, one step back
6

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 7–13why, one step back
7export interface MapState extends ImmerBaseState {
8 type: ProxyType.Map9 copy: AnyMap | undefined10 assigned: Map<any, boolean> | undefined11 base: AnyMap12 revoked: boolean13 draft: Drafted<AnyMap, MapState>

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 14–16why, one step back
14}
1516// Make sure DraftMap declarion doesn't die if Map is not avialable...

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 19–21why, one step back
19
20export class DraftMap<K, V> extends MapBase implements Map<K, V> {21 [DRAFT_STATE]: MapState

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 22why, one step back
22 constructor(target: AnyMap, parent?: ImmerState) {

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 23–24why, one step back
23 super()
24 this[DRAFT_STATE] = {

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 25why, one step back
25 type: ProxyType.Map,

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 26why, one step back
26 parent,

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 27why, one step back
27 scope: parent ? parent.scope : ImmerScope.current!,

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 28–32why, one step back
28 modified: false,
29 finalized: false,30 copy: undefined,31 assigned: undefined,32 base: target,

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 33–36why, one step back
33 draft: this,
34 isManual: false,35 revoked: false36 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 37–47why, one step back
37 }
3839 get size(): number {40 return latest(this[DRAFT_STATE]).size41 }4243 has(key: K): boolean {44 return latest(this[DRAFT_STATE]).has(key)45 }4647 set(key: K, value: V): this {

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 48–49why, one step back
48 const state = this[DRAFT_STATE]
49 assertUnrevoked(state)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 50–51why, one step back
50 if (latest(state).get(key) !== value) {
51 prepareCopy(state)

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 52why, one step back
52 state.scope.immer.markChanged(state)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 53–54why, one step back
53 state.assigned!.set(key, true)
54 state.copy!.set(key, value)

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 56–61why, one step back
56 }
57 return this58 }5960 delete(key: K): boolean {61 if (!this.has(key)) {

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 62why, one step back
62 return false

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 63–64why, one step back
63 }
64

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 65–66why, one step back
65 const state = this[DRAFT_STATE]
66 assertUnrevoked(state)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 67–74why, one step back
67 prepareCopy(state)
68 state.scope.immer.markChanged(state)69 state.assigned!.set(key, false)70 state.copy!.delete(key)71 return true72 }7374 clear() {

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 75–76why, one step back
75 const state = this[DRAFT_STATE]
76 assertUnrevoked(state)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 77–85why, one step back
77 prepareCopy(state)
78 state.scope.immer.markChanged(state)79 state.assigned = new Map()80 for (const key of latest(state).keys()) {81 state.assigned.set(key, false)82 }83 return state.copy!.clear()84 }85

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 86–89why, one step back
86 forEach(cb: (value: V, key: K, self: this) => void, thisArg?: any) {
87 const state = this[DRAFT_STATE]88 latest(state).forEach((_value: V, key: K, _map: this) => {89 cb.call(thisArg, this.get(key), key, this)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 90–92why, one step back
90 })
91 }92

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 93–95why, one step back
93 get(key: K): V {
94 const state = this[DRAFT_STATE]95 assertUnrevoked(state)

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 96why, one step back
96 const value = latest(state).get(key)

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 97why, one step back
97 if (state.finalized || !isDraftable(value)) {

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 98–102why, one step back
98 return value
99 }100 if (value !== state.base.get(key)) {101 return value // either already drafted or reassigned102 }

fixed most testsMichel Weststratebefore adoption385c0a4

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 104–109why, one step back
104 const draft = state.scope.immer.createProxy(value, state)
105 prepareCopy(state)106 state.copy!.set(key, draft)107 return draft108 }109

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 110–111why, one step back
110 keys(): IterableIterator<K> {
111 return latest(this[DRAFT_STATE]).keys()

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 112–113why, one step back
112 }
113

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 114why, one step back
114 values(): IterableIterator<V> {

fixed most testsMichel Weststratebefore adoption385c0a4

lines 115–116why, one step back
115 const iterator = this.keys()
116 return {

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 117why, one step back
117 [iteratorSymbol]: () => this.values(),

fixed most testsMichel Weststratebefore adoption385c0a4

lines 118–119why, one step back
118 next: () => {
119 const r = iterator.next()

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

fixed most testsMichel Weststratebefore adoption385c0a4

line 122why, one step back
122 return {

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 123–124why, one step back
123 done: false,
124 value

fixed most testsMichel Weststratebefore adoption385c0a4

lines 125–129why, one step back
125 }
126 }127 } as any128 }129

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 130why, one step back
130 entries(): IterableIterator<[K, V]> {

fixed most testsMichel Weststratebefore adoption385c0a4

lines 131–132why, one step back
131 const iterator = this.keys()
132 return {

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 133why, one step back
133 [iteratorSymbol]: () => this.entries(),

fixed most testsMichel Weststratebefore adoption385c0a4

lines 134–135why, one step back
134 next: () => {
135 const r = iterator.next()

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

fixed most testsMichel Weststratebefore adoption385c0a4

line 138why, one step back
138 return {

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 139–140why, one step back
139 done: false,
140 value: [r.value, value]

fixed most testsMichel Weststratebefore adoption385c0a4

lines 141–145why, one step back
141 }
142 }143 } as any144 }145

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 146why, one step back
146 [iteratorSymbol]() {

fixed most testsMichel Weststratebefore adoption385c0a4

lines 147–148why, one step back
147 return this.entries()
148 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 149–150why, one step back
149}
150

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 151why, one step back
151export function proxyMap(target: AnyMap, parent?: ImmerState) {

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 152–154why, one step back
152 return new DraftMap(target, parent)
153}154

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 155–158why, one step back
155function prepareCopy(state: MapState) {
156 if (!state.copy) {157 state.assigned = new Map()158 state.copy = new Map(state.base)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 159–160why, one step back
159 }
160}