Skip to content
Myr

mapset.ts

This file

Path

src/plugins/mapset.ts

356 lines · 8.3 KiB · TypeScript

Last changed by

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

History of this file

Take it

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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()

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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,

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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 }

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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

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

lines 56–58why, one step back
56 get size(): number {
57 return latest(this[DRAFT_STATE]).size58 }

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

line 59why, one step back
59

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 61–63why, one step back
61 return latest(this[DRAFT_STATE]).has(key)
62 }63

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 74–77why, one step back
74 }
75 return this76 }77

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 79–95why, one step back
79 if (!this.has(key)) {
80 return false81 }8283 const state: MapState = this[DRAFT_STATE]84 assertUnrevoked(state)85 prepareMapCopy(state)86 markChanged(state)87 if (state.base_.has(key)) {88 state.assigned_!.set(key, false)89 } else {90 state.assigned_!.delete(key)91 }92 state.copy_!.delete(key)93 return true94 }95

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 111–116why, one step back
111 const state: MapState = this[DRAFT_STATE]
112 latest(state).forEach((_value: any, key: any, _map: any) => {113 cb.call(thisArg, this.get(key), key, this)114 })115 }116

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 118–127why, one step back
118 const state: MapState = this[DRAFT_STATE]
119 assertUnrevoked(state)120 const value = latest(state).get(key)121 if (state.finalized_ || !isDraftable(value)) {122 return value123 }124 if (value !== state.base_.get(key)) {125 return value // either already drafted or reassigned126 }127 // 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 128why, one step back
128 const draft = createProxy(state.scope_, value, state, key)

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 129–133why, one step back
129 prepareMapCopy(state)
130 state.copy_!.set(key, draft)131 return draft132 }133

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 135–137why, one step back
135 return latest(this[DRAFT_STATE]).keys()
136 }137

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 141–150why, one step back
141 next: () => {
142 const r = iterator.next()143 /* istanbul ignore next */144 if (r.done) return r145 const value = this.get(r.value)146 return {147 done: false,148 value149 }150 }

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

line 151why, one step back
151 })

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 152–153why, one step back
152 }
153

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 157–166why, one step back
157 next: () => {
158 const r = iterator.next()159 /* istanbul ignore next */160 if (r.done) return r161 const value = this.get(r.value)162 return {163 done: false,164 value: [r.value, value]165 }166 }

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

line 167why, one step back
167 })

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 168–169why, one step back
168 }
169

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 171–172why, one step back
171 return this.entries()
172 }

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

lines 173–177why, one step back
173 }
174175 function iteratorFrom<T, TReturn>(176 iterable: Iterator<T, TReturn>177 ): IterableIterator<T> {

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

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

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

lines 180–187why, one step back
180 }
181182 const iterator: IterableIterator<T> = {183 ...iterable,184 [Symbol.iterator]: () => iterator185 }186187 return iterator

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

line 188why, one step back
188 }

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

line 189why, one step back
189

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

lines 190–193why, one step back
190 function proxyMap_<T extends AnyMap>(
191 target: T,192 parent?: ImmerState193 ): [T, MapState] {

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 197–205why, one step back
197 }
198199 function prepareMapCopy(state: MapState) {200 if (!state.copy_) {201 state.assigned_ = new Map()202 state.copy_ = new Map(state.base_)203 }204 }205

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

lines 206–209why, one step back
206 class DraftSet extends Set {
207 [DRAFT_STATE]: SetState208 constructor(target: AnySet, parent?: ImmerState) {209 super()

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 212–220why, one step back
212 parent_: parent,
213 scope_: parent ? parent.scope_ : getCurrentScope()!,214 modified_: false,215 finalized_: false,216 copy_: undefined,217 base_: target,218 draft_: this,219 drafts_: new Map(),220 revoked_: false,

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

lines 221–223why, one step back
221 isManual_: false,
222 assigned_: undefined,223 callbacks_: []

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

line 224why, one step back
224 }

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 225–226why, one step back
225 }
226

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

lines 227–229why, one step back
227 get size(): number {
228 return latest(this[DRAFT_STATE]).size229 }

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

line 230why, one step back
230

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 232–243why, one step back
232 const state: SetState = this[DRAFT_STATE]
233 assertUnrevoked(state)234 // bit of trickery here, to be able to recognize both the value, and the draft of its value235 if (!state.copy_) {236 return state.base_.has(value)237 }238 if (state.copy_.has(value)) return true239 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))240 return true241 return false242 }243

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 245–250why, one step back
245 const state: SetState = this[DRAFT_STATE]
246 assertUnrevoked(state)247 if (!this.has(value)) {248 prepareSetCopy(state)249 markChanged(state)250 state.copy_!.add(value)

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 252–255why, one step back
252 }
253 return this254 }255

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 257–272why, one step back
257 if (!this.has(value)) {
258 return false259 }260261 const state: SetState = this[DRAFT_STATE]262 assertUnrevoked(state)263 prepareSetCopy(state)264 markChanged(state)265 return (266 state.copy_!.delete(value) ||267 (state.drafts_.has(value)268 ? state.copy_!.delete(state.drafts_.get(value))269 : /* istanbul ignore next */ false)270 )271 }272

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 274–282why, one step back
274 const state: SetState = this[DRAFT_STATE]
275 assertUnrevoked(state)276 if (latest(state).size) {277 prepareSetCopy(state)278 markChanged(state)279 state.copy_!.clear()280 }281 }282

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 284–289why, one step back
284 const state: SetState = this[DRAFT_STATE]
285 assertUnrevoked(state)286 prepareSetCopy(state)287 return state.copy_!.values()288 }289

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 298–300why, one step back
298 return this.values()
299 }300

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 302–304why, one step back
302 return this.values()
303 }304

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 306–312why, one step back
306 const iterator = this.values()
307 let result = iterator.next()308 while (!result.done) {309 cb.call(thisArg, result.value, result.value, this)310 result = iterator.next()311 }312 }

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

line 313why, one step back
313 }

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

lines 314–317why, one step back
314 function proxySet_<T extends AnySet>(
315 target: T,316 parent?: ImmerState317 ): [T, SetState] {

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

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

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 321–328why, one step back
321 }
322323 function prepareSetCopy(state: SetState) {324 if (!state.copy_) {325 // create drafts for all entries to preserve insertion order326 state.copy_ = new Set()327 state.base_.forEach(value => {328 if (isDraftable(value)) {

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

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

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

lines 330–342why, one step back
330 state.drafts_.set(value, draft)
331 state.copy_!.add(draft)332 } else {333 state.copy_!.add(value)334 }335 })336 }337 }338339 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {340 if (state.revoked_) die(3, JSON.stringify(latest(state)))341 }342

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

lines 343–355why, one step back
343 function fixSetContents(target: ImmerState) {
344 // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #628345 // To preserve insertion order in all cases we then clear the set346 if (target.type_ === ArchType.Set && target.copy_) {347 const copy = new Set(target.copy_)348 target.copy_.clear()349 copy.forEach(value => {350 target.copy_!.add(getValue(value))351 })352 }353 }354355 loadPlugin(PluginMapSet, {proxyMap_, proxySet_, fixSetContents})

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

line 356why, one step back
356}