map.ts
This file
Path
src/map.ts
169 lines · 3.8 KiB · TypeScript
Last changed by
Removed unused stuffMichel Weststratebefore adoption74c0536
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"Introduced proper state typesMichel Weststratebefore adoption3842f85
line 4why, one step back4import {AnyMap, Drafted} from "./types"added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 5–9why, one step back56// TODO: create own states7// TODO: clean up the maps and such from ES5 / Proxy states89export interface MapState {Introduced proper state typesMichel Weststratebefore adoption3842f85
line 10why, one step back10 type: "map"Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 11–15why, one step back11 parent: any // TODO: type12 scope: ImmerScope13 modified: boolean14 finalizing: boolean15 finalized: booleanIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 16why, one step back16 copy: AnyMap | undefinedSome initial cleanupMichel Weststratebefore adoptionc53a75f
line 17why, one step back17 assigned: Map<any, boolean> | undefinedIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 18why, one step back18 base: AnyMapSome initial cleanupMichel Weststratebefore adoptionc53a75f
line 19why, one step back19 revoke(): voidIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 20why, one step back20 draft: Drafted<AnyMap, MapState>added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 21–25why, one step back21}2223function prepareCopy(state: MapState) {24 if (!state.copy) {25 state.assigned = new Map()Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 26why, one step back26 state.copy = new Map(state.base)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 27–30why, one step back27 }28}2930// Make sure DraftMap declarion doesn't die if Map is not avialable...Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 31–32why, one step back31const MapBase: MapConstructor =32 typeof Map !== "undefined" ? Map : (function FakeMap() {} as any)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 33–34why, one step back3334// TODO: fix types for draftsRemoved unused stuffMichel Weststratebefore adoption74c0536
line 35why, one step back35// TODO: assert unrevokedadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 36–40why, one step back36export class DraftMap<K, V> extends MapBase implements Map<K, V> {37 [DRAFT_STATE]: MapState38 constructor(target, parent) {39 super()40 this[DRAFT_STATE] = {Introduced proper state typesMichel Weststratebefore adoption3842f85
line 41why, one step back41 type: "map",added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 42–53why, one step back42 parent,43 scope: parent ? parent.scope : ImmerScope.current,44 modified: false,45 finalized: false,46 finalizing: false,47 copy: undefined,48 assigned: undefined,49 base: target,50 draft: this as any, // TODO: fix typing51 revoke() {52 // TODO: make sure this marks the Map as revoked, and assert everywhere53 }Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 54why, one step back54 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 55–65why, one step back55 }5657 get size(): number {58 return latest(this[DRAFT_STATE]).size59 }6061 has(key: K): boolean {62 return latest(this[DRAFT_STATE]).has(key)63 }6465 set(key: K, value: V): this {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 66why, one step back66 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 67–71why, one step back67 if (latest(state).get(key) !== value) {68 prepareCopy(state)69 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly70 state.assigned!.set(key, true)71 state.copy!.set(key, value)Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 72why, one step back72 state.assigned!.set(key, true)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 73–78why, one step back73 }74 return this75 }7677 delete(key: K): boolean {78 if (!this.has(key)) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 79why, one step back79 return falseadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 80–81why, one step back80 }81Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 82why, one step back82 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 83–90why, one step back83 prepareCopy(state)84 state.scope.immer.markChanged(state)85 state.assigned!.set(key, false)86 state.copy!.delete(key)87 return true88 }8990 clear() {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 91why, one step back91 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 92–102why, one step back92 prepareCopy(state)93 state.scope.immer.markChanged(state)94 state.assigned = new Map()95 for (const key of latest(state).keys()) {96 state.assigned.set(key, false)97 }98 return state.copy!.clear()99 }100101 // @ts-ignore TODO:102 forEach(cb) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 103why, one step back103 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 104–108why, one step back104 latest(state).forEach((_value, key, _map) => {105 cb(this.get(key), key, this)106 })107 }108Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 109–110why, one step back109 get(key: K): V /* TODO: Draft<V> */ {110 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 111–117why, one step back111 const value = latest(state).get(key)112 if (state.finalizing || state.finalized || !isDraftable(value)) {113 return value114 }115 if (value !== state.base.get(key)) {116 return value // either already drafted or reassigned117 }fixed most testsMichel Weststratebefore adoption385c0a4
line 118why, one step back118 // despite what it looks, this creates a draft only once, see above conditionadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 119–125why, one step back119 const draft = state.scope.immer.createProxy(value, state)120 prepareCopy(state)121 state.copy!.set(key, draft)122 return draft123 }124125 keys() {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 126why, one step back126 return latest(this[DRAFT_STATE]).keys()added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 127–128why, one step back127 }128fixed most testsMichel Weststratebefore adoption385c0a4
lines 129–134why, one step back129 values() {130 const iterator = this.keys()131 return {132 [Symbol.iterator]: () => this.values(), // TODO: don't use symbol directly133 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 valuefixed most testsMichel Weststratebefore adoption385c0a4
lines 140–150why, one step back140 }141 }142 } as any143 }144145 entries() {146 const iterator = this.keys()147 return {148 [Symbol.iterator]: () => this.entries(), // TODO: don't use symbol directly149 next: () => {150 const r = iterator.next()Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 151–152why, one step back151 if (r.done) return r152 const value = this.get(r.value)fixed most testsMichel Weststratebefore adoption385c0a4
line 153why, one step back153 return {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 154–155why, one step back154 done: false,155 value: [r.value, value]fixed most testsMichel Weststratebefore adoption385c0a4
lines 156–160why, one step back156 }157 }158 } as any159 }160Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 161–162why, one step back161 [Symbol.iterator]() {162 // TODO: don't use symbol directlyfixed most testsMichel Weststratebefore adoption385c0a4
lines 163–164why, one step back163 return this.entries()164 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 165–169why, one step back165}166167export function proxyMap(target, parent) {168 return new DraftMap(target, parent)169}