map.ts
This file
Path
src/map.ts
184 lines · 4.3 KiB · TypeScript
Last changed by
fix: #502Michel Weststratebefore adoption8d4f723
History of this fileTake it
fix: #502Michel Weststratebefore adoption8d4f723
lines 1–2why, one step back1import {__extends} from "./extends"2import {isDraftable, latest} from "./common"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 3–6why, one step back34import {ImmerScope} from "./scope"5import {AnyMap, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"6import {assertUnrevoked} from "./es5"fix: #502Michel Weststratebefore adoption8d4f723
line 7why, one step back7import {DRAFT_STATE, iteratorSymbol, hasMap} from "./env"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 8–17why, one step back89export interface MapState extends ImmerBaseState {10 type: ProxyType.Map11 copy: AnyMap | undefined12 assigned: Map<any, boolean> | undefined13 base: AnyMap14 revoked: boolean15 draft: Drafted<AnyMap, MapState>16}17fix: #502Michel Weststratebefore adoption8d4f723
lines 18–25why, one step back18const DraftMap = (function(_super) {19 if (!_super) {20 /* istanbul ignore next */21 throw new Error("Map is not polyfilled")22 }23 __extends(DraftMap, _super)24 // Create class manually, cause #50225 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 26–34why, one step back26 this[DRAFT_STATE] = {27 type: ProxyType.Map,28 parent,29 scope: parent ? parent.scope : ImmerScope.current!,30 modified: false,31 finalized: false,32 copy: undefined,33 assigned: undefined,34 base: target,fix: #502Michel Weststratebefore adoption8d4f723
line 35why, one step back35 draft: this as any,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 36–38why, one step back36 isManual: false,37 revoked: false38 }fix: #502Michel Weststratebefore adoption8d4f723
line 39why, one step back39 return thisfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 40why, one step back40 }fix: #502Michel Weststratebefore adoption8d4f723
line 41why, one step back41 const p = DraftMap.prototypefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 42why, one step back42fix: #502Michel Weststratebefore adoption8d4f723
lines 43–50why, one step back43 // TODO: smaller build size if we create a util for Object.defineProperty44 Object.defineProperty(p, "size", {45 get: function() {46 return latest(this[DRAFT_STATE]).size47 },48 enumerable: true,49 configurable: true50 })feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 51why, one step back51fix: #502Michel Weststratebefore adoption8d4f723
line 52why, one step back52 p.has = function(key: any): boolean {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 53–55why, one step back53 return latest(this[DRAFT_STATE]).has(key)54 }55fix: #502Michel Weststratebefore adoption8d4f723
line 56why, one step back56 p.set = function(key: any, value: any) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 57–68why, one step back57 const state = this[DRAFT_STATE]58 assertUnrevoked(state)59 if (latest(state).get(key) !== value) {60 prepareCopy(state)61 state.scope.immer.markChanged(state)62 state.assigned!.set(key, true)63 state.copy!.set(key, value)64 state.assigned!.set(key, true)65 }66 return this67 }68fix: #502Michel Weststratebefore adoption8d4f723
line 69why, one step back69 p.delete = function(key: any): boolean {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 70–82why, one step back70 if (!this.has(key)) {71 return false72 }7374 const state = this[DRAFT_STATE]75 assertUnrevoked(state)76 prepareCopy(state)77 state.scope.immer.markChanged(state)78 state.assigned!.set(key, false)79 state.copy!.delete(key)80 return true81 }82fix: #502Michel Weststratebefore adoption8d4f723
line 83why, one step back83 p.clear = function() {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 84–94why, one step back84 const state = this[DRAFT_STATE]85 assertUnrevoked(state)86 prepareCopy(state)87 state.scope.immer.markChanged(state)88 state.assigned = new Map()89 for (const key of latest(state).keys()) {90 state.assigned.set(key, false)91 }92 return state.copy!.clear()93 }94fix: #502Michel Weststratebefore adoption8d4f723
lines 95–98why, one step back95 p.forEach = function(96 cb: (value: any, key: any, self: any) => void,97 thisArg?: any98 ) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 99why, one step back99 const state = this[DRAFT_STATE]fix: #502Michel Weststratebefore adoption8d4f723
line 100why, one step back100 latest(state).forEach((_value: any, key: any, _map: any) => {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 101–104why, one step back101 cb.call(thisArg, this.get(key), key, this)102 })103 }104fix: #502Michel Weststratebefore adoption8d4f723
line 105why, one step back105 p.get = function(key: any): any {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 106–121why, one step back106 const state = this[DRAFT_STATE]107 assertUnrevoked(state)108 const value = latest(state).get(key)109 if (state.finalized || !isDraftable(value)) {110 return value111 }112 if (value !== state.base.get(key)) {113 return value // either already drafted or reassigned114 }115 // despite what it looks, this creates a draft only once, see above condition116 const draft = state.scope.immer.createProxy(value, state)117 prepareCopy(state)118 state.copy!.set(key, draft)119 return draft120 }121fix: #502Michel Weststratebefore adoption8d4f723
line 122why, one step back122 p.keys = function(): IterableIterator<any> {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 123–125why, one step back123 return latest(this[DRAFT_STATE]).keys()124 }125fix: #502Michel Weststratebefore adoption8d4f723
line 126why, one step back126 p.values = function(): IterableIterator<any> {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 127–142why, one step back127 const iterator = this.keys()128 return {129 [iteratorSymbol]: () => this.values(),130 next: () => {131 const r = iterator.next()132 /* istanbul ignore next */133 if (r.done) return r134 const value = this.get(r.value)135 return {136 done: false,137 value138 }139 }140 } as any141 }142fix: #502Michel Weststratebefore adoption8d4f723
line 143why, one step back143 p.entries = function(): IterableIterator<[any, any]> {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 144–159why, one step back144 const iterator = this.keys()145 return {146 [iteratorSymbol]: () => this.entries(),147 next: () => {148 const r = iterator.next()149 /* istanbul ignore next */150 if (r.done) return r151 const value = this.get(r.value)152 return {153 done: false,154 value: [r.value, value]155 }156 }157 } as any158 }159fix: #502Michel Weststratebefore adoption8d4f723
line 160why, one step back160 p[iteratorSymbol] = function() {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 161–163why, one step back161 return this.entries()162 }163fix: #502Michel Weststratebefore adoption8d4f723
lines 164–171why, one step back164 return DraftMap165})(Map)166167export function proxyMap<T extends AnyMap>(168 target: T,169 parent?: ImmerState170): T & {[DRAFT_STATE]: MapState} {171 // @ts-ignorefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 172–179why, one step back172 return new DraftMap(target, parent)173}174175function prepareCopy(state: MapState) {176 if (!state.copy) {177 state.assigned = new Map()178 state.copy = new Map(state.base)179 }fix: #502Michel Weststratebefore adoption8d4f723
lines 180–183why, one step back180}181182export function isMap(target: any): target is AnyMap {183 return hasMap && (target instanceof Map || target instanceof DraftMap) // TODO: fixfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 184why, one step back184}