map.ts
This file
Path
src/map.ts
159 lines · 3.7 KiB · TypeScript
Last changed by
Enums FTWMichel Weststratebefore adoptionfd76f1c
History of this fileTake it
Processed bunch of todosMichel Weststratebefore adoption00425e1
line 1why, one step back1import {isDraftable, DRAFT_STATE, latest, iteratorSymbol} from "./common"added set and iteror stuffMichel Weststratebefore adoption08f4682
line 2why, one step back2Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 3why, one step back3import {ImmerScope} from "./scope"Enums FTWMichel Weststratebefore adoptionfd76f1c
line 4why, one step back4import {AnyMap, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"added set and iteror stuffMichel Weststratebefore adoption08f4682
line 5why, one step back5Processed bunch of todosMichel Weststratebefore adoption00425e1
line 6why, one step back6export interface MapState extends ImmerBaseState {Enums FTWMichel Weststratebefore adoptionfd76f1c
line 7why, one step back7 type: ProxyType.MapSome initial cleanupMichel Weststratebefore adoptionc53a75f
line 8why, one step back8 finalizing: booleanIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 9why, one step back9 copy: AnyMap | undefinedSome initial cleanupMichel Weststratebefore adoptionc53a75f
line 10why, one step back10 assigned: Map<any, boolean> | undefinedIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 11why, one step back11 base: AnyMapSome initial cleanupMichel Weststratebefore adoptionc53a75f
line 12why, one step back12 revoke(): voidIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 13why, one step back13 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...Some initial cleanupMichel Weststratebefore adoptionc53a75f
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]: MapStateTS type improvementsMichel Weststratebefore adoptiondd1808e
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] = {Enums FTWMichel Weststratebefore adoptionfd76f1c
line 25why, one step back25 type: ProxyType.Map,added set and iteror stuffMichel Weststratebefore adoption08f4682
line 26why, one step back26 parent,TS type improvementsMichel Weststratebefore adoptiondd1808e
line 27why, one step back27 scope: parent ? parent.scope : ImmerScope.current!,added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 28–33why, one step back28 modified: false,29 finalized: false,30 finalizing: false,31 copy: undefined,32 assigned: undefined,33 base: target,Processed bunch of todosMichel Weststratebefore adoption00425e1
line 34why, one step back34 draft: this,added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 35–36why, one step back35 revoke() {36 // TODO: make sure this marks the Map as revoked, and assert everywhereProcessed bunch of todosMichel Weststratebefore adoption00425e1
lines 37–38why, one step back37 },38 isManual: falseSome initial cleanupMichel Weststratebefore adoptionc53a75f
line 39why, one step back39 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 40–50why, one step back40 }4142 get size(): number {43 return latest(this[DRAFT_STATE]).size44 }4546 has(key: K): boolean {47 return latest(this[DRAFT_STATE]).has(key)48 }4950 set(key: K, value: V): this {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 51why, one step back51 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 52–56why, one step back52 if (latest(state).get(key) !== value) {53 prepareCopy(state)54 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly55 state.assigned!.set(key, true)56 state.copy!.set(key, value)Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 57why, one step back57 state.assigned!.set(key, true)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 58–63why, one step back58 }59 return this60 }6162 delete(key: K): boolean {63 if (!this.has(key)) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 64why, one step back64 return falseadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 65–66why, one step back65 }66Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 67why, one step back67 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 68–75why, one step back68 prepareCopy(state)69 state.scope.immer.markChanged(state)70 state.assigned!.set(key, false)71 state.copy!.delete(key)72 return true73 }7475 clear() {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 76why, one step back76 const state = this[DRAFT_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 }85TS type improvementsMichel Weststratebefore adoptiondd1808e
line 86why, one step back86 forEach(cb: (value: V, key: K, self: this) => void, thisArg?: any) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 87why, one step back87 const state = this[DRAFT_STATE]TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 88–89why, one step back88 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 }92Processed bunch of todosMichel Weststratebefore adoption00425e1
line 93why, one step back93 get(key: K): V {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 94why, one step back94 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 95–101why, one step back95 const value = latest(state).get(key)96 if (state.finalizing || state.finalized || !isDraftable(value)) {97 return value98 }99 if (value !== state.base.get(key)) {100 return value // either already drafted or reassigned101 }fixed most testsMichel Weststratebefore adoption385c0a4
line 102why, one step back102 // despite what it looks, this creates a draft only once, see above conditionadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 103–108why, one step back103 const draft = state.scope.immer.createProxy(value, state)104 prepareCopy(state)105 state.copy!.set(key, draft)106 return draft107 }108Processed bunch of todosMichel Weststratebefore adoption00425e1
line 109why, one step back109 keys(): IterableIterator<K> {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 110why, one step back110 return latest(this[DRAFT_STATE]).keys()added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 111–112why, one step back111 }112Processed bunch of todosMichel Weststratebefore adoption00425e1
line 113why, one step back113 values(): IterableIterator<V> {fixed most testsMichel Weststratebefore adoption385c0a4
lines 114–115why, one step back114 const iterator = this.keys()115 return {Processed bunch of todosMichel Weststratebefore adoption00425e1
line 116why, one step back116 [iteratorSymbol]: () => this.values(),fixed most testsMichel Weststratebefore adoption385c0a4
lines 117–118why, one step back117 next: () => {118 const r = iterator.next()Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 119–120why, one step back119 if (r.done) return r120 const value = this.get(r.value)fixed most testsMichel Weststratebefore adoption385c0a4
line 121why, one step back121 return {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 122–123why, one step back122 done: false,123 valuefixed most testsMichel Weststratebefore adoption385c0a4
lines 124–128why, one step back124 }125 }126 } as any127 }128Processed bunch of todosMichel Weststratebefore adoption00425e1
line 129why, one step back129 entries(): IterableIterator<[K, V]> {fixed most testsMichel Weststratebefore adoption385c0a4
lines 130–131why, one step back130 const iterator = this.keys()131 return {Processed bunch of todosMichel Weststratebefore adoption00425e1
line 132why, one step back132 [iteratorSymbol]: () => this.entries(),fixed most testsMichel Weststratebefore adoption385c0a4
lines 133–134why, one step back133 next: () => {134 const r = iterator.next()Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 135–136why, one step back135 if (r.done) return r136 const value = this.get(r.value)fixed most testsMichel Weststratebefore adoption385c0a4
line 137why, one step back137 return {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 138–139why, one step back138 done: false,139 value: [r.value, value]fixed most testsMichel Weststratebefore adoption385c0a4
lines 140–144why, one step back140 }141 }142 } as any143 }144Processed bunch of todosMichel Weststratebefore adoption00425e1
line 145why, one step back145 [iteratorSymbol]() {fixed most testsMichel Weststratebefore adoption385c0a4
lines 146–147why, one step back146 return this.entries()147 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 148–149why, one step back148}149TS type improvementsMichel Weststratebefore adoptiondd1808e
line 150why, one step back150export function proxyMap(target: AnyMap, parent?: ImmerState) {added set and iteror stuffMichel Weststratebefore adoption08f4682
line 151why, one step back151 return new DraftMap(target, parent)TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 152–158why, one step back152}153154function prepareCopy(state: MapState) {155 if (!state.copy) {156 state.assigned = new Map()157 state.copy = new Map(state.base)158 }added set and iteror stuffMichel Weststratebefore adoption08f4682
line 159why, one step back159}