map.ts
This file
Path
src/map.ts
184 lines · 4.0 KiB · TypeScript
Last changed by
feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
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 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: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
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: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
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: 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 = DraftMap.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–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: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
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: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
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: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
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: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 90why, one step back90 p.clear = function() {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 91–98why, 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 return state.copy!.clear()97 }98fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 99–102why, one step back99 p.forEach = function(100 cb: (value: any, key: any, self: any) => void,101 thisArg?: any102 ) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 103why, one step back103 const state = this[DRAFT_STATE]fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 104why, one step back104 latest(state).forEach((_value: any, key: any, _map: any) => {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 105–108why, one step back105 cb.call(thisArg, this.get(key), key, this)106 })107 }108fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 109why, one step back109 p.get = function(key: any): any {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 110–125why, one step back110 const state = this[DRAFT_STATE]111 assertUnrevoked(state)112 const value = latest(state).get(key)113 if (state.finalized || !isDraftable(value)) {114 return value115 }116 if (value !== state.base.get(key)) {117 return value // either already drafted or reassigned118 }119 // despite what it looks, this creates a draft only once, see above condition120 const draft = state.scope.immer.createProxy(value, state)121 prepareCopy(state)122 state.copy!.set(key, draft)123 return draft124 }125fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 126why, one step back126 p.keys = function(): IterableIterator<any> {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 127–129why, one step back127 return latest(this[DRAFT_STATE]).keys()128 }129fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 130why, one step back130 p.values = function(): IterableIterator<any> {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 131–146why, one step back131 const iterator = this.keys()132 return {133 [iteratorSymbol]: () => this.values(),134 next: () => {135 const r = iterator.next()136 /* istanbul ignore next */137 if (r.done) return r138 const value = this.get(r.value)139 return {140 done: false,141 value142 }143 }144 } as any145 }146fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 147why, one step back147 p.entries = function(): IterableIterator<[any, any]> {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 148–163why, one step back148 const iterator = this.keys()149 return {150 [iteratorSymbol]: () => this.entries(),151 next: () => {152 const r = iterator.next()153 /* istanbul ignore next */154 if (r.done) return r155 const value = this.get(r.value)156 return {157 done: false,158 value: [r.value, value]159 }160 }161 } as any162 }163fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 164why, one step back164 p[iteratorSymbol] = function() {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 165–167why, one step back165 return this.entries()166 }167fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 168–175why, one step back168 return DraftMap169})(Map)170171export function proxyMap<T extends AnyMap>(172 target: T,173 parent?: ImmerState174): T & {[DRAFT_STATE]: MapState} {175 // @ts-ignorefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 176–184why, one step back176 return new DraftMap(target, parent)177}178179function prepareCopy(state: MapState) {180 if (!state.copy) {181 state.assigned = new Map()182 state.copy = new Map(state.base)183 }184}