Skip to content
Myr

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 file

Take it

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 1why, one step back
1import {isDraftable, DRAFT_STATE, latest} from "./common"

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 2why, one step back
2

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 3why, one step back
3import {ImmerScope} from "./scope"

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 4why, one step back
4import {AnyMap, Drafted} from "./types"

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 5–9why, one step back
5
6// 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 back
10 type: "map"

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 11–15why, one step back
11 parent: any // TODO: type
12 scope: ImmerScope13 modified: boolean14 finalizing: boolean15 finalized: boolean

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 16why, one step back
16 copy: AnyMap | undefined

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 17why, one step back
17 assigned: Map<any, boolean> | undefined

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 18why, one step back
18 base: AnyMap

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 19why, one step back
19 revoke(): void

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 20why, one step back
20 draft: Drafted<AnyMap, MapState>

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 21–25why, one step back
21}
2223function prepareCopy(state: MapState) {24 if (!state.copy) {25 state.assigned = new Map()

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 26why, one step back
26 state.copy = new Map(state.base)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 27–30why, one step back
27 }
28}2930// Make sure DraftMap declarion doesn't die if Map is not avialable...

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 31–32why, one step back
31const MapBase: MapConstructor =
32 typeof Map !== "undefined" ? Map : (function FakeMap() {} as any)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 33–34why, one step back
33
34// TODO: fix types for drafts

Removed unused stuffMichel Weststratebefore adoption74c0536

line 35why, one step back
35// TODO: assert unrevoked

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 36–40why, one step back
36export 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 back
41 type: "map",

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 42–53why, one step back
42 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 back
54 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 55–65why, one step back
55 }
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 back
66 const state = this[DRAFT_STATE]

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 67–71why, one step back
67 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 back
72 state.assigned!.set(key, true)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 73–78why, one step back
73 }
74 return this75 }7677 delete(key: K): boolean {78 if (!this.has(key)) {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 79why, one step back
79 return false

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 80–81why, one step back
80 }
81

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 82why, one step back
82 const state = this[DRAFT_STATE]

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 83–90why, one step back
83 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 back
91 const state = this[DRAFT_STATE]

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 92–102why, one step back
92 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 back
103 const state = this[DRAFT_STATE]

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 104–108why, one step back
104 latest(state).forEach((_value, key, _map) => {
105 cb(this.get(key), key, this)106 })107 }108

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 109–110why, one step back
109 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 back
111 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 back
118 // despite what it looks, this creates a draft only once, see above condition

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 119–125why, one step back
119 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 back
126 return latest(this[DRAFT_STATE]).keys()

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 127–128why, one step back
127 }
128

fixed most testsMichel Weststratebefore adoption385c0a4

lines 129–134why, one step back
129 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 back
135 if (r.done) return r
136 const value = this.get(r.value)

fixed most testsMichel Weststratebefore adoption385c0a4

line 137why, one step back
137 return {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 138–139why, one step back
138 done: false,
139 value

fixed most testsMichel Weststratebefore adoption385c0a4

lines 140–150why, one step back
140 }
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 back
151 if (r.done) return r
152 const value = this.get(r.value)

fixed most testsMichel Weststratebefore adoption385c0a4

line 153why, one step back
153 return {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 154–155why, one step back
154 done: false,
155 value: [r.value, value]

fixed most testsMichel Weststratebefore adoption385c0a4

lines 156–160why, one step back
156 }
157 }158 } as any159 }160

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 161–162why, one step back
161 [Symbol.iterator]() {
162 // TODO: don't use symbol directly

fixed most testsMichel Weststratebefore adoption385c0a4

lines 163–164why, one step back
163 return this.entries()
164 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 165–169why, one step back
165}
166167export function proxyMap(target, parent) {168 return new DraftMap(target, parent)169}