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 fileTake it
The walk stopped after 200 commits: lines older than ba24aab point at it.
chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
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: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
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: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
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: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
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: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
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: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
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: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 54–55why, one step back54 }55Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 56–58why, one step back56 get size(): number {57 return latest(this[DRAFT_STATE]).size58 }chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
line 59why, one step back59Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 60why, one step back60 has(key: any): boolean {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 61–63why, one step back61 return latest(this[DRAFT_STATE]).has(key)62 }63Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 64why, one step back64 set(key: any, value: any) {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 65–72why, one step back65 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 back73 handleCrossReference(state, key, value)chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 74–77why, one step back74 }75 return this76 }77Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 78why, one step back78 delete(key: any): boolean {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 79–95why, one step back79 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 }95Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 96why, one step back96 clear() {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 97–109why, one step back97 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 }109Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 110why, one step back110 forEach(cb: (value: any, key: any, self: any) => void, thisArg?: any) {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 111–116why, one step back111 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 }116Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 117why, one step back117 get(key: any): any {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 118–127why, one step back118 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 conditionperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 128why, one step back128 const draft = createProxy(state.scope_, value, state, key)chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 129–133why, one step back129 prepareMapCopy(state)130 state.copy_!.set(key, draft)131 return draft132 }133Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 134why, one step back134 keys(): IterableIterator<any> {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 135–137why, one step back135 return latest(this[DRAFT_STATE]).keys()136 }137Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 138why, one step back138 values(): IterableIterator<any> {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
line 139why, one step back139 const iterator = this.keys()fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
line 140why, one step back140 return iteratorFrom({chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 141–150why, one step back141 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 back151 })chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 152–153why, one step back152 }153Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 154why, one step back154 entries(): IterableIterator<[any, any]> {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
line 155why, one step back155 const iterator = this.keys()fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
line 156why, one step back156 return iteratorFrom({chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 157–166why, one step back157 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 back167 })chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 168–169why, one step back168 }169Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 170why, one step back170 [Symbol.iterator]() {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 171–172why, one step back171 return this.entries()172 }fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
lines 173–177why, one step back173 }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 back178 if (hasIteratorFrom) {179 return _globalIterator!.from(iterable)fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
lines 180–187why, one step back180 }181182 const iterator: IterableIterator<T> = {183 ...iterable,184 [Symbol.iterator]: () => iterator185 }186187 return iteratorMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 188why, one step back188 }chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
line 189why, one step back189perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 190–193why, one step back190 function proxyMap_<T extends AnyMap>(191 target: T,192 parent?: ImmerState193 ): [T, MapState] {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
line 194why, one step back194 // @ts-ignoreperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 195–196why, one step back195 const map = new DraftMap(target, parent)196 return [map as any, map[DRAFT_STATE]]chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 197–205why, one step back197 }198199 function prepareMapCopy(state: MapState) {200 if (!state.copy_) {201 state.assigned_ = new Map()202 state.copy_ = new Map(state.base_)203 }204 }205Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 206–209why, one step back206 class DraftSet extends Set {207 [DRAFT_STATE]: SetState208 constructor(target: AnySet, parent?: ImmerState) {209 super()chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
line 210why, one step back210 this[DRAFT_STATE] = {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 211why, one step back211 type_: ArchType.Set,chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 212–220why, one step back212 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 back221 isManual_: false,222 assigned_: undefined,223 callbacks_: []Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 224why, one step back224 }chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 225–226why, one step back225 }226Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 227–229why, one step back227 get size(): number {228 return latest(this[DRAFT_STATE]).size229 }chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
line 230why, one step back230Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 231why, one step back231 has(value: any): boolean {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 232–243why, one step back232 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 }243Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 244why, one step back244 add(value: any): any {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 245–250why, one step back245 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 back251 handleCrossReference(state, value, value)chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 252–255why, one step back252 }253 return this254 }255Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 256why, one step back256 delete(value: any): any {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 257–272why, one step back257 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 }272Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 273why, one step back273 clear() {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 274–282why, one step back274 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 }282Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 283why, one step back283 values(): IterableIterator<any> {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 284–289why, one step back284 const state: SetState = this[DRAFT_STATE]285 assertUnrevoked(state)286 prepareSetCopy(state)287 return state.copy_!.values()288 }289Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 290why, one step back290 entries(): IterableIterator<[any, any]> {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 291–296why, one step back291 const state: SetState = this[DRAFT_STATE]292 assertUnrevoked(state)293 prepareSetCopy(state)294 return state.copy_!.entries()295 }296Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 297why, one step back297 keys(): IterableIterator<any> {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 298–300why, one step back298 return this.values()299 }300Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 301why, one step back301 [Symbol.iterator]() {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 302–304why, one step back302 return this.values()303 }304Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 305why, one step back305 forEach(cb: any, thisArg?: any) {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 306–312why, one step back306 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 back313 }perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 314–317why, one step back314 function proxySet_<T extends AnySet>(315 target: T,316 parent?: ImmerState317 ): [T, SetState] {chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
line 318why, one step back318 // @ts-ignoreperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 319–320why, one step back319 const set = new DraftSet(target, parent)320 return [set as any, set[DRAFT_STATE]]chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 321–328why, one step back321 }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 back329 const draft = createProxy(state.scope_, value, state, value)chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
lines 330–342why, one step back330 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 }342perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 343–355why, one step back343 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})chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab
line 356why, one step back356}