set.ts
This file
Path
src/set.ts
165 lines · 3.6 KiB · TypeScript
Last changed by
fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
History of this fileTake it
fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 1–14why, one step back1import {2 __extends,3 ImmerBaseState,4 ProxyType,5 AnySet,6 Drafted,7 ImmerState,8 DRAFT_STATE,9 ImmerScope,10 latest,11 assertUnrevoked,12 iteratorSymbol,13 isDraftable14} from "./internal"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 15–24why, one step back1516export interface SetState extends ImmerBaseState {17 type: ProxyType.Set18 copy: AnySet | undefined19 base: AnySet20 drafts: Map<any, Drafted> // maps the original value to the draft value in the new set21 revoked: boolean22 draft: Drafted<AnySet, SetState>23}24fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 25–32why, one step back25const DraftSet = (function(_super) {26 if (!_super) {27 /* istanbul ignore next */28 throw new Error("Set is not polyfilled")29 }30 __extends(DraftSet, _super)31 // Create class manually, cause #50232 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 33–45why, one step back33 this[DRAFT_STATE] = {34 type: ProxyType.Set,35 parent,36 scope: parent ? parent.scope : ImmerScope.current!,37 modified: false,38 finalized: false,39 copy: undefined,40 base: target,41 draft: this,42 drafts: new Map(),43 revoked: false,44 isManual: false45 }fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 46why, one step back46 return thisfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 47why, one step back47 }fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 48why, one step back48 const p = DraftSet.prototypefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 49why, one step back49fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 50–56why, one step back50 Object.defineProperty(p, "size", {51 get: function() {52 return latest(this[DRAFT_STATE]).size53 },54 enumerable: true,55 configurable: true56 })feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 57why, one step back57fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 58why, one step back58 p.has = function(value: any): boolean {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 59–70why, one step back59 const state = this[DRAFT_STATE]60 assertUnrevoked(state)61 // bit of trickery here, to be able to recognize both the value, and the draft of its value62 if (!state.copy) {63 return state.base.has(value)64 }65 if (state.copy.has(value)) return true66 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))67 return true68 return false69 }70fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 71why, one step back71 p.add = function(value: any): any {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 72–83why, one step back72 const state = this[DRAFT_STATE]73 assertUnrevoked(state)74 if (state.copy) {75 state.copy.add(value)76 } else if (!state.base.has(value)) {77 prepareCopy(state)78 state.scope.immer.markChanged(state)79 state.copy!.add(value)80 }81 return this82 }83fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 84why, one step back84 p.delete = function(value: any): any {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 85–100why, one step back85 if (!this.has(value)) {86 return false87 }8889 const state = this[DRAFT_STATE]90 assertUnrevoked(state)91 prepareCopy(state)92 state.scope.immer.markChanged(state)93 return (94 state.copy!.delete(value) ||95 (state.drafts.has(value)96 ? state.copy!.delete(state.drafts.get(value))97 : /* istanbul ignore next */ false)98 )99 }100fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 101why, one step back101 p.clear = function() {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 102–108why, one step back102 const state = this[DRAFT_STATE]103 assertUnrevoked(state)104 prepareCopy(state)105 state.scope.immer.markChanged(state)106 return state.copy!.clear()107 }108fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 109why, one step back109 p.values = function(): IterableIterator<any> {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 110–115why, one step back110 const state = this[DRAFT_STATE]111 assertUnrevoked(state)112 prepareCopy(state)113 return state.copy!.values()114 }115fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 116why, one step back116 p.entries = function entries(): IterableIterator<[any, any]> {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 117–122why, one step back117 const state = this[DRAFT_STATE]118 assertUnrevoked(state)119 prepareCopy(state)120 return state.copy!.entries()121 }122fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 123why, one step back123 p.keys = function(): IterableIterator<any> {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 124–126why, one step back124 return this.values()125 }126fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 127why, one step back127 p[iteratorSymbol] = function() {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 128–130why, one step back128 return this.values()129 }130fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 131why, one step back131 p.forEach = function forEach(cb: any, thisArg?: any) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 132–139why, one step back132 const iterator = this.values()133 let result = iterator.next()134 while (!result.done) {135 cb.call(thisArg, result.value, result.value, this)136 result = iterator.next()137 }138 }139fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 140–147why, one step back140 return DraftSet141})(Set)142143export function proxySet<T extends AnySet>(144 target: T,145 parent?: ImmerState146): T & {[DRAFT_STATE]: SetState} {147 // @ts-ignorefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 148–165why, one step back148 return new DraftSet(target, parent)149}150151function prepareCopy(state: SetState) {152 if (!state.copy) {153 // create drafts for all entries to preserve insertion order154 state.copy = new Set()155 state.base.forEach(value => {156 if (isDraftable(value)) {157 const draft = state.scope.immer.createProxy(value, state)158 state.drafts.set(value, draft)159 state.copy!.add(draft)160 } else {161 state.copy!.add(value)162 }163 })164 }165}