Skip to content
Myr

mapset.ts

This file

Path

src/plugins/mapset.ts

332 lines · 7.5 KiB · TypeScript

Last changed by

fix: Fix broken array patching and ensure all values in draft Maps/Sets are finalized (#1201)Mark Eriksonbefore adoptionb208d58

History of this file

Take it

The walk stopped after 200 commits: lines older than 3f9a94e point at it.

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 1–15why, one step back
1// types only!
2import {3 ImmerState,4 AnyMap,5 AnySet,6 MapState,7 SetState,8 DRAFT_STATE,9 getCurrentScope,10 latest,11 isDraftable,12 createProxy,13 loadPlugin,14 markChanged,15 die,

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 16why, one step back
16 ArchType,

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 17–18why, one step back
17 each,
18 getValue,

fix: Fix broken array patching and ensure all values in draft Maps/Sets are finalized (#1201)Mark Eriksonbefore adoptionb208d58

lines 19–20why, one step back
19 PluginMapSet,
20 handleCrossReference

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 21–23why, one step back
21} from "../internal"
2223export function enableMapSet() {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 24–25why, one step back
24 class DraftMap extends Map {
25 [DRAFT_STATE]: MapState

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

line 26why, one step back
26

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 27–28why, one step back
27 constructor(target: AnyMap, parent?: ImmerState) {
28 super()

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

line 29why, one step back
29 this[DRAFT_STATE] = {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 30why, one step back
30 type_: ArchType.Map,

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 31–39why, one step back
31 parent_: parent,
32 scope_: parent ? parent.scope_ : getCurrentScope()!,33 modified_: false,34 finalized_: false,35 copy_: undefined,36 assigned_: undefined,37 base_: target,38 draft_: this as any,39 isManual_: false,

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 40–41why, one step back
40 revoked_: false,
41 callbacks_: []

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 42why, one step back
42 }

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 43–44why, one step back
43 }
44

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 45–47why, one step back
45 get size(): number {
46 return latest(this[DRAFT_STATE]).size47 }

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

line 48why, one step back
48

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 49why, one step back
49 has(key: any): boolean {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 50–52why, one step back
50 return latest(this[DRAFT_STATE]).has(key)
51 }52

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 53why, one step back
53 set(key: any, value: any) {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 54–61why, one step back
54 const state: MapState = this[DRAFT_STATE]
55 assertUnrevoked(state)56 if (!latest(state).has(key) || latest(state).get(key) !== value) {57 prepareMapCopy(state)58 markChanged(state)59 state.assigned_!.set(key, true)60 state.copy_!.set(key, value)61 state.assigned_!.set(key, true)

fix: Fix broken array patching and ensure all values in draft Maps/Sets are finalized (#1201)Mark Eriksonbefore adoptionb208d58

line 62why, one step back
62 handleCrossReference(state, key, value)

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 63–66why, one step back
63 }
64 return this65 }66

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 67why, one step back
67 delete(key: any): boolean {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 68–75why, one step back
68 if (!this.has(key)) {
69 return false70 }7172 const state: MapState = this[DRAFT_STATE]73 assertUnrevoked(state)74 prepareMapCopy(state)75 markChanged(state)

fix: immerjs#876 Ensure empty patch set for atomic set+delete on Map (#878)Julian Scheidbefore adoptione140918

lines 76–80why, one step back
76 if (state.base_.has(key)) {
77 state.assigned_!.set(key, false)78 } else {79 state.assigned_!.delete(key)80 }

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 81–84why, one step back
81 state.copy_!.delete(key)
82 return true83 }84

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 85why, one step back
85 clear() {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 86–98why, one step back
86 const state: MapState = this[DRAFT_STATE]
87 assertUnrevoked(state)88 if (latest(state).size) {89 prepareMapCopy(state)90 markChanged(state)91 state.assigned_ = new Map()92 each(state.base_, key => {93 state.assigned_!.set(key, false)94 })95 state.copy_!.clear()96 }97 }98

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

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

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 100–105why, one step back
100 const state: MapState = this[DRAFT_STATE]
101 latest(state).forEach((_value: any, key: any, _map: any) => {102 cb.call(thisArg, this.get(key), key, this)103 })104 }105

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 106why, one step back
106 get(key: any): any {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 107–116why, one step back
107 const state: MapState = this[DRAFT_STATE]
108 assertUnrevoked(state)109 const value = latest(state).get(key)110 if (state.finalized_ || !isDraftable(value)) {111 return value112 }113 if (value !== state.base_.get(key)) {114 return value // either already drafted or reassigned115 }116 // despite what it looks, this creates a draft only once, see above condition

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 117why, one step back
117 const draft = createProxy(state.scope_, value, state, key)

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 118–122why, one step back
118 prepareMapCopy(state)
119 state.copy_!.set(key, draft)120 return draft121 }122

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 123why, one step back
123 keys(): IterableIterator<any> {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 124–126why, one step back
124 return latest(this[DRAFT_STATE]).keys()
125 }126

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 127why, one step back
127 values(): IterableIterator<any> {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

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

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 130why, one step back
130 [Symbol.iterator]: () => this.values(),

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 131–143why, one step back
131 next: () => {
132 const r = iterator.next()133 /* istanbul ignore next */134 if (r.done) return r135 const value = this.get(r.value)136 return {137 done: false,138 value139 }140 }141 } as any142 }143

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 144why, one step back
144 entries(): IterableIterator<[any, any]> {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

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

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 147why, one step back
147 [Symbol.iterator]: () => this.entries(),

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 148–160why, one step back
148 next: () => {
149 const r = iterator.next()150 /* istanbul ignore next */151 if (r.done) return r152 const value = this.get(r.value)153 return {154 done: false,155 value: [r.value, value]156 }157 }158 } as any159 }160

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 161why, one step back
161 [Symbol.iterator]() {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

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

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 164why, one step back
164 }

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

line 165why, one step back
165

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 166–169why, one step back
166 function proxyMap_<T extends AnyMap>(
167 target: T,168 parent?: ImmerState169 ): [T, MapState] {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

line 170why, one step back
170 // @ts-ignore

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 171–172why, one step back
171 const map = new DraftMap(target, parent)
172 return [map as any, map[DRAFT_STATE]]

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 173–181why, one step back
173 }
174175 function prepareMapCopy(state: MapState) {176 if (!state.copy_) {177 state.assigned_ = new Map()178 state.copy_ = new Map(state.base_)179 }180 }181

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 182–185why, one step back
182 class DraftSet extends Set {
183 [DRAFT_STATE]: SetState184 constructor(target: AnySet, parent?: ImmerState) {185 super()

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

line 186why, one step back
186 this[DRAFT_STATE] = {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 187why, one step back
187 type_: ArchType.Set,

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 188–196why, one step back
188 parent_: parent,
189 scope_: parent ? parent.scope_ : getCurrentScope()!,190 modified_: false,191 finalized_: false,192 copy_: undefined,193 base_: target,194 draft_: this,195 drafts_: new Map(),196 revoked_: false,

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 197–199why, one step back
197 isManual_: false,
198 assigned_: undefined,199 callbacks_: []

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 200why, one step back
200 }

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 201–202why, one step back
201 }
202

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 203–205why, one step back
203 get size(): number {
204 return latest(this[DRAFT_STATE]).size205 }

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

line 206why, one step back
206

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 207why, one step back
207 has(value: any): boolean {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 208–219why, one step back
208 const state: SetState = this[DRAFT_STATE]
209 assertUnrevoked(state)210 // bit of trickery here, to be able to recognize both the value, and the draft of its value211 if (!state.copy_) {212 return state.base_.has(value)213 }214 if (state.copy_.has(value)) return true215 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))216 return true217 return false218 }219

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 220why, one step back
220 add(value: any): any {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 221–226why, one step back
221 const state: SetState = this[DRAFT_STATE]
222 assertUnrevoked(state)223 if (!this.has(value)) {224 prepareSetCopy(state)225 markChanged(state)226 state.copy_!.add(value)

fix: Fix broken array patching and ensure all values in draft Maps/Sets are finalized (#1201)Mark Eriksonbefore adoptionb208d58

line 227why, one step back
227 handleCrossReference(state, value, value)

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 228–231why, one step back
228 }
229 return this230 }231

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 232why, one step back
232 delete(value: any): any {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 233–248why, one step back
233 if (!this.has(value)) {
234 return false235 }236237 const state: SetState = this[DRAFT_STATE]238 assertUnrevoked(state)239 prepareSetCopy(state)240 markChanged(state)241 return (242 state.copy_!.delete(value) ||243 (state.drafts_.has(value)244 ? state.copy_!.delete(state.drafts_.get(value))245 : /* istanbul ignore next */ false)246 )247 }248

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 249why, one step back
249 clear() {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 250–258why, one step back
250 const state: SetState = this[DRAFT_STATE]
251 assertUnrevoked(state)252 if (latest(state).size) {253 prepareSetCopy(state)254 markChanged(state)255 state.copy_!.clear()256 }257 }258

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 259why, one step back
259 values(): IterableIterator<any> {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 260–265why, one step back
260 const state: SetState = this[DRAFT_STATE]
261 assertUnrevoked(state)262 prepareSetCopy(state)263 return state.copy_!.values()264 }265

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 266why, one step back
266 entries(): IterableIterator<[any, any]> {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 267–272why, one step back
267 const state: SetState = this[DRAFT_STATE]
268 assertUnrevoked(state)269 prepareSetCopy(state)270 return state.copy_!.entries()271 }272

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 273why, one step back
273 keys(): IterableIterator<any> {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 274–276why, one step back
274 return this.values()
275 }276

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 277why, one step back
277 [Symbol.iterator]() {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 278–280why, one step back
278 return this.values()
279 }280

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 281why, one step back
281 forEach(cb: any, thisArg?: any) {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 282–288why, one step back
282 const iterator = this.values()
283 let result = iterator.next()284 while (!result.done) {285 cb.call(thisArg, result.value, result.value, this)286 result = iterator.next()287 }288 }

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 289why, one step back
289 }

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 290–293why, one step back
290 function proxySet_<T extends AnySet>(
291 target: T,292 parent?: ImmerState293 ): [T, SetState] {

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

line 294why, one step back
294 // @ts-ignore

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 295–296why, one step back
295 const set = new DraftSet(target, parent)
296 return [set as any, set[DRAFT_STATE]]

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 297–304why, one step back
297 }
298299 function prepareSetCopy(state: SetState) {300 if (!state.copy_) {301 // create drafts for all entries to preserve insertion order302 state.copy_ = new Set()303 state.base_.forEach(value => {304 if (isDraftable(value)) {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 305why, one step back
305 const draft = createProxy(state.scope_, value, state, value)

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 306–318why, one step back
306 state.drafts_.set(value, draft)
307 state.copy_!.add(draft)308 } else {309 state.copy_!.add(value)310 }311 })312 }313 }314315 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {316 if (state.revoked_) die(3, JSON.stringify(latest(state)))317 }318

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 319–331why, one step back
319 function fixSetContents(target: ImmerState) {
320 // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #628321 // To preserve insertion order in all cases we then clear the set322 if (target.type_ === ArchType.Set && target.copy_) {323 const copy = new Set(target.copy_)324 target.copy_.clear()325 copy.forEach(value => {326 target.copy_!.add(getValue(value))327 })328 }329 }330331 loadPlugin(PluginMapSet, {proxyMap_, proxySet_, fixSetContents})

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

line 332why, one step back
332}