Skip to content
Myr

map.ts

This file

Path

src/map.ts

159 lines · 3.7 KiB · TypeScript

Last changed by

Enums FTWMichel Weststratebefore adoptionfd76f1c

History of this file

Take it

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 3why, one step back
3import {ImmerScope} from "./scope"

Enums FTWMichel Weststratebefore adoptionfd76f1c

line 4why, one step back
4import {AnyMap, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 5why, one step back
5

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 6why, one step back
6export interface MapState extends ImmerBaseState {

Enums FTWMichel Weststratebefore adoptionfd76f1c

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 8why, one step back
8 finalizing: boolean

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 11why, one step back
11 base: AnyMap

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 13why, one step back
13 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...

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

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] = {

Enums FTWMichel Weststratebefore adoptionfd76f1c

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 26why, one step back
26 parent,

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 34why, one step back
34 draft: this,

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 35–36why, one step back
35 revoke() {
36 // TODO: make sure this marks the Map as revoked, and assert everywhere

Processed bunch of todosMichel Weststratebefore adoption00425e1

lines 37–38why, one step back
37 },
38 isManual: false

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 39why, one step back
39 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 40–50why, one step back
40 }
4142 get size(): number {43 return latest(this[DRAFT_STATE]).size44 }4546 has(key: K): boolean {47 return latest(this[DRAFT_STATE]).has(key)48 }4950 set(key: K, value: V): this {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 52–56why, one step back
52 if (latest(state).get(key) !== value) {
53 prepareCopy(state)54 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly55 state.assigned!.set(key, true)56 state.copy!.set(key, value)

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 58–63why, one step back
58 }
59 return this60 }6162 delete(key: K): boolean {63 if (!this.has(key)) {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 64why, one step back
64 return false

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 65–66why, one step back
65 }
66

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 68–75why, one step back
68 prepareCopy(state)
69 state.scope.immer.markChanged(state)70 state.assigned!.set(key, false)71 state.copy!.delete(key)72 return true73 }7475 clear() {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 76why, one step back
76 const state = this[DRAFT_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

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 86why, one step back
86 forEach(cb: (value: V, key: K, self: this) => void, thisArg?: any) {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 88–89why, one step back
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

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 93why, one step back
93 get(key: K): V {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 95–101why, one step back
95 const value = latest(state).get(key)
96 if (state.finalizing || state.finalized || !isDraftable(value)) {97 return value98 }99 if (value !== state.base.get(key)) {100 return value // either already drafted or reassigned101 }

fixed most testsMichel Weststratebefore adoption385c0a4

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 109why, one step back
109 keys(): IterableIterator<K> {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

fixed most testsMichel Weststratebefore adoption385c0a4

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

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

fixed most testsMichel Weststratebefore adoption385c0a4

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

fixed most testsMichel Weststratebefore adoption385c0a4

line 121why, one step back
121 return {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

fixed most testsMichel Weststratebefore adoption385c0a4

lines 124–128why, one step back
124 }
125 }126 } as any127 }128

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

fixed most testsMichel Weststratebefore adoption385c0a4

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

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

fixed most testsMichel Weststratebefore adoption385c0a4

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

fixed most testsMichel Weststratebefore adoption385c0a4

line 137why, one step back
137 return {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

fixed most testsMichel Weststratebefore adoption385c0a4

lines 140–144why, one step back
140 }
141 }142 } as any143 }144

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

fixed most testsMichel Weststratebefore adoption385c0a4

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 151why, one step back
151 return new DraftMap(target, parent)

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 159why, one step back
159}