mapset.ts
This file
Path
src/plugins/mapset.ts
354 lines · 7.9 KiB · TypeScript
Last changed by
fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
History of this fileTake it
The walk stopped after 200 commits: lines older than 8571fe0 point at it.
docs: Update performance.mdx (#942)anabefore adoption8571fe0
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 handleCrossReferencedocs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 21–22why, one step back21} from "../internal"22fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
lines 23–31why, one step back23declare global {24 // `Iterator.from` was added in ES2025.25 var Iterator:26 | undefined27 | {28 from<T, TReturn>(iterable: Iterator<T, TReturn>): IterableIterator<T>29 }30}31docs: Update performance.mdx (#942)anabefore adoption8571fe0
line 32why, one step back32export function enableMapSet() {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 33–34why, one step back33 class DraftMap extends Map {34 [DRAFT_STATE]: MapStatedocs: Update performance.mdx (#942)anabefore adoption8571fe0
line 35why, one step back35Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 36–37why, one step back36 constructor(target: AnyMap, parent?: ImmerState) {37 super()docs: Update performance.mdx (#942)anabefore adoption8571fe0
line 38why, one step back38 this[DRAFT_STATE] = {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 39why, one step back39 type_: ArchType.Map,docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 40–48why, one step back40 parent_: parent,41 scope_: parent ? parent.scope_ : getCurrentScope()!,42 modified_: false,43 finalized_: false,44 copy_: undefined,45 assigned_: undefined,46 base_: target,47 draft_: this as any,48 isManual_: false,perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 49–50why, one step back49 revoked_: false,50 callbacks_: []Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 51why, one step back51 }docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 52–53why, one step back52 }53Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 54–56why, one step back54 get size(): number {55 return latest(this[DRAFT_STATE]).size56 }docs: Update performance.mdx (#942)anabefore adoption8571fe0
line 57why, one step back57Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 58why, one step back58 has(key: any): boolean {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 59–61why, one step back59 return latest(this[DRAFT_STATE]).has(key)60 }61Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 62why, one step back62 set(key: any, value: any) {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 63–70why, one step back63 const state: MapState = this[DRAFT_STATE]64 assertUnrevoked(state)65 if (!latest(state).has(key) || latest(state).get(key) !== value) {66 prepareMapCopy(state)67 markChanged(state)68 state.assigned_!.set(key, true)69 state.copy_!.set(key, value)70 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 71why, one step back71 handleCrossReference(state, key, value)docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 72–75why, one step back72 }73 return this74 }75Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 76why, one step back76 delete(key: any): boolean {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 77–93why, one step back77 if (!this.has(key)) {78 return false79 }8081 const state: MapState = this[DRAFT_STATE]82 assertUnrevoked(state)83 prepareMapCopy(state)84 markChanged(state)85 if (state.base_.has(key)) {86 state.assigned_!.set(key, false)87 } else {88 state.assigned_!.delete(key)89 }90 state.copy_!.delete(key)91 return true92 }93Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 94why, one step back94 clear() {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 95–107why, one step back95 const state: MapState = this[DRAFT_STATE]96 assertUnrevoked(state)97 if (latest(state).size) {98 prepareMapCopy(state)99 markChanged(state)100 state.assigned_ = new Map()101 each(state.base_, key => {102 state.assigned_!.set(key, false)103 })104 state.copy_!.clear()105 }106 }107Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 108why, one step back108 forEach(cb: (value: any, key: any, self: any) => void, thisArg?: any) {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 109–114why, one step back109 const state: MapState = this[DRAFT_STATE]110 latest(state).forEach((_value: any, key: any, _map: any) => {111 cb.call(thisArg, this.get(key), key, this)112 })113 }114Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 115why, one step back115 get(key: any): any {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 116–125why, one step back116 const state: MapState = this[DRAFT_STATE]117 assertUnrevoked(state)118 const value = latest(state).get(key)119 if (state.finalized_ || !isDraftable(value)) {120 return value121 }122 if (value !== state.base_.get(key)) {123 return value // either already drafted or reassigned124 }125 // 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 126why, one step back126 const draft = createProxy(state.scope_, value, state, key)docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 127–131why, one step back127 prepareMapCopy(state)128 state.copy_!.set(key, draft)129 return draft130 }131Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 132why, one step back132 keys(): IterableIterator<any> {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 133–135why, one step back133 return latest(this[DRAFT_STATE]).keys()134 }135Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 136why, one step back136 values(): IterableIterator<any> {docs: Update performance.mdx (#942)anabefore adoption8571fe0
line 137why, one step back137 const iterator = this.keys()fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
line 138why, one step back138 return iteratorFrom({docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 139–148why, one step back139 next: () => {140 const r = iterator.next()141 /* istanbul ignore next */142 if (r.done) return r143 const value = this.get(r.value)144 return {145 done: false,146 value147 }148 }fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
line 149why, one step back149 })docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 150–151why, one step back150 }151Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 152why, one step back152 entries(): IterableIterator<[any, any]> {docs: Update performance.mdx (#942)anabefore adoption8571fe0
line 153why, one step back153 const iterator = this.keys()fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
line 154why, one step back154 return iteratorFrom({docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 155–164why, one step back155 next: () => {156 const r = iterator.next()157 /* istanbul ignore next */158 if (r.done) return r159 const value = this.get(r.value)160 return {161 done: false,162 value: [r.value, value]163 }164 }fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
line 165why, one step back165 })docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 166–167why, one step back166 }167Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 168why, one step back168 [Symbol.iterator]() {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 169–170why, one step back169 return this.entries()170 }fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036
lines 171–185why, one step back171 }172173 function iteratorFrom<T, TReturn>(174 iterable: Iterator<T, TReturn>175 ): IterableIterator<T> {176 if (typeof Iterator !== "undefined") {177 return Iterator.from(iterable)178 }179180 const iterator: IterableIterator<T> = {181 ...iterable,182 [Symbol.iterator]: () => iterator183 }184185 return iteratorMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 186why, one step back186 }docs: Update performance.mdx (#942)anabefore adoption8571fe0
line 187why, one step back187perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 188–191why, one step back188 function proxyMap_<T extends AnyMap>(189 target: T,190 parent?: ImmerState191 ): [T, MapState] {docs: Update performance.mdx (#942)anabefore adoption8571fe0
line 192why, one step back192 // @ts-ignoreperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 193–194why, one step back193 const map = new DraftMap(target, parent)194 return [map as any, map[DRAFT_STATE]]docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 195–203why, one step back195 }196197 function prepareMapCopy(state: MapState) {198 if (!state.copy_) {199 state.assigned_ = new Map()200 state.copy_ = new Map(state.base_)201 }202 }203Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 204–207why, one step back204 class DraftSet extends Set {205 [DRAFT_STATE]: SetState206 constructor(target: AnySet, parent?: ImmerState) {207 super()docs: Update performance.mdx (#942)anabefore adoption8571fe0
line 208why, one step back208 this[DRAFT_STATE] = {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 209why, one step back209 type_: ArchType.Set,docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 210–218why, one step back210 parent_: parent,211 scope_: parent ? parent.scope_ : getCurrentScope()!,212 modified_: false,213 finalized_: false,214 copy_: undefined,215 base_: target,216 draft_: this,217 drafts_: new Map(),218 revoked_: false,perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 219–221why, one step back219 isManual_: false,220 assigned_: undefined,221 callbacks_: []Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 222why, one step back222 }docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 223–224why, one step back223 }224Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 225–227why, one step back225 get size(): number {226 return latest(this[DRAFT_STATE]).size227 }docs: Update performance.mdx (#942)anabefore adoption8571fe0
line 228why, one step back228Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 229why, one step back229 has(value: any): boolean {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 230–241why, one step back230 const state: SetState = this[DRAFT_STATE]231 assertUnrevoked(state)232 // bit of trickery here, to be able to recognize both the value, and the draft of its value233 if (!state.copy_) {234 return state.base_.has(value)235 }236 if (state.copy_.has(value)) return true237 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))238 return true239 return false240 }241Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 242why, one step back242 add(value: any): any {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 243–248why, one step back243 const state: SetState = this[DRAFT_STATE]244 assertUnrevoked(state)245 if (!this.has(value)) {246 prepareSetCopy(state)247 markChanged(state)248 state.copy_!.add(value)fix: Fix broken array patching and ensure all values in draft Maps/Sets are finalized (#1201)Mark Eriksonbefore adoptionb208d58
line 249why, one step back249 handleCrossReference(state, value, value)docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 250–253why, one step back250 }251 return this252 }253Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 254why, one step back254 delete(value: any): any {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 255–270why, one step back255 if (!this.has(value)) {256 return false257 }258259 const state: SetState = this[DRAFT_STATE]260 assertUnrevoked(state)261 prepareSetCopy(state)262 markChanged(state)263 return (264 state.copy_!.delete(value) ||265 (state.drafts_.has(value)266 ? state.copy_!.delete(state.drafts_.get(value))267 : /* istanbul ignore next */ false)268 )269 }270Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 271why, one step back271 clear() {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 272–280why, one step back272 const state: SetState = this[DRAFT_STATE]273 assertUnrevoked(state)274 if (latest(state).size) {275 prepareSetCopy(state)276 markChanged(state)277 state.copy_!.clear()278 }279 }280Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 281why, one step back281 values(): IterableIterator<any> {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 282–287why, one step back282 const state: SetState = this[DRAFT_STATE]283 assertUnrevoked(state)284 prepareSetCopy(state)285 return state.copy_!.values()286 }287Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 288why, one step back288 entries(): IterableIterator<[any, any]> {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 289–294why, one step back289 const state: SetState = this[DRAFT_STATE]290 assertUnrevoked(state)291 prepareSetCopy(state)292 return state.copy_!.entries()293 }294Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 295why, one step back295 keys(): IterableIterator<any> {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 296–298why, one step back296 return this.values()297 }298Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 299why, one step back299 [Symbol.iterator]() {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 300–302why, one step back300 return this.values()301 }302Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 303why, one step back303 forEach(cb: any, thisArg?: any) {docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 304–310why, one step back304 const iterator = this.values()305 let result = iterator.next()306 while (!result.done) {307 cb.call(thisArg, result.value, result.value, this)308 result = iterator.next()309 }310 }Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 311why, one step back311 }perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 312–315why, one step back312 function proxySet_<T extends AnySet>(313 target: T,314 parent?: ImmerState315 ): [T, SetState] {docs: Update performance.mdx (#942)anabefore adoption8571fe0
line 316why, one step back316 // @ts-ignoreperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 317–318why, one step back317 const set = new DraftSet(target, parent)318 return [set as any, set[DRAFT_STATE]]docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 319–326why, one step back319 }320321 function prepareSetCopy(state: SetState) {322 if (!state.copy_) {323 // create drafts for all entries to preserve insertion order324 state.copy_ = new Set()325 state.base_.forEach(value => {326 if (isDraftable(value)) {perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 327why, one step back327 const draft = createProxy(state.scope_, value, state, value)docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 328–340why, one step back328 state.drafts_.set(value, draft)329 state.copy_!.add(draft)330 } else {331 state.copy_!.add(value)332 }333 })334 }335 }336337 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {338 if (state.revoked_) die(3, JSON.stringify(latest(state)))339 }340perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 341–353why, one step back341 function fixSetContents(target: ImmerState) {342 // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #628343 // To preserve insertion order in all cases we then clear the set344 if (target.type_ === ArchType.Set && target.copy_) {345 const copy = new Set(target.copy_)346 target.copy_.clear()347 copy.forEach(value => {348 target.copy_!.add(getValue(value))349 })350 }351 }352353 loadPlugin(PluginMapSet, {proxyMap_, proxySet_, fixSetContents})docs: Update performance.mdx (#942)anabefore adoption8571fe0
line 354why, one step back354}