Skip to content
Myr

mapset.ts

This file

Path

src/plugins/mapset.ts

365 lines · 8.5 KiB · TypeScript

Last changed by

landed by oscabriel8848a5b

History of this file

Take it

The walk stopped after 200 commits: lines older than 07f5cc9 point at it.

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

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(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 21–22why, one step back
21} from "../internal"
22

fix: remove global `var Iterator` declaration conflicting with ESNext lib (#1290)Erfanbefore adoptionb00474e

lines 23–32why, one step back
23// Feature-detect `Iterator.from` (added in ES2025) without augmenting the
24// global scope. Declaring `var Iterator` in a `declare global` block leaks a25// conflicting global variable into the bundled `.d.ts` (TS2403 when compiled26// against the ESNext lib). We cast via `globalThis` to satisfy the type checker27// while keeping the runtime feature detect accurate. (#1273)28const _globalIterator = (globalThis as any).Iterator as29 | undefined30 | { from: <T>(iterable: Iterator<T>) => IterableIterator<T> }31const hasIteratorFrom =32 typeof _globalIterator?.from === "function"

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036

line 33why, one step back
33

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 34why, one step back
34export function enableMapSet() {

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 37why, one step back
37

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

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

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 42–50why, one step back
42 parent_: parent,
43 scope_: parent ? parent.scope_ : getCurrentScope()!,44 modified_: false,45 finalized_: false,46 copy_: undefined,47 assigned_: undefined,48 base_: target,49 draft_: this as any,50 isManual_: false,

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

lines 51–52why, one step back
51 revoked_: false,
52 callbacks_: []

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

line 53why, one step back
53 }

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 54–55why, one step back
54 }
55

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

line 56why, one step back
56 get size(): number {
57 const state: MapState = this[DRAFT_STATE]58 assertUnrevoked(state)59 return latest(state).size

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

line 60why, one step back
60 }

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 61why, one step back
61

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

line 62why, one step back
62 has(key: any): boolean {
63 const state: MapState = this[DRAFT_STATE]64 assertUnrevoked(state)65 return latest(state).has(key)

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

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

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 69–76why, one step back
69 const state: MapState = this[DRAFT_STATE]
70 assertUnrevoked(state)71 if (!latest(state).has(key) || latest(state).get(key) !== value) {72 prepareMapCopy(state)73 markChanged(state)74 state.assigned_!.set(key, true)75 state.copy_!.set(key, value)76 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 77why, one step back
77 handleCrossReference(state, key, value)

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 78–81why, one step back
78 }
79 return this80 }81

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 83–99why, one step back
83 if (!this.has(key)) {
84 return false85 }8687 const state: MapState = this[DRAFT_STATE]88 assertUnrevoked(state)89 prepareMapCopy(state)90 markChanged(state)91 if (state.base_.has(key)) {92 state.assigned_!.set(key, false)93 } else {94 state.assigned_!.delete(key)95 }96 state.copy_!.delete(key)97 return true98 }99

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 101–113why, one step back
101 const state: MapState = this[DRAFT_STATE]
102 assertUnrevoked(state)103 if (latest(state).size) {104 prepareMapCopy(state)105 markChanged(state)106 state.assigned_ = new Map()107 each(state.base_, key => {108 state.assigned_!.set(key, false)109 })110 state.copy_!.clear()111 }112 }113

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 115why, one step back
115 const state: MapState = this[DRAFT_STATE]
116 assertUnrevoked(state)

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 117–121why, one step back
117 latest(state).forEach((_value: any, key: any, _map: any) => {
118 cb.call(thisArg, this.get(key), key, this)119 })120 }121

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 123–132why, one step back
123 const state: MapState = this[DRAFT_STATE]
124 assertUnrevoked(state)125 const value = latest(state).get(key)126 if (state.finalized_ || !isDraftable(value)) {127 return value128 }129 if (value !== state.base_.get(key)) {130 return value // either already drafted or reassigned131 }132 // 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 133why, one step back
133 const draft = createProxy(state.scope_, value, state, key)

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 134–138why, one step back
134 prepareMapCopy(state)
135 state.copy_!.set(key, draft)136 return draft137 }138

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

line 139why, one step back
139 keys(): IterableIterator<any> {
140 const state: MapState = this[DRAFT_STATE]141 assertUnrevoked(state)142 return latest(state).keys()

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 143–144why, one step back
143 }
144

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 146why, one step back
146 const iterator = this.keys()

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036

line 147why, one step back
147 return iteratorFrom({

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 148–157why, 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 value156 }157 }

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036

line 158why, one step back
158 })

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

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

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 162why, one step back
162 const iterator = this.keys()

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036

line 163why, one step back
163 return iteratorFrom({

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 164–173why, one step back
164 next: () => {
165 const r = iterator.next()166 /* istanbul ignore next */167 if (r.done) return r168 const value = this.get(r.value)169 return {170 done: false,171 value: [r.value, value]172 }173 }

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036

line 174why, one step back
174 })

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 175–176why, one step back
175 }
176

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 178–179why, one step back
178 return this.entries()
179 }

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036

