Skip to content
Myr

set.ts

This file

Path

src/set.ts

152 lines · 3.6 KiB · TypeScript

Last changed by

TS type improvementsMichel Weststratebefore adoptiondd1808e

History of this file

Take it

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 2–3why, one step back
2
3// TODO: kill:

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 5why, one step back
5import {AnySet, Drafted, ImmerState} from "./types"

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 6–10why, one step back
6
7// TODO: create own states8// TODO: clean up the maps and such from ES5 / Proxy states910export interface SetState {

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 11why, one step back
11 type: "set"

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 12why, one step back
12 parent?: ImmerState

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 13–16why, one step back
13 scope: ImmerScope
14 modified: boolean15 finalizing: boolean16 finalized: boolean

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 17–18why, one step back
17 copy: AnySet | undefined
18 base: AnySet

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 19why, one step back
19 drafts: Map<any, Drafted> // maps the original value to the draft value in the new set

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 21why, one step back
21 draft: Drafted<AnySet, SetState>

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 22–24why, one step back
22}
2324// Make sure DraftSet declarion doesn't die if Map is not avialable...

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 25–26why, one step back
25const SetBase: SetConstructor =
26 typeof Set !== "undefined" ? Set : (function FakeSet() {} as any)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 27–28why, one step back
27
28// TODO: fix types for drafts

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 29why, one step back
29// TODO: assert unrevoked, use freeze for that

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 30–31why, one step back
30export class DraftSet<K, V> extends SetBase implements Set<V> {
31 [DRAFT_STATE]: SetState

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 32why, one step back
32 constructor(target: AnySet, parent?: ImmerState) {

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 33–34why, one step back
33 super()
34 this[DRAFT_STATE] = {

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 35why, one step back
35 type: "set",

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 36why, one step back
36 parent,

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 37why, one step back
37 scope: parent ? parent.scope : ImmerScope.current!,

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 38–43why, one step back
38 modified: false,
39 finalized: false,40 finalizing: false,41 copy: undefined,42 base: target,43 draft: this as any, // TODO: fix typing

fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301

line 44why, one step back
44 drafts: new Map(),

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 45–47why, one step back
45 revoke() {
46 // TODO: make sure this marks the Map as revoked, and assert everywhere47 }

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 48why, one step back
48 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 49–54why, one step back
49 }
5051 get size(): number {52 return latest(this[DRAFT_STATE]).size53 }54

fixed most testsMichel Weststratebefore adoption385c0a4

line 55why, one step back
55 has(value: V): boolean {

fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301

lines 56–60why, one step back
56 const state = this[DRAFT_STATE]
57 // bit of trickery here, to be able to recognize both the value, and the draft of its value58 if (!state.copy) {59 return state.base.has(value)60 }

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 61–64why, one step back
61 if (state.copy.has(value)) return true
62 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))63 return true64 return false

fixed most testsMichel Weststratebefore adoption385c0a4

lines 65–66why, one step back
65 }
66

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 67why, one step back
67 add(value: V): this {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

fixed most testsMichel Weststratebefore adoption385c0a4

lines 69–71why, one step back
69 if (state.copy) {
70 state.copy.add(value)71 } else if (!state.base.has(value)) {

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 72–79why, one step back
72 prepareCopy(state)
73 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly74 state.copy!.add(value)75 }76 return this77 }7879 delete(value: V): boolean {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 81why, one step back
81 if (!this.has(value)) {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 82why, one step back
82 return false

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 83–86why, one step back
83 }
8485 prepareCopy(state)86 state.scope.immer.markChanged(state)

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 87–92why, one step back
87 return (
88 state.copy!.delete(value) ||89 (state.drafts.has(value)90 ? state.copy!.delete(state.drafts.get(value))91 : false)92 )

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 93–95why, one step back
93 }
9495 clear() {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 97–101why, one step back
97 prepareCopy(state)
98 state.scope.immer.markChanged(state)99 return state.copy!.clear()100 }101

fixed most testsMichel Weststratebefore adoption385c0a4

lines 102–105why, one step back
102 values(): IterableIterator<V> {
103 const state = this[DRAFT_STATE]104 prepareCopy(state)105 return state.copy!.values()

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 106–107why, one step back
106 }
107

fixed most testsMichel Weststratebefore adoption385c0a4

lines 108–112why, one step back
108 entries(): IterableIterator<[V, V]> {
109 const state = this[DRAFT_STATE]110 prepareCopy(state)111 return state.copy!.entries()112 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 113why, one step back
113

fixed most testsMichel Weststratebefore adoption385c0a4

lines 114–122why, one step back
114 keys(): IterableIterator<V> {
115 return this.values()116 }117118 // TODO: factor out symbol119 [Symbol.iterator]() {120 return this.values()121 }122

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 123why, one step back
123 forEach(cb: (value: V, key: V, self: this) => void, thisArg?: any) {

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

fixed most testsMichel Weststratebefore adoption385c0a4

line 125why, one step back
125 const iterator = this.values()

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 126–127why, one step back
126 let result = iterator.next()
127 while (!result.done) {

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 128why, one step back
128 cb.call(thisArg, result.value, result.value, this)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 129–133why, one step back
129 result = iterator.next()
130 }131 }132}133

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 134why, one step back
134export function proxySet(target: AnySet, parent?: ImmerState) {

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 135why, one step back
135 return new DraftSet(target, parent)

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 136–151why, one step back
136}
137138function prepareCopy(state: SetState) {139 if (!state.copy) {140 // create drafts for all entries to preserve insertion order141 state.copy = new Set()142 state.base.forEach(value => {143 if (isDraftable(value)) {144 const draft = state.scope.immer.createProxy(value, state)145 state.drafts.set(value, draft)146 state.copy!.add(draft)147 } else {148 state.copy!.add(value)149 }150 })151 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 152why, one step back
152}