map.ts
This file
Path
src/map.ts
165 lines · 3.8 KiB · TypeScript
Last changed by
TS type improvementsMichel Weststratebefore adoptiondd1808e
History of this fileTake it
Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 1why, one step back1import {isDraftable, DRAFT_STATE, latest} 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"TS type improvementsMichel Weststratebefore adoptiondd1808e
line 4why, one step back4import {AnyMap, Drafted, ImmerState} from "./types"added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 5–6why, one step back56export interface MapState {Introduced proper state typesMichel Weststratebefore adoption3842f85
line 7why, one step back7 type: "map"TS type improvementsMichel Weststratebefore adoptiondd1808e
line 8why, one step back8 parent?: ImmerStateSome initial cleanupMichel Weststratebefore adoptionc53a75f
lines 9–12why, one step back9 scope: ImmerScope10 modified: boolean11 finalizing: boolean12 finalized: booleanIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 13why, one step back13 copy: AnyMap | undefinedSome initial cleanupMichel Weststratebefore adoptionc53a75f
line 14why, one step back14 assigned: Map<any, boolean> | undefinedIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 15why, one step back15 base: AnyMapSome initial cleanupMichel Weststratebefore adoptionc53a75f
line 16why, one step back16 revoke(): voidIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 17why, one step back17 draft: Drafted<AnyMap, MapState>added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 18–20why, one step back18}1920// Make sure DraftMap declarion doesn't die if Map is not avialable...Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 21–22why, one step back21const MapBase: MapConstructor =22 typeof Map !== "undefined" ? Map : (function FakeMap() {} as any)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 23–24why, one step back2324// TODO: fix types for draftsFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 25why, one step back25// TODO: assert unrevoked, use freeze for thatadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 26–27why, one step back26export class DraftMap<K, V> extends MapBase implements Map<K, V> {27 [DRAFT_STATE]: MapStateTS type improvementsMichel Weststratebefore adoptiondd1808e
line 28why, one step back28 constructor(target: AnyMap, parent?: ImmerState) {added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 29–30why, one step back29 super()30 this[DRAFT_STATE] = {Introduced proper state typesMichel Weststratebefore adoption3842f85
line 31why, one step back31 type: "map",added set and iteror stuffMichel Weststratebefore adoption08f4682
line 32why, one step back32 parent,TS type improvementsMichel Weststratebefore adoptiondd1808e
line 33why, one step back33 scope: parent ? parent.scope : ImmerScope.current!,added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 34–43why, one step back34 modified: false,35 finalized: false,36 finalizing: false,37 copy: undefined,38 assigned: undefined,39 base: target,40 draft: this as any, // TODO: fix typing41 revoke() {42 // TODO: make sure this marks the Map as revoked, and assert everywhere43 }Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 44why, one step back44 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 45–55why, one step back45 }4647 get size(): number {48 return latest(this[DRAFT_STATE]).size49 }5051 has(key: K): boolean {52 return latest(this[DRAFT_STATE]).has(key)53 }5455 set(key: K, value: V): this {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 56why, one step back56 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 57–61why, one step back57 if (latest(state).get(key) !== value) {58 prepareCopy(state)59 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly60 state.assigned!.set(key, true)61 state.copy!.set(key, value)Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 62why, one step back62 state.assigned!.set(key, true)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 63–68why, one step back63 }64 return this65 }6667 delete(key: K): boolean {68 if (!this.has(key)) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 69why, one step back69 return falseadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 70–71why, one step back70 }71Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 72why, one step back72 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 73–80why, one step back73 prepareCopy(state)74 state.scope.immer.markChanged(state)75 state.assigned!.set(key, false)76 state.copy!.delete(key)77 return true78 }7980 clear() {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 81why, one step back81 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 82–90why, one step back82 prepareCopy(state)83 state.scope.immer.markChanged(state)84 state.assigned = new Map()85 for (const key of latest(state).keys()) {86 state.assigned.set(key, false)87 }88 return state.copy!.clear()89 }90TS type improvementsMichel Weststratebefore adoptiondd1808e
line 91why, one step back91 forEach(cb: (value: V, key: K, self: this) => void, thisArg?: any) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 92why, one step back92 const state = this[DRAFT_STATE]TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 93–94why, one step back93 latest(state).forEach((_value: V, key: K, _map: this) => {94 cb.call(thisArg, this.get(key), key, this)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 95–97why, one step back95 })96 }97Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 98–99why, one step back98 get(key: K): V /* TODO: Draft<V> */ {99 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 100–106why, one step back100 const value = latest(state).get(key)101 if (state.finalizing || state.finalized || !isDraftable(value)) {102 return value103 }104 if (value !== state.base.get(key)) {105 return value // either already drafted or reassigned106 }fixed most testsMichel Weststratebefore adoption385c0a4
line 107why, one step back107 // despite what it looks, this creates a draft only once, see above conditionadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 108–114why, one step back108 const draft = state.scope.immer.createProxy(value, state)109 prepareCopy(state)110 state.copy!.set(key, draft)111 return draft112 }113114 keys() {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 115why, one step back115 return latest(this[DRAFT_STATE]).keys()added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 116–117why, one step back116 }117fixed most testsMichel Weststratebefore adoption385c0a4
lines 118–123why, one step back118 values() {119 const iterator = this.keys()120 return {121 [Symbol.iterator]: () => this.values(), // TODO: don't use symbol directly122 next: () => {123 const r = iterator.next()Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 124–125why, one step back124 if (r.done) return r125 const value = this.get(r.value)fixed most testsMichel Weststratebefore adoption385c0a4
line 126why, one step back126 return {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 127–128why, one step back127 done: false,128 valuefixed most testsMichel Weststratebefore adoption385c0a4
lines 129–139why, one step back129 }130 }131 } as any132 }133134 entries() {135 const iterator = this.keys()136 return {137 [Symbol.iterator]: () => this.entries(), // TODO: don't use symbol directly138 next: () => {139 const r = iterator.next()Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 140–141why, one step back140 if (r.done) return r141 const value = this.get(r.value)fixed most testsMichel Weststratebefore adoption385c0a4
line 142why, one step back142 return {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 143–144why, one step back143 done: false,144 value: [r.value, value]fixed most testsMichel Weststratebefore adoption385c0a4
lines 145–149why, one step back145 }146 }147 } as any148 }149Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 150–151why, one step back150 [Symbol.iterator]() {151 // TODO: don't use symbol directlyfixed most testsMichel Weststratebefore adoption385c0a4
lines 152–153why, one step back152 return this.entries()153 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 154–155why, one step back154}155TS type improvementsMichel Weststratebefore adoptiondd1808e
line 156why, one step back156export function proxyMap(target: AnyMap, parent?: ImmerState) {added set and iteror stuffMichel Weststratebefore adoption08f4682
line 157why, one step back157 return new DraftMap(target, parent)TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 158–164why, one step back158}159160function prepareCopy(state: MapState) {161 if (!state.copy) {162 state.assigned = new Map()163 state.copy = new Map(state.base)164 }added set and iteror stuffMichel Weststratebefore adoption08f4682
line 165why, one step back165}