Skip to content
Myr

set.ts

This file

Path

src/set.ts

148 lines · 3.4 KiB · TypeScript

Last changed by

Removed unused stuffMichel Weststratebefore adoption74c0536

History of this file

Take it

Removed unused stuffMichel Weststratebefore adoption74c0536

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 5why, one step back
5import {AnySet, Drafted} 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"

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 12–16why, one step back
12 parent: any // TODO: type
13 scope: ImmerScope14 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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 19–20why, one step back
19 drafts: Map<any, any> // maps the original value to the draft value in the new set
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–25why, one step back
22}
2324function prepareCopy(state: SetState) {25 if (!state.copy) {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 26–32why, one step back
26 // create drafts for all entries to preserve insertion order
27 state.copy = new Set()28 state.base.forEach(value => {29 const draft = state.scope.immer.createProxy(value, state)30 state.copy!.add(draft)31 state.drafts.set(value, draft)32 })

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 33–36why, one step back
33 }
34}3536// Make sure DraftSet declarion doesn't die if Map is not avialable...

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 39–40why, one step back
39
40// TODO: fix types for drafts

Removed unused stuffMichel Weststratebefore adoption74c0536

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 42–46why, one step back
42export class DraftSet<K, V> extends SetBase implements Set<V> {
43 [DRAFT_STATE]: SetState44 constructor(target, parent) {45 super()46 this[DRAFT_STATE] = {

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 48–55why, one step back
48 parent,
49 scope: parent ? parent.scope : ImmerScope.current,50 modified: false,51 finalized: false,52 finalizing: false,53 copy: undefined,54 base: target,55 draft: this as any, // TODO: fix typing

fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 57–59why, one step back
57 revoke() {
58 // TODO: make sure this marks the Map as revoked, and assert everywhere59 }

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 60why, one step back
60 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 61–66why, one step back
61 }
6263 get size(): number {64 return latest(this[DRAFT_STATE]).size65 }66

fixed most testsMichel Weststratebefore adoption385c0a4

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

fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 73–76why, one step back
73 if (state.copy.has(value)) return true
74 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))75 return true76 return false

fixed most testsMichel Weststratebefore adoption385c0a4

lines 77–78why, one step back
77 }
78

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

fixed most testsMichel Weststratebefore adoption385c0a4

lines 81–83why, one step back
81 if (state.copy) {
82 state.copy.add(value)83 } else if (!state.base.has(value)) {

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 84–91why, one step back
84 prepareCopy(state)
85 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly86 state.copy!.add(value)87 }88 return this89 }9091 delete(value: V): boolean {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 94why, one step back
94 return false

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 95–98why, one step back
95 }
9697 prepareCopy(state)98 state.scope.immer.markChanged(state)

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 99–104why, one step back
99 return (
100 state.copy!.delete(value) ||101 (state.drafts.has(value)102 ? state.copy!.delete(state.drafts.get(value))103 : false)104 )

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 105–107why, one step back
105 }
106107 clear() {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 109–113why, one step back
109 prepareCopy(state)
110 state.scope.immer.markChanged(state)111 return state.copy!.clear()112 }113

fixed most testsMichel Weststratebefore adoption385c0a4

lines 114–117why, one step back
114 values(): IterableIterator<V> {
115 const state = this[DRAFT_STATE]116 prepareCopy(state)117 return state.copy!.values()

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 118–119why, one step back
118 }
119

fixed most testsMichel Weststratebefore adoption385c0a4

lines 120–124why, one step back
120 entries(): IterableIterator<[V, V]> {
121 const state = this[DRAFT_STATE]122 prepareCopy(state)123 return state.copy!.entries()124 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 125why, one step back
125

fixed most testsMichel Weststratebefore adoption385c0a4

lines 126–134why, one step back
126 keys(): IterableIterator<V> {
127 return this.values()128 }129130 // TODO: factor out symbol131 [Symbol.iterator]() {132 return this.values()133 }134

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 135–136why, one step back
135 forEach(cb, thisArg) {
136 const state = this[DRAFT_STATE]

fixed most testsMichel Weststratebefore adoption385c0a4

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 138–148why, one step back
138 let result = iterator.next()
139 while (!result.done) {140 cb.call(thisArg, result.value, result.value, state.draft)141 result = iterator.next()142 }143 }144}145146export function proxySet(target, parent) {147 return new DraftSet(target, parent)148}