map.ts
This file
Path
src/map.ts
187 lines · 4.2 KiB · TypeScript
Last changed by
Some initial cleanupMichel Weststratebefore adoptionc53a75f
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:Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 4–6why, one step back4import {assertUnrevoked, ES5Draft} from "./es5"5import {ImmerScope} from "./scope"6import {Immer} from "./immer"added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 7–11why, one step back78// TODO: create own states9// TODO: clean up the maps and such from ES5 / Proxy states1011export interface MapState {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 12–21why, one step back12 parent: any // TODO: type13 scope: ImmerScope14 modified: boolean15 finalizing: boolean16 finalized: boolean17 copy: Map<any, any> | undefined18 assigned: Map<any, boolean> | undefined19 base: Map<any, any>20 revoke(): void21 draft: ES5Draftadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 22–26why, one step back22}2324function prepareCopy(state: MapState) {25 if (!state.copy) {26 state.assigned = new Map()Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 27why, one step back27 state.copy = new Map(state.base)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 28–31why, one step back28 }29}3031// Make sure DraftMap declarion doesn't die if Map is not avialable...Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 32–33why, one step back32const MapBase: MapConstructor =33 typeof Map !== "undefined" ? Map : (function FakeMap() {} as any)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 34–52why, one step back3435// TODO: fix types for drafts36export class DraftMap<K, V> extends MapBase implements Map<K, V> {37 [DRAFT_STATE]: MapState38 constructor(target, parent) {39 super()40 this[DRAFT_STATE] = {41 parent,42 scope: parent ? parent.scope : ImmerScope.current,43 modified: false,44 finalized: false,45 finalizing: false,46 copy: undefined,47 assigned: undefined,48 base: target,49 draft: this as any, // TODO: fix typing50 revoke() {51 // TODO: make sure this marks the Map as revoked, and assert everywhere52 }Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 53why, one step back53 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 54–64why, one step back54 }5556 get size(): number {57 return latest(this[DRAFT_STATE]).size58 }5960 has(key: K): boolean {61 return latest(this[DRAFT_STATE]).has(key)62 }6364 set(key: K, value: V): this {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 65why, one step back65 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 66–70why, one step back66 if (latest(state).get(key) !== value) {67 prepareCopy(state)68 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly69 state.assigned!.set(key, true)70 state.copy!.set(key, value)Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 71why, one step back71 state.assigned!.set(key, true)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 72–77why, one step back72 }73 return this74 }7576 delete(key: K): boolean {77 if (!this.has(key)) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 78why, one step back78 return falseadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 79–80why, one step back79 }80Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 81why, one step back81 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 82–89why, one step back82 prepareCopy(state)83 state.scope.immer.markChanged(state)84 state.assigned!.set(key, false)85 state.copy!.delete(key)86 return true87 }8889 clear() {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 90why, one step back90 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 91–101why, one step back91 prepareCopy(state)92 state.scope.immer.markChanged(state)93 state.assigned = new Map()94 for (const key of latest(state).keys()) {95 state.assigned.set(key, false)96 }97 return state.copy!.clear()98 }99100 // @ts-ignore TODO:101 forEach(cb) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 102why, one step back102 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 103–107why, one step back103 latest(state).forEach((_value, key, _map) => {104 cb(this.get(key), key, this)105 })106 }107Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 108–109why, one step back108 get(key: K): V /* TODO: Draft<V> */ {109 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 110–116why, one step back110 const value = latest(state).get(key)111 if (state.finalizing || state.finalized || !isDraftable(value)) {112 return value113 }114 if (value !== state.base.get(key)) {115 return value // either already drafted or reassigned116 }fixed most testsMichel Weststratebefore adoption385c0a4
line 117why, one step back117 // despite what it looks, this creates a draft only once, see above conditionadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 118–124why, one step back118 const draft = state.scope.immer.createProxy(value, state)119 prepareCopy(state)120 state.copy!.set(key, draft)121 return draft122 }123124 keys() {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 125why, one step back125 return latest(this[DRAFT_STATE]).keys()added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 126–127why, one step back126 }127fixed most testsMichel Weststratebefore adoption385c0a4
lines 128–133why, one step back128 values() {129 const iterator = this.keys()130 return {131 [Symbol.iterator]: () => this.values(), // TODO: don't use symbol directly132 next: () => {133 const r = iterator.next()Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 134–135why, one step back134 if (r.done) return r135 const value = this.get(r.value)fixed most testsMichel Weststratebefore adoption385c0a4
line 136why, one step back136 return {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 137–138why, one step back137 done: false,138 valuefixed most testsMichel Weststratebefore adoption385c0a4
lines 139–149why, one step back139 }140 }141 } as any142 }143144 entries() {145 const iterator = this.keys()146 return {147 [Symbol.iterator]: () => this.entries(), // TODO: don't use symbol directly148 next: () => {149 const r = iterator.next()Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 150–151why, one step back150 if (r.done) return r151 const value = this.get(r.value)fixed most testsMichel Weststratebefore adoption385c0a4
line 152why, one step back152 return {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 153–154why, one step back153 done: false,154 value: [r.value, value]fixed most testsMichel Weststratebefore adoption385c0a4
lines 155–159why, one step back155 }156 }157 } as any158 }159Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 160–161why, one step back160 [Symbol.iterator]() {161 // TODO: don't use symbol directlyfixed most testsMichel Weststratebefore adoption385c0a4
lines 162–163why, one step back162 return this.entries()163 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 164–167why, one step back164}165166export function proxyMap(target, parent) {167 if (target instanceof DraftMap) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 168why, one step back168 return target // TODO: or copy?added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 169–187why, one step back169 }170 return new DraftMap(target, parent)171}172173export function hasMapChanges(state) {174 const {base, draft} = state175176 if (base.size !== draft.size) return true177178 // IE11 supports only forEach iteration179 let hasChanges = false180 // TODO: optimize: break on first difference181 draft.forEach(function(value, key) {182 if (!hasChanges) {183 hasChanges = isDraftable(value) ? value.modified : value !== base.get(key)184 }185 })186 return hasChanges187}