mapset.ts
This file
Path
src/plugins/mapset.ts
332 lines · 7.5 KiB · TypeScript
Last changed by
fix: Fix broken array patching and ensure all values in draft Maps/Sets are finalized (#1201)Mark Eriksonbefore adoptionb208d58
History of this fileTake it
The walk stopped after 200 commits: lines older than d91a659 point at it.
fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
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 handleCrossReferencefix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 21–23why, one step back21} from "../internal"2223export function enableMapSet() {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 24–25why, one step back24 class DraftMap extends Map {25 [DRAFT_STATE]: MapStatefix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
line 26why, one step back26Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 27–28why, one step back27 constructor(target: AnyMap, parent?: ImmerState) {28 super()fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
line 29why, one step back29 this[DRAFT_STATE] = {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 30why, one step back30 type_: ArchType.Map,fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 31–39why, one step back31 parent_: parent,32 scope_: parent ? parent.scope_ : getCurrentScope()!,33 modified_: false,34 finalized_: false,35 copy_: undefined,36 assigned_: undefined,37 base_: target,38 draft_: this as any,39 isManual_: false,perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 40–41why, one step back40 revoked_: false,41 callbacks_: []Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 42why, one step back42 }fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 43–44why, one step back43 }44Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 45–47why, one step back45 get size(): number {46 return latest(this[DRAFT_STATE]).size47 }fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
line 48why, one step back48Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 49why, one step back49 has(key: any): boolean {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 50–52why, one step back50 return latest(this[DRAFT_STATE]).has(key)51 }52Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 53why, one step back53 set(key: any, value: any) {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 54–61why, one step back54 const state: MapState = this[DRAFT_STATE]55 assertUnrevoked(state)56 if (!latest(state).has(key) || latest(state).get(key) !== value) {57 prepareMapCopy(state)58 markChanged(state)59 state.assigned_!.set(key, true)60 state.copy_!.set(key, value)61 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 62why, one step back62 handleCrossReference(state, key, value)fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 63–66why, one step back63 }64 return this65 }66Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 67why, one step back67 delete(key: any): boolean {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 68–84why, one step back68 if (!this.has(key)) {69 return false70 }7172 const state: MapState = this[DRAFT_STATE]73 assertUnrevoked(state)74 prepareMapCopy(state)75 markChanged(state)76 if (state.base_.has(key)) {77 state.assigned_!.set(key, false)78 } else {79 state.assigned_!.delete(key)80 }81 state.copy_!.delete(key)82 return true83 }84Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 85why, one step back85 clear() {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 86–98why, one step back86 const state: MapState = this[DRAFT_STATE]87 assertUnrevoked(state)88 if (latest(state).size) {89 prepareMapCopy(state)90 markChanged(state)91 state.assigned_ = new Map()92 each(state.base_, key => {93 state.assigned_!.set(key, false)94 })95 state.copy_!.clear()96 }97 }98Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 99why, one step back99 forEach(cb: (value: any, key: any, self: any) => void, thisArg?: any) {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 100–105why, one step back100 const state: MapState = this[DRAFT_STATE]101 latest(state).forEach((_value: any, key: any, _map: any) => {102 cb.call(thisArg, this.get(key), key, this)103 })104 }105Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 106why, one step back106 get(key: any): any {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 107–116why, one step back107 const state: MapState = this[DRAFT_STATE]108 assertUnrevoked(state)109 const value = latest(state).get(key)110 if (state.finalized_ || !isDraftable(value)) {111 return value112 }113 if (value !== state.base_.get(key)) {114 return value // either already drafted or reassigned115 }116 // 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 117why, one step back117 const draft = createProxy(state.scope_, value, state, key)fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 118–122why, one step back118 prepareMapCopy(state)119 state.copy_!.set(key, draft)120 return draft121 }122Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 123why, one step back123 keys(): IterableIterator<any> {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 124–126why, one step back124 return latest(this[DRAFT_STATE]).keys()125 }126Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 127why, one step back127 values(): IterableIterator<any> {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 128–129why, one step back128 const iterator = this.keys()129 return {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 130why, one step back130 [Symbol.iterator]: () => this.values(),fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 131–143why, one step back131 next: () => {132 const r = iterator.next()133 /* istanbul ignore next */134 if (r.done) return r135 const value = this.get(r.value)136 return {137 done: false,138 value139 }140 }141 } as any142 }143Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 144why, one step back144 entries(): IterableIterator<[any, any]> {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 145–146why, one step back145 const iterator = this.keys()146 return {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 147why, one step back147 [Symbol.iterator]: () => this.entries(),fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 148–160why, 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 value: [r.value, value]156 }157 }158 } as any159 }160Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 161why, one step back161 [Symbol.iterator]() {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 162–163why, one step back162 return this.entries()163 }Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 164why, one step back164 }fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
line 165why, one step back165perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 166–169why, one step back166 function proxyMap_<T extends AnyMap>(167 target: T,168 parent?: ImmerState169 ): [T, MapState] {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
line 170why, one step back170 // @ts-ignoreperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 171–172why, one step back171 const map = new DraftMap(target, parent)172 return [map as any, map[DRAFT_STATE]]fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 173–181why, one step back173 }174175 function prepareMapCopy(state: MapState) {176 if (!state.copy_) {177 state.assigned_ = new Map()178 state.copy_ = new Map(state.base_)179 }180 }181Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 182–185why, one step back182 class DraftSet extends Set {183 [DRAFT_STATE]: SetState184 constructor(target: AnySet, parent?: ImmerState) {185 super()fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
line 186why, one step back186 this[DRAFT_STATE] = {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 187why, one step back187 type_: ArchType.Set,fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 188–196why, one step back188 parent_: parent,189 scope_: parent ? parent.scope_ : getCurrentScope()!,190 modified_: false,191 finalized_: false,192 copy_: undefined,193 base_: target,194 draft_: this,195 drafts_: new Map(),196 revoked_: false,perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 197–199why, one step back197 isManual_: false,198 assigned_: undefined,199 callbacks_: []Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 200why, one step back200 }fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 201–202why, one step back201 }202Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 203–205why, one step back203 get size(): number {204 return latest(this[DRAFT_STATE]).size205 }fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
line 206why, one step back206Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 207why, one step back207 has(value: any): boolean {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 208–219why, one step back208 const state: SetState = this[DRAFT_STATE]209 assertUnrevoked(state)210 // bit of trickery here, to be able to recognize both the value, and the draft of its value211 if (!state.copy_) {212 return state.base_.has(value)213 }214 if (state.copy_.has(value)) return true215 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))216 return true217 return false218 }219Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 220why, one step back220 add(value: any): any {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 221–226why, one step back221 const state: SetState = this[DRAFT_STATE]222 assertUnrevoked(state)223 if (!this.has(value)) {224 prepareSetCopy(state)225 markChanged(state)226 state.copy_!.add(value)fix: Fix broken array patching and ensure all values in draft Maps/Sets are finalized (#1201)Mark Eriksonbefore adoptionb208d58
line 227why, one step back227 handleCrossReference(state, value, value)fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 228–231why, one step back228 }229 return this230 }231Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 232why, one step back232 delete(value: any): any {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 233–248why, one step back233 if (!this.has(value)) {234 return false235 }236237 const state: SetState = this[DRAFT_STATE]238 assertUnrevoked(state)239 prepareSetCopy(state)240 markChanged(state)241 return (242 state.copy_!.delete(value) ||243 (state.drafts_.has(value)244 ? state.copy_!.delete(state.drafts_.get(value))245 : /* istanbul ignore next */ false)246 )247 }248Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 249why, one step back249 clear() {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 250–258why, one step back250 const state: SetState = this[DRAFT_STATE]251 assertUnrevoked(state)252 if (latest(state).size) {253 prepareSetCopy(state)254 markChanged(state)255 state.copy_!.clear()256 }257 }258Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 259why, one step back259 values(): IterableIterator<any> {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 260–265why, one step back260 const state: SetState = this[DRAFT_STATE]261 assertUnrevoked(state)262 prepareSetCopy(state)263 return state.copy_!.values()264 }265Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 266why, one step back266 entries(): IterableIterator<[any, any]> {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 267–272why, one step back267 const state: SetState = this[DRAFT_STATE]268 assertUnrevoked(state)269 prepareSetCopy(state)270 return state.copy_!.entries()271 }272Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 273why, one step back273 keys(): IterableIterator<any> {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 274–276why, one step back274 return this.values()275 }276Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 277why, one step back277 [Symbol.iterator]() {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 278–280why, one step back278 return this.values()279 }280Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 281why, one step back281 forEach(cb: any, thisArg?: any) {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 282–288why, one step back282 const iterator = this.values()283 let result = iterator.next()284 while (!result.done) {285 cb.call(thisArg, result.value, result.value, this)286 result = iterator.next()287 }288 }Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 289why, one step back289 }perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 290–293why, one step back290 function proxySet_<T extends AnySet>(291 target: T,292 parent?: ImmerState293 ): [T, SetState] {fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
line 294why, one step back294 // @ts-ignoreperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 295–296why, one step back295 const set = new DraftSet(target, parent)296 return [set as any, set[DRAFT_STATE]]fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 297–304why, one step back297 }298299 function prepareSetCopy(state: SetState) {300 if (!state.copy_) {301 // create drafts for all entries to preserve insertion order302 state.copy_ = new Set()303 state.base_.forEach(value => {304 if (isDraftable(value)) {perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 305why, one step back305 const draft = createProxy(state.scope_, value, state, value)fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 306–318why, one step back306 state.drafts_.set(value, draft)307 state.copy_!.add(draft)308 } else {309 state.copy_!.add(value)310 }311 })312 }313 }314315 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {316 if (state.revoked_) die(3, JSON.stringify(latest(state)))317 }318perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 319–331why, one step back319 function fixSetContents(target: ImmerState) {320 // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #628321 // To preserve insertion order in all cases we then clear the set322 if (target.type_ === ArchType.Set && target.copy_) {323 const copy = new Set(target.copy_)324 target.copy_.clear()325 copy.forEach(value => {326 target.copy_!.add(getValue(value))327 })328 }329 }330331 loadPlugin(PluginMapSet, {proxyMap_, proxySet_, fixSetContents})fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
line 332why, one step back332}