map.ts
This file
Path
src/map.ts
187 lines · 4.0 KiB · TypeScript
Last changed by
applied module ordering patternMichel Weststratebefore adoption7639c29
History of this fileTake it
applied module ordering patternMichel Weststratebefore adoption7639c29
lines 1–14why, one step back1import {2 __extends,3 ImmerBaseState,4 ProxyType,5 AnyMap,6 Drafted,7 ImmerState,8 DRAFT_STATE,9 ImmerScope,10 latest,11 assertUnrevoked,12 isDraftable,13 iteratorSymbol14} from "./internal"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 15–24why, one step back1516export interface MapState extends ImmerBaseState {17 type: ProxyType.Map18 copy: AnyMap | undefined19 assigned: Map<any, boolean> | undefined20 base: AnyMap21 revoked: boolean22 draft: Drafted<AnyMap, MapState>23}24fix: #502Michel Weststratebefore adoption8d4f723
lines 25–32why, one step back25const DraftMap = (function(_super) {26 if (!_super) {27 /* istanbul ignore next */28 throw new Error("Map is not polyfilled")29 }30 __extends(DraftMap, _super)31 // Create class manually, cause #50232 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 33–41why, one step back33 this[DRAFT_STATE] = {34 type: ProxyType.Map,35 parent,36 scope: parent ? parent.scope : ImmerScope.current!,37 modified: false,38 finalized: false,39 copy: undefined,40 assigned: undefined,41 base: target,fix: #502Michel Weststratebefore adoption8d4f723
line 42why, one step back42 draft: this as any,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 43–45why, one step back43 isManual: false,44 revoked: false45 }fix: #502Michel Weststratebefore adoption8d4f723
line 46why, one step back46 return thisfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 47why, one step back47 }fix: #502Michel Weststratebefore adoption8d4f723
line 48why, one step back48 const p = DraftMap.prototypefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 49why, one step back49fix: #502Michel Weststratebefore adoption8d4f723
lines 50–57why, one step back50 // TODO: smaller build size if we create a util for Object.defineProperty51 Object.defineProperty(p, "size", {52 get: function() {53 return latest(this[DRAFT_STATE]).size54 },55 enumerable: true,56 configurable: true57 })feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 58why, one step back58fix: #502Michel Weststratebefore adoption8d4f723
line 59why, one step back59 p.has = function(key: any): boolean {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 60–62why, one step back60 return latest(this[DRAFT_STATE]).has(key)61 }62fix: #502Michel Weststratebefore adoption8d4f723
line 63why, one step back63 p.set = function(key: any, value: any) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 64–75why, one step back64 const state = this[DRAFT_STATE]65 assertUnrevoked(state)66 if (latest(state).get(key) !== value) {67 prepareCopy(state)68 state.scope.immer.markChanged(state)69 state.assigned!.set(key, true)70 state.copy!.set(key, value)71 state.assigned!.set(key, true)72 }73 return this74 }75fix: #502Michel Weststratebefore adoption8d4f723
line 76why, one step back76 p.delete = function(key: any): boolean {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 77–89why, one step back77 if (!this.has(key)) {78 return false79 }8081 const state = this[DRAFT_STATE]82 assertUnrevoked(state)83 prepareCopy(state)84 state.scope.immer.markChanged(state)85 state.assigned!.set(key, false)86 state.copy!.delete(key)87 return true88 }89fix: #502Michel Weststratebefore adoption8d4f723
line 90why, one step back90 p.clear = function() {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 91–101why, one step back91 const state = this[DRAFT_STATE]92 assertUnrevoked(state)93 prepareCopy(state)94 state.scope.immer.markChanged(state)95 state.assigned = new Map()96 for (const key of latest(state).keys()) {97 state.assigned.set(key, false)98 }99 return state.copy!.clear()100 }101fix: #502Michel Weststratebefore adoption8d4f723
lines 102–105why, one step back102 p.forEach = function(103 cb: (value: any, key: any, self: any) => void,104 thisArg?: any105 ) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 106why, one step back106 const state = this[DRAFT_STATE]fix: #502Michel Weststratebefore adoption8d4f723
line 107why, one step back107 latest(state).forEach((_value: any, key: any, _map: any) => {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 108–111why, one step back108 cb.call(thisArg, this.get(key), key, this)109 })110 }111fix: #502Michel Weststratebefore adoption8d4f723
line 112why, one step back112 p.get = function(key: any): any {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 113–128why, one step back113 const state = this[DRAFT_STATE]114 assertUnrevoked(state)115 const value = latest(state).get(key)116 if (state.finalized || !isDraftable(value)) {117 return value118 }119 if (value !== state.base.get(key)) {120 return value // either already drafted or reassigned121 }122 // despite what it looks, this creates a draft only once, see above condition123 const draft = state.scope.immer.createProxy(value, state)124 prepareCopy(state)125 state.copy!.set(key, draft)126 return draft127 }128fix: #502Michel Weststratebefore adoption8d4f723
line 129why, one step back129 p.keys = function(): IterableIterator<any> {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 130–132why, one step back130 return latest(this[DRAFT_STATE]).keys()131 }132fix: #502Michel Weststratebefore adoption8d4f723
line 133why, one step back133 p.values = function(): IterableIterator<any> {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 134–149why, one step back134 const iterator = this.keys()135 return {136 [iteratorSymbol]: () => this.values(),137 next: () => {138 const r = iterator.next()139 /* istanbul ignore next */140 if (r.done) return r141 const value = this.get(r.value)142 return {143 done: false,144 value145 }146 }147 } as any148 }149fix: #502Michel Weststratebefore adoption8d4f723
line 150why, one step back150 p.entries = function(): IterableIterator<[any, any]> {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 151–166why, one step back151 const iterator = this.keys()152 return {153 [iteratorSymbol]: () => this.entries(),154 next: () => {155 const r = iterator.next()156 /* istanbul ignore next */157 if (r.done) return r158 const value = this.get(r.value)159 return {160 done: false,161 value: [r.value, value]162 }163 }164 } as any165 }166fix: #502Michel Weststratebefore adoption8d4f723
line 167why, one step back167 p[iteratorSymbol] = function() {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 168–170why, one step back168 return this.entries()169 }170fix: #502Michel Weststratebefore adoption8d4f723
lines 171–178why, one step back171 return DraftMap172})(Map)173174export function proxyMap<T extends AnyMap>(175 target: T,176 parent?: ImmerState177): T & {[DRAFT_STATE]: MapState} {178 // @ts-ignorefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 179–187why, one step back179 return new DraftMap(target, parent)180}181182function prepareCopy(state: MapState) {183 if (!state.copy) {184 state.assigned = new Map()185 state.copy = new Map(state.base)186 }187}