Skip to content
Myr

map.ts

This file

Path

src/map.ts

165 lines · 3.8 KiB · TypeScript

Last changed by

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

line 2why, one step back
2

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 5–6why, one step back
5
6export interface MapState {

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 8why, one step back
8 parent?: ImmerState

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 9–12why, one step back
9 scope: ImmerScope
10 modified: boolean11 finalizing: boolean12 finalized: boolean

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 15why, one step back
15 base: AnyMap

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 18–20why, one step back
18}
1920// Make sure DraftMap declarion doesn't die if Map is not avialable...

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 23–24why, one step back
23
24// TODO: fix types for drafts

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 25why, one step back
25// TODO: assert unrevoked, use freeze for that

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 32why, one step back
32 parent,

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 34–43why, one step back
34 modified: false,
35 finalized: false,36 finalizing: false,37 copy: undefined,38 assigned: undefined,39 base: target,40 draft: this as any, // TODO: fix typing41 revoke() {42 // TODO: make sure this marks the Map as revoked, and assert everywhere43 }

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 44why, one step back
44 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 45–55why, one step back
45 }
4647 get size(): number {48 return latest(this[DRAFT_STATE]).size49 }5051 has(key: K): boolean {52 return latest(this[DRAFT_STATE]).has(key)53 }5455 set(key: K, value: V): this {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 57–61why, one step back
57 if (latest(state).get(key) !== value) {
58 prepareCopy(state)59 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly60 state.assigned!.set(key, true)61 state.copy!.set(key, value)

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 63–68why, one step back
63 }
64 return this65 }6667 delete(key: K): boolean {68 if (!this.has(key)) {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 69why, one step back
69 return false

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 70–71why, one step back
70 }
71

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 73–80why, one step back
73 prepareCopy(state)
74 state.scope.immer.markChanged(state)75 state.assigned!.set(key, false)76 state.copy!.delete(key)77 return true78 }7980 clear() {

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–90why, one step back
82 prepareCopy(state)
83 state.scope.immer.markChanged(state)84 state.assigned = new Map()85 for (const key of latest(state).keys()) {86 state.assigned.set(key, false)87 }88 return state.copy!.clear()89 }90

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 93–94why, one step back
93 latest(state).forEach((_value: V, key: K, _map: this) => {
94 cb.call(thisArg, this.get(key), key, this)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 95–97why, one step back
95 })
96 }97

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 100–106why, one step back
100 const value = latest(state).get(key)
101 if (state.finalizing || state.finalized || !isDraftable(value)) {102 return value103 }104 if (value !== state.base.get(key)) {105 return value // either already drafted or reassigned106 }

fixed most testsMichel Weststratebefore adoption385c0a4

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 108–114why, one step back
108 const draft = state.scope.immer.createProxy(value, state)
109 prepareCopy(state)110 state.copy!.set(key, draft)111 return draft112 }113114 keys() {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 116–117why, one step back
116 }
117

fixed most testsMichel Weststratebefore adoption385c0a4

lines 118–123why, one step back
118 values() {
119 const iterator = this.keys()120 return {121 [Symbol.iterator]: () => this.values(), // TODO: don't use symbol directly122 next: () => {123 const r = iterator.next()

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

fixed most testsMichel Weststratebefore adoption385c0a4

line 126why, one step back
126 return {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 127–128why, one step back
127 done: false,
128 value

fixed most testsMichel Weststratebefore adoption385c0a4

lines 129–139why, one step back
129 }
130 }131 } as any132 }133134 entries() {135 const iterator = this.keys()136 return {137 [Symbol.iterator]: () => this.entries(), // TODO: don't use symbol directly138 next: () => {139 const r = iterator.next()

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

fixed most testsMichel Weststratebefore adoption385c0a4

line 142why, one step back
142 return {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 143–144why, one step back
143 done: false,
144 value: [r.value, value]

fixed most testsMichel Weststratebefore adoption385c0a4

lines 145–149why, one step back
145 }
146 }147 } as any148 }149

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

fixed most testsMichel Weststratebefore adoption385c0a4

lines 152–153why, one step back
152 return this.entries()
153 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 154–155why, one step back
154}
155

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 158–164why, one step back
158}
159160function prepareCopy(state: MapState) {161 if (!state.copy) {162 state.assigned = new Map()163 state.copy = new Map(state.base)164 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 165why, one step back
165}