mapset.ts
This file
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 back1// 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 back16 ArchType,perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 17–18why, one step back17 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 back19 PluginMapSet,20 handleCrossReferencechore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 21–22why, one step back21} from "../internal"22fix: remove global `var Iterator` declaration conflicting with ESNext lib (#1290)Erfanbefore adoptionb00474e
lines 23–32why, one step back23// Feature-detect `Iterator.from` (added in ES2025) without augmenting the24// 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 back33chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 34why, one step back34export function enableMapSet() {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 35–36why, one step back35 class DraftMap extends Map {36 [DRAFT_STATE]: MapStatechore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 37why, one step back37Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 38–39why, one step back38 constructor(target: AnyMap, parent?: ImmerState) {39 super()chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 40why, one step back40 this[DRAFT_STATE] = {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 41why, one step back41 type_: ArchType.Map,chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 42–50why, one step back42 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 back51 revoked_: false,52 callbacks_: []Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 53why, one step back53 }chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 54–55why, one step back54 }55Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 56why, one step back56 get size(): number {57 const state: MapState = this[DRAFT_STATE]58 assertUnrevoked(state)59 return latest(state).sizeMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 60why, one step back60 }chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 61why, one step back61Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 62why, one step back62 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 back66 }67Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 68why, one step back68 set(key: any, value: any) {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 69–76why, one step back69 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 back77 handleCrossReference(state, key, value)chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 78–81why, one step back78 }79 return this80 }81Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 82why, one step back82 delete(key: any): boolean {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 83–99why, one step back83 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 }99Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 100why, one step back100 clear() {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 101–113why, one step back101 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 }113Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 114why, one step back114 forEach(cb: (value: any, key: any, self: any) => void, thisArg?: any) {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 115why, one step back115 const state: MapState = this[DRAFT_STATE]116 assertUnrevoked(state)chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 117–121why, one step back117 latest(state).forEach((_value: any, key: any, _map: any) => {118 cb.call(thisArg, this.get(key), key, this)119 })120 }121Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 122why, one step back122 get(key: any): any {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 123–132why, one step back123 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 conditionperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 133why, one step back133 const draft = createProxy(state.scope_, value, state, key)chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 134–138why, one step back134 prepareMapCopy(state)135 state.copy_!.set(key, draft)136 return draft137 }138Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 139why, one step back139 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 back143 }144Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 145why, one step back145 values(): IterableIterator<any> {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 146why, one step back146 const iterator = this.keys()fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
line 147why, one step back147 return iteratorFrom({chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 148–157why, one step back148 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 back158 })chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 159–160why, one step back159 }160Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 161why, one step back161 entries(): IterableIterator<[any, any]> {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 162why, one step back162 const iterator = this.keys()fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
line 163why, one step back163 return iteratorFrom({chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 164–173why, one step back164 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 back174 })chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 175–176why, one step back175 }176Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 177why, one step back177 [Symbol.iterator]() {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 178–179why, one step back178 return this.entries()179 }fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
lines 180–184why, one step back180 }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 back185 if (hasIteratorFrom) {186 return _globalIterator!.from(iterable)fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
lines 187–194why, one step back187 }188189 const iterator: IterableIterator<T> = {190 ...iterable,191 [Symbol.iterator]: () => iterator192 }193194 return iteratorMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 195why, one step back195 }chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 196why, one step back196perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 197–200why, one step back197 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 back201 // @ts-ignoreperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 202–203why, one step back202 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 back204 }205206 function prepareMapCopy(state: MapState) {207 if (!state.copy_) {208 state.assigned_ = new Map()209 state.copy_ = new Map(state.base_)210 }211 }212Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 213–216why, one step back213 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 back217 this[DRAFT_STATE] = {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 218why, one step back218 type_: ArchType.Set,chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 219–227why, one step back219 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 back228 isManual_: false,229 assigned_: undefined,230 callbacks_: []Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 231why, one step back231 }chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 232–233why, one step back232 }233Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 234why, one step back234 get size(): number {235 const state: SetState = this[DRAFT_STATE]236 assertUnrevoked(state)237 return latest(state).sizeMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 238why, one step back238 }chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 239why, one step back239Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 240why, one step back240 has(value: any): boolean {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 241–252why, one step back241 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 }252Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 253why, one step back253 add(value: any): any {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 254–259why, one step back254 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 back260 handleCrossReference(state, value, value)chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 261–264why, one step back261 }262 return this263 }264Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 265why, one step back265 delete(value: any): any {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 266–281why, one step back266 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 }281Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 282why, one step back282 clear() {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 283–291why, one step back283 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 }291Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 292why, one step back292 values(): IterableIterator<any> {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 293–298why, one step back293 const state: SetState = this[DRAFT_STATE]294 assertUnrevoked(state)295 prepareSetCopy(state)296 return state.copy_!.values()297 }298Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 299why, one step back299 entries(): IterableIterator<[any, any]> {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 300–305why, one step back300 const state: SetState = this[DRAFT_STATE]301 assertUnrevoked(state)302 prepareSetCopy(state)303 return state.copy_!.entries()304 }305Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 306why, one step back306 keys(): IterableIterator<any> {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 307–309why, one step back307 return this.values()308 }309Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 310why, one step back310 [Symbol.iterator]() {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 311–313why, one step back311 return this.values()312 }313Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 314why, one step back314 forEach(cb: any, thisArg?: any) {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 315–321why, one step back315 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 back322 }perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 323–326why, one step back323 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 back327 // @ts-ignoreperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 328–329why, one step back328 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 back330 }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 back338 const draft = createProxy(state.scope_, value, state, value)chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 339–351why, one step back339 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 }351perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 352–364why, one step back352 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 back365}