lines 180–184why, one step back
180 }
181182 function iteratorFrom<T, TReturn>(183 iterable: Iterator<T, TReturn>184 ): IterableIterator<T> {

fix: remove global `var Iterator` declaration conflicting with ESNext lib (#1290)Erfanbefore adoptionb00474e

lines 185–186why, one step back
185 if (hasIteratorFrom) {
186 return _globalIterator!.from(iterable)

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036

lines 187–194why, one step back
187 }
188189 const iterator: IterableIterator<T> = {190 ...iterable,191 [Symbol.iterator]: () => iterator192 }193194 return iterator

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

line 195why, one step back
195 }

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 196why, one step back
196

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

lines 197–200why, one step back
197 function proxyMap_<T extends AnyMap>(
198 target: T,199 parent?: ImmerState200 ): [T, MapState] {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

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

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 204–212why, one step back
204 }
205206 function prepareMapCopy(state: MapState) {207 if (!state.copy_) {208 state.assigned_ = new Map()209 state.copy_ = new Map(state.base_)210 }211 }212

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

lines 213–216why, one step back
213 class DraftSet extends Set {
214 [DRAFT_STATE]: SetState215 constructor(target: AnySet, parent?: ImmerState) {216 super()

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

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

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 219–227why, one step back
219 parent_: parent,
220 scope_: parent ? parent.scope_ : getCurrentScope()!,221 modified_: false,222 finalized_: false,223 copy_: undefined,224 base_: target,225 draft_: this,226 drafts_: new Map(),227 revoked_: false,

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

lines 228–230why, one step back
228 isManual_: false,
229 assigned_: undefined,230 callbacks_: []

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

line 231why, one step back
231 }

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 232–233why, one step back
232 }
233

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

line 234why, one step back
234 get size(): number {
235 const state: SetState = this[DRAFT_STATE]236 assertUnrevoked(state)237 return latest(state).size

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

line 238why, one step back
238 }

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 239why, one step back
239

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 241–252why, one step back
241 const state: SetState = this[DRAFT_STATE]
242 assertUnrevoked(state)243 // bit of trickery here, to be able to recognize both the value, and the draft of its value244 if (!state.copy_) {245 return state.base_.has(value)246 }247 if (state.copy_.has(value)) return true248 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))249 return true250 return false251 }252

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 254–259why, one step back
254 const state: SetState = this[DRAFT_STATE]
255 assertUnrevoked(state)256 if (!this.has(value)) {257 prepareSetCopy(state)258 markChanged(state)259 state.copy_!.add(value)

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 261–264why, one step back
261 }
262 return this263 }264

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 266–281why, one step back
266 if (!this.has(value)) {
267 return false268 }269270 const state: SetState = this[DRAFT_STATE]271 assertUnrevoked(state)272 prepareSetCopy(state)273 markChanged(state)274 return (275 state.copy_!.delete(value) ||276 (state.drafts_.has(value)277 ? state.copy_!.delete(state.drafts_.get(value))278 : /* istanbul ignore next */ false)279 )280 }281

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 283–291why, one step back
283 const state: SetState = this[DRAFT_STATE]
284 assertUnrevoked(state)285 if (latest(state).size) {286 prepareSetCopy(state)287 markChanged(state)288 state.copy_!.clear()289 }290 }291

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 293–298why, one step back
293 const state: SetState = this[DRAFT_STATE]
294 assertUnrevoked(state)295 prepareSetCopy(state)296 return state.copy_!.values()297 }298

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 300–305why, one step back
300 const state: SetState = this[DRAFT_STATE]
301 assertUnrevoked(state)302 prepareSetCopy(state)303 return state.copy_!.entries()304 }305

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 307–309why, one step back
307 return this.values()
308 }309

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 311–313why, one step back
311 return this.values()
312 }313

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 315–321why, one step back
315 const iterator = this.values()
316 let result = iterator.next()317 while (!result.done) {318 cb.call(thisArg, result.value, result.value, this)319 result = iterator.next()320 }321 }

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

line 322why, one step back
322 }

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

lines 323–326why, one step back
323 function proxySet_<T extends AnySet>(
324 target: T,325 parent?: ImmerState326 ): [T, SetState] {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

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

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 330–337why, one step back
330 }
331332 function prepareSetCopy(state: SetState) {333 if (!state.copy_) {334 // create drafts for all entries to preserve insertion order335 state.copy_ = new Set()336 state.base_.forEach(value => {337 if (isDraftable(value)) {

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 339–351why, one step back
339 state.drafts_.set(value, draft)
340 state.copy_!.add(draft)341 } else {342 state.copy_!.add(value)343 }344 })345 }346 }347348 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {349 if (state.revoked_) die(3, JSON.stringify(latest(state)))350 }351

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

lines 352–364why, one step back
352 function fixSetContents(target: ImmerState) {
353 // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #628354 // To preserve insertion order in all cases we then clear the set355 if (target.type_ === ArchType.Set && target.copy_) {356 const copy = new Set(target.copy_)357 target.copy_.clear()358 copy.forEach(value => {359 target.copy_!.add(getValue(value))360 })361 }362 }363364 loadPlugin(PluginMapSet, {proxyMap_, proxySet_, fixSetContents})

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 365why, one step back
365}