map.ts
This file
Path
src/map.ts
163 lines · 3.7 KiB · TypeScript
Last changed by
feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
History of this fileTake it
feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 1–163why, one step back1import {isDraftable, DRAFT_STATE, latest, iteratorSymbol} from "./common"23import {ImmerScope} from "./scope"4import {AnyMap, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"5import {assertUnrevoked} from "./es5"67export interface MapState extends ImmerBaseState {8 type: ProxyType.Map9 copy: AnyMap | undefined10 assigned: Map<any, boolean> | undefined11 base: AnyMap12 revoked: boolean13 draft: Drafted<AnyMap, MapState>14}1516// Make sure DraftMap declarion doesn't die if Map is not avialable...17/* istanbul ignore next */18const MapBase: MapConstructor =19 typeof Map !== "undefined" ? Map : (function FakeMap() {} as any)2021export class DraftMap<K, V> extends MapBase implements Map<K, V> {22 [DRAFT_STATE]: MapState23 constructor(target: AnyMap, parent?: ImmerState) {24 super()25 this[DRAFT_STATE] = {26 type: ProxyType.Map,27 parent,28 scope: parent ? parent.scope : ImmerScope.current!,29 modified: false,30 finalized: false,31 copy: undefined,32 assigned: undefined,33 base: target,34 draft: this,35 isManual: false,36 revoked: false37 }38 }3940 get size(): number {41 return latest(this[DRAFT_STATE]).size42 }4344 has(key: K): boolean {45 return latest(this[DRAFT_STATE]).has(key)46 }4748 set(key: K, value: V): this {49 const state = this[DRAFT_STATE]50 assertUnrevoked(state)51 if (latest(state).get(key) !== value) {52 prepareCopy(state)53 state.scope.immer.markChanged(state)54 state.assigned!.set(key, true)55 state.copy!.set(key, value)56 state.assigned!.set(key, true)57 }58 return this59 }6061 delete(key: K): boolean {62 if (!this.has(key)) {63 return false64 }6566 const state = this[DRAFT_STATE]67 assertUnrevoked(state)68 prepareCopy(state)69 state.scope.immer.markChanged(state)70 state.assigned!.set(key, false)71 state.copy!.delete(key)72 return true73 }7475 clear() {76 const state = this[DRAFT_STATE]77 assertUnrevoked(state)78 prepareCopy(state)79 state.scope.immer.markChanged(state)80 state.assigned = new Map()81 for (const key of latest(state).keys()) {82 state.assigned.set(key, false)83 }84 return state.copy!.clear()85 }8687 forEach(cb: (value: V, key: K, self: this) => void, thisArg?: any) {88 const state = this[DRAFT_STATE]89 latest(state).forEach((_value: V, key: K, _map: this) => {90 cb.call(thisArg, this.get(key), key, this)91 })92 }9394 get(key: K): V {95 const state = this[DRAFT_STATE]96 assertUnrevoked(state)97 const value = latest(state).get(key)98 if (state.finalized || !isDraftable(value)) {99 return value100 }101 if (value !== state.base.get(key)) {102 return value // either already drafted or reassigned103 }104 // despite what it looks, this creates a draft only once, see above condition105 const draft = state.scope.immer.createProxy(value, state)106 prepareCopy(state)107 state.copy!.set(key, draft)108 return draft109 }110111 keys(): IterableIterator<K> {112 return latest(this[DRAFT_STATE]).keys()113 }114115 values(): IterableIterator<V> {116 const iterator = this.keys()117 return {118 [iteratorSymbol]: () => this.values(),119 next: () => {120 const r = iterator.next()121 /* istanbul ignore next */122 if (r.done) return r123 const value = this.get(r.value)124 return {125 done: false,126 value127 }128 }129 } as any130 }131132 entries(): IterableIterator<[K, V]> {133 const iterator = this.keys()134 return {135 [iteratorSymbol]: () => this.entries(),136 next: () => {137 const r = iterator.next()138 /* istanbul ignore next */139 if (r.done) return r140 const value = this.get(r.value)141 return {142 done: false,143 value: [r.value, value]144 }145 }146 } as any147 }148149 [iteratorSymbol]() {150 return this.entries()151 }152}153154export function proxyMap(target: AnyMap, parent?: ImmerState) {155 return new DraftMap(target, parent)156}157158function prepareCopy(state: MapState) {159 if (!state.copy) {160 state.assigned = new Map()161 state.copy = new Map(state.base)162 }163}