Skip to content
Myr

set.ts

This file

Path

src/set.ts

141 lines · 3.3 KiB · TypeScript

Last changed by

Enums FTWMichel Weststratebefore adoptionfd76f1c

History of this file

Take it

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 1why, one step back
1import {DRAFT_STATE, latest, isDraftable, iteratorSymbol} 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"

Enums FTWMichel Weststratebefore adoptionfd76f1c

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 5why, one step back
5

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 6why, one step back
6export interface SetState extends ImmerBaseState {

Enums FTWMichel Weststratebefore adoptionfd76f1c

line 7why, one step back
7 type: ProxyType.Set

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 8why, one step back
8 finalizing: boolean

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 9–10why, one step back
9 copy: AnySet | undefined
10 base: AnySet

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 14–16why, one step back
14}
1516// Make sure DraftSet declarion doesn't die if Map is not avialable...

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Enums FTWMichel Weststratebefore adoptionfd76f1c

line 25why, one step back
25 type: ProxyType.Set,

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 26why, one step back
26 parent,

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 28–32why, one step back
28 modified: false,
29 finalized: false,30 finalizing: false,31 copy: undefined,32 base: target,

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 33why, one step back
33 draft: this,

fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 35–36why, one step back
35 revoke() {
36 // TODO: make sure this marks the Map as revoked, and assert everywhere

Processed bunch of todosMichel Weststratebefore adoption00425e1

lines 37–38why, one step back
37 },
38 isManual: false

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 39why, one step back
39 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 40–45why, one step back
40 }
4142 get size(): number {43 return latest(this[DRAFT_STATE]).size44 }45

fixed most testsMichel Weststratebefore adoption385c0a4

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

fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 52–55why, one step back
52 if (state.copy.has(value)) return true
53 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))54 return true55 return false

fixed most testsMichel Weststratebefore adoption385c0a4

lines 56–57why, one step back
56 }
57

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

fixed most testsMichel Weststratebefore adoption385c0a4

lines 60–62why, one step back
60 if (state.copy) {
61 state.copy.add(value)62 } else if (!state.base.has(value)) {

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 63why, one step back
63 prepareCopy(state)

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 64why, one step back
64 state.scope.immer.markChanged(state)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 65–70why, one step back
65 state.copy!.add(value)
66 }67 return this68 }6970 delete(value: V): boolean {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 73why, one step back
73 return false

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 74–77why, one step back
74 }
7576 prepareCopy(state)77 state.scope.immer.markChanged(state)

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 78–83why, one step back
78 return (
79 state.copy!.delete(value) ||80 (state.drafts.has(value)81 ? state.copy!.delete(state.drafts.get(value))82 : false)83 )

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 84–86why, one step back
84 }
8586 clear() {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 88–92why, one step back
88 prepareCopy(state)
89 state.scope.immer.markChanged(state)90 return state.copy!.clear()91 }92

fixed most testsMichel Weststratebefore adoption385c0a4

lines 93–96why, one step back
93 values(): IterableIterator<V> {
94 const state = this[DRAFT_STATE]95 prepareCopy(state)96 return state.copy!.values()

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 97–98why, one step back
97 }
98

fixed most testsMichel Weststratebefore adoption385c0a4

lines 99–103why, one step back
99 entries(): IterableIterator<[V, V]> {
100 const state = this[DRAFT_STATE]101 prepareCopy(state)102 return state.copy!.entries()103 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 104why, one step back
104

fixed most testsMichel Weststratebefore adoption385c0a4

lines 105–108why, one step back
105 keys(): IterableIterator<V> {
106 return this.values()107 }108

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 109why, one step back
109 [iteratorSymbol]() {

fixed most testsMichel Weststratebefore adoption385c0a4

lines 110–112why, one step back
110 return this.values()
111 }112

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

fixed most testsMichel Weststratebefore adoption385c0a4

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 118–122why, one step back
118 result = iterator.next()
119 }120 }121}122

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 125–140why, one step back
125}
126127function prepareCopy(state: SetState) {128 if (!state.copy) {129 // create drafts for all entries to preserve insertion order130 state.copy = new Set()131 state.base.forEach(value => {132 if (isDraftable(value)) {133 const draft = state.scope.immer.createProxy(value, state)134 state.drafts.set(value, draft)135 state.copy!.add(draft)136 } else {137 state.copy!.add(value)138 }139 })140 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 141why, one step back
141}