map.ts
This file
Path
src/map.ts
190 lines · 4.3 KiB · TypeScript
Last changed by
Introduced proper state typesMichel Weststratebefore adoption3842f85
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
lines 2–3why, one step back23// TODO: kill:Introduced proper state typesMichel Weststratebefore adoption3842f85
line 4why, one step back4import {assertUnrevoked} from "./es5"Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 5–6why, one step back5import {ImmerScope} from "./scope"6import {Immer} from "./immer"Introduced proper state typesMichel Weststratebefore adoption3842f85
line 7why, one step back7import {AnyMap, Drafted} from "./types"added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 8–12why, one step back89// TODO: create own states10// TODO: clean up the maps and such from ES5 / Proxy states1112export interface MapState {Introduced proper state typesMichel Weststratebefore adoption3842f85
line 13why, one step back13 type: "map"Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 14–18why, one step back14 parent: any // TODO: type15 scope: ImmerScope16 modified: boolean17 finalizing: boolean18 finalized: booleanIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 19why, one step back19 copy: AnyMap | undefinedSome initial cleanupMichel Weststratebefore adoptionc53a75f
line 20why, one step back20 assigned: Map<any, boolean> | undefinedIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 21why, one step back21 base: AnyMapSome initial cleanupMichel Weststratebefore adoptionc53a75f
line 22why, one step back22 revoke(): voidIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 23why, one step back23 draft: Drafted<AnyMap, MapState>added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 24–28why, one step back24}2526function prepareCopy(state: MapState) {27 if (!state.copy) {28 state.assigned = new Map()Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 29why, one step back29 state.copy = new Map(state.base)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 30–33why, one step back30 }31}3233// Make sure DraftMap declarion doesn't die if Map is not avialable...Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 34–35why, one step back34const MapBase: MapConstructor =35 typeof Map !== "undefined" ? Map : (function FakeMap() {} as any)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 36–42why, one step back3637// TODO: fix types for drafts38export class DraftMap<K, V> extends MapBase implements Map<K, V> {39 [DRAFT_STATE]: MapState40 constructor(target, parent) {41 super()42 this[DRAFT_STATE] = {Introduced proper state typesMichel Weststratebefore adoption3842f85
line 43why, one step back43 type: "map",added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 44–55why, one step back44 parent,45 scope: parent ? parent.scope : ImmerScope.current,46 modified: false,47 finalized: false,48 finalizing: false,49 copy: undefined,50 assigned: undefined,51 base: target,52 draft: this as any, // TODO: fix typing53 revoke() {54 // TODO: make sure this marks the Map as revoked, and assert everywhere55 }Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 56why, one step back56 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 57–67why, one step back57 }5859 get size(): number {60 return latest(this[DRAFT_STATE]).size61 }6263 has(key: K): boolean {64 return latest(this[DRAFT_STATE]).has(key)65 }6667 set(key: K, value: V): this {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 68why, one step back68 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 69–73why, one step back69 if (latest(state).get(key) !== value) {70 prepareCopy(state)71 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly72 state.assigned!.set(key, true)73 state.copy!.set(key, value)Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 74why, one step back74 state.assigned!.set(key, true)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 75–80why, one step back75 }76 return this77 }7879 delete(key: K): boolean {80 if (!this.has(key)) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 81why, one step back81 return falseadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 82–83why, one step back82 }83Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 84why, one step back84 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 85–92why, one step back85 prepareCopy(state)86 state.scope.immer.markChanged(state)87 state.assigned!.set(key, false)88 state.copy!.delete(key)89 return true90 }9192 clear() {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 93why, one step back93 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 94–104why, one step back94 prepareCopy(state)95 state.scope.immer.markChanged(state)96 state.assigned = new Map()97 for (const key of latest(state).keys()) {98 state.assigned.set(key, false)99 }100 return state.copy!.clear()101 }102103 // @ts-ignore TODO:104 forEach(cb) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 105why, one step back105 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 106–110why, one step back106 latest(state).forEach((_value, key, _map) => {107 cb(this.get(key), key, this)108 })109 }110Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 111–112why, one step back111 get(key: K): V /* TODO: Draft<V> */ {112 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 113–119why, one step back113 const value = latest(state).get(key)114 if (state.finalizing || state.finalized || !isDraftable(value)) {115 return value116 }117 if (value !== state.base.get(key)) {118 return value // either already drafted or reassigned119 }fixed most testsMichel Weststratebefore adoption385c0a4
line 120why, one step back120 // despite what it looks, this creates a draft only once, see above conditionadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 121–127why, one step back121 const draft = state.scope.immer.createProxy(value, state)122 prepareCopy(state)123 state.copy!.set(key, draft)124 return draft125 }126127 keys() {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 128why, one step back128 return latest(this[DRAFT_STATE]).keys()added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 129–130why, one step back129 }130fixed most testsMichel Weststratebefore adoption385c0a4
lines 131–136why, one step back131 values() {132 const iterator = this.keys()133 return {134 [Symbol.iterator]: () => this.values(), // TODO: don't use symbol directly135 next: () => {136 const r = iterator.next()Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 137–138why, one step back137 if (r.done) return r138 const value = this.get(r.value)fixed most testsMichel Weststratebefore adoption385c0a4
line 139why, one step back139 return {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 140–141why, one step back140 done: false,141 valuefixed most testsMichel Weststratebefore adoption385c0a4
lines 142–152why, one step back142 }143 }144 } as any145 }146147 entries() {148 const iterator = this.keys()149 return {150 [Symbol.iterator]: () => this.entries(), // TODO: don't use symbol directly151 next: () => {152 const r = iterator.next()Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 153–154why, one step back153 if (r.done) return r154 const value = this.get(r.value)fixed most testsMichel Weststratebefore adoption385c0a4
line 155why, one step back155 return {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 156–157why, one step back156 done: false,157 value: [r.value, value]fixed most testsMichel Weststratebefore adoption385c0a4
lines 158–162why, one step back158 }159 }160 } as any161 }162Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 163–164why, one step back163 [Symbol.iterator]() {164 // TODO: don't use symbol directlyfixed most testsMichel Weststratebefore adoption385c0a4
lines 165–166why, one step back165 return this.entries()166 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 167–170why, one step back167}168169export function proxyMap(target, parent) {170 if (target instanceof DraftMap) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 171why, one step back171 return target // TODO: or copy?added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 172–190why, one step back172 }173 return new DraftMap(target, parent)174}175176export function hasMapChanges(state) {177 const {base, draft} = state178179 if (base.size !== draft.size) return true180181 // IE11 supports only forEach iteration182 let hasChanges = false183 // TODO: optimize: break on first difference184 draft.forEach(function(value, key) {185 if (!hasChanges) {186 hasChanges = isDraftable(value) ? value.modified : value !== base.get(key)187 }188 })189 return hasChanges190}