map.ts
This file
Path
src/map.ts
160 lines · 3.6 KiB · TypeScript
Last changed by
Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
History of this fileTake it
Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 1why, one step back1import {isDraftable, DRAFT_STATE, latest, iteratorSymbol} from "./common"added set and iteror stuffMichel Weststratebefore adoption08f4682
line 2why, one step back2Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 3–5why, one step back3import {ImmerScope} from "./scope"4import {AnyMap, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"5import {assertUnrevoked} from "./es5"added set and iteror stuffMichel Weststratebefore adoption08f4682
line 6why, one step back6Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 7–13why, one step back7export interface MapState extends ImmerBaseState {8 type: ProxyType.Map9 copy: AnyMap | undefined10 assigned: Map<any, boolean> | undefined11 base: AnyMap12 revoked: boolean13 draft: Drafted<AnyMap, MapState>added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 14–16why, one step back14}1516// Make sure DraftMap declarion doesn't die if Map is not avialable...Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 17–18why, one step back17const MapBase: MapConstructor =18 typeof Map !== "undefined" ? Map : (function FakeMap() {} as any)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 19–21why, one step back1920export class DraftMap<K, V> extends MapBase implements Map<K, V> {21 [DRAFT_STATE]: MapStateMerge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 22why, one step back22 constructor(target: AnyMap, parent?: ImmerState) {added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 23–24why, one step back23 super()24 this[DRAFT_STATE] = {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 25why, one step back25 type: ProxyType.Map,added set and iteror stuffMichel Weststratebefore adoption08f4682
line 26why, one step back26 parent,Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 27why, one step back27 scope: parent ? parent.scope : ImmerScope.current!,added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 28–32why, one step back28 modified: false,29 finalized: false,30 copy: undefined,31 assigned: undefined,32 base: target,Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 33–36why, one step back33 draft: this,34 isManual: false,35 revoked: false36 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 37–47why, one step back37 }3839 get size(): number {40 return latest(this[DRAFT_STATE]).size41 }4243 has(key: K): boolean {44 return latest(this[DRAFT_STATE]).has(key)45 }4647 set(key: K, value: V): this {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 48–49why, one step back48 const state = this[DRAFT_STATE]49 assertUnrevoked(state)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 50–51why, one step back50 if (latest(state).get(key) !== value) {51 prepareCopy(state)Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 52why, one step back52 state.scope.immer.markChanged(state)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 53–54why, one step back53 state.assigned!.set(key, true)54 state.copy!.set(key, value)Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 55why, one step back55 state.assigned!.set(key, true)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 56–61why, one step back56 }57 return this58 }5960 delete(key: K): boolean {61 if (!this.has(key)) {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 62why, one step back62 return falseadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 63–64why, one step back63 }64Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 65–66why, one step back65 const state = this[DRAFT_STATE]66 assertUnrevoked(state)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 67–74why, one step back67 prepareCopy(state)68 state.scope.immer.markChanged(state)69 state.assigned!.set(key, false)70 state.copy!.delete(key)71 return true72 }7374 clear() {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 75–76why, one step back75 const state = this[DRAFT_STATE]76 assertUnrevoked(state)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 77–85why, one step back77 prepareCopy(state)78 state.scope.immer.markChanged(state)79 state.assigned = new Map()80 for (const key of latest(state).keys()) {81 state.assigned.set(key, false)82 }83 return state.copy!.clear()84 }85Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 86–89why, one step back86 forEach(cb: (value: V, key: K, self: this) => void, thisArg?: any) {87 const state = this[DRAFT_STATE]88 latest(state).forEach((_value: V, key: K, _map: this) => {89 cb.call(thisArg, this.get(key), key, this)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 90–92why, one step back90 })91 }92Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 93–95why, one step back93 get(key: K): V {94 const state = this[DRAFT_STATE]95 assertUnrevoked(state)added set and iteror stuffMichel Weststratebefore adoption08f4682
line 96why, one step back96 const value = latest(state).get(key)Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 97why, one step back97 if (state.finalized || !isDraftable(value)) {added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 98–102why, one step back98 return value99 }100 if (value !== state.base.get(key)) {101 return value // either already drafted or reassigned102 }fixed most testsMichel Weststratebefore adoption385c0a4
line 103why, one step back103 // despite what it looks, this creates a draft only once, see above conditionadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 104–109why, one step back104 const draft = state.scope.immer.createProxy(value, state)105 prepareCopy(state)106 state.copy!.set(key, draft)107 return draft108 }109Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 110–111why, one step back110 keys(): IterableIterator<K> {111 return latest(this[DRAFT_STATE]).keys()added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 112–113why, one step back112 }113Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 114why, one step back114 values(): IterableIterator<V> {fixed most testsMichel Weststratebefore adoption385c0a4
lines 115–116why, one step back115 const iterator = this.keys()116 return {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 117why, one step back117 [iteratorSymbol]: () => this.values(),fixed most testsMichel Weststratebefore adoption385c0a4
lines 118–119why, one step back118 next: () => {119 const r = iterator.next()Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 120–121why, one step back120 if (r.done) return r121 const value = this.get(r.value)fixed most testsMichel Weststratebefore adoption385c0a4
line 122why, one step back122 return {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 123–124why, one step back123 done: false,124 valuefixed most testsMichel Weststratebefore adoption385c0a4
lines 125–129why, one step back125 }126 }127 } as any128 }129Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 130why, one step back130 entries(): IterableIterator<[K, V]> {fixed most testsMichel Weststratebefore adoption385c0a4
lines 131–132why, one step back131 const iterator = this.keys()132 return {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 133why, one step back133 [iteratorSymbol]: () => this.entries(),fixed most testsMichel Weststratebefore adoption385c0a4
lines 134–135why, one step back134 next: () => {135 const r = iterator.next()Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 136–137why, one step back136 if (r.done) return r137 const value = this.get(r.value)fixed most testsMichel Weststratebefore adoption385c0a4
line 138why, one step back138 return {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 139–140why, one step back139 done: false,140 value: [r.value, value]fixed most testsMichel Weststratebefore adoption385c0a4
lines 141–145why, one step back141 }142 }143 } as any144 }145Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 146why, one step back146 [iteratorSymbol]() {fixed most testsMichel Weststratebefore adoption385c0a4
lines 147–148why, one step back147 return this.entries()148 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 149–150why, one step back149}150Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 151why, one step back151export function proxyMap(target: AnyMap, parent?: ImmerState) {added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 152–154why, one step back152 return new DraftMap(target, parent)153}154Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 155–158why, one step back155function prepareCopy(state: MapState) {156 if (!state.copy) {157 state.assigned = new Map()158 state.copy = new Map(state.base)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 159–160why, one step back159 }160}