Skip to content
Myr

set.ts

This file

Path

src/set.ts

144 lines · 3.4 KiB · TypeScript

Last changed by

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

History of this file

Take it

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 3–5why, one step back
3import {ImmerScope} from "./scope"
4import {AnySet, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"5import {assertUnrevoked} from "./es5"

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 6why, one step back
6

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 7–13why, one step back
7export interface SetState extends ImmerBaseState {
8 type: ProxyType.Set9 copy: AnySet | undefined10 base: AnySet11 drafts: Map<any, Drafted> // maps the original value to the draft value in the new set12 revoked: boolean13 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...

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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] = {

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 26why, one step back
26 parent,

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 34–36why, one step back
34 revoked: false,
35 isManual: false36 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 37–42why, one step back
37 }
3839 get size(): number {40 return latest(this[DRAFT_STATE]).size41 }42

fixed most testsMichel Weststratebefore adoption385c0a4

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

fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 45why, one step back
45 assertUnrevoked(state)

fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301

lines 46–49why, one step back
46 // bit of trickery here, to be able to recognize both the value, and the draft of its value
47 if (!state.copy) {48 return state.base.has(value)49 }

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 50–53why, one step back
50 if (state.copy.has(value)) return true
51 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))52 return true53 return false

fixed most testsMichel Weststratebefore adoption385c0a4

lines 54–55why, one step back
54 }
55

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 57–58why, one step back
57 const state = this[DRAFT_STATE]
58 assertUnrevoked(state)

fixed most testsMichel Weststratebefore adoption385c0a4

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 64–70why, one step back
64 state.copy!.add(value)
65 }66 return this67 }6869 delete(value: V): boolean {70 if (!this.has(value)) {

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 71why, one step back
71 return false

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 72–73why, one step back
72 }
73

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 74–75why, one step back
74 const state = this[DRAFT_STATE]
75 assertUnrevoked(state)

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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() {

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 87–88why, one step back
87 const state = this[DRAFT_STATE]
88 assertUnrevoked(state)

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

fixed most testsMichel Weststratebefore adoption385c0a4

lines 94–95why, one step back
94 values(): IterableIterator<V> {
95 const state = this[DRAFT_STATE]

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 96why, one step back
96 assertUnrevoked(state)

fixed most testsMichel Weststratebefore adoption385c0a4

lines 97–98why, one step back
97 prepareCopy(state)
98 return state.copy!.values()

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 99–100why, one step back
99 }
100

fixed most testsMichel Weststratebefore adoption385c0a4

lines 101–102why, one step back
101 entries(): IterableIterator<[V, V]> {
102 const state = this[DRAFT_STATE]

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 103why, one step back
103 assertUnrevoked(state)

fixed most testsMichel Weststratebefore adoption385c0a4

lines 104–106why, one step back
104 prepareCopy(state)
105 return state.copy!.entries()106 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 107why, one step back
107

fixed most testsMichel Weststratebefore adoption385c0a4

lines 108–111why, one step back
108 keys(): IterableIterator<V> {
109 return this.values()110 }111

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

fixed most testsMichel Weststratebefore adoption385c0a4

lines 113–115why, one step back
113 return this.values()
114 }115

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

fixed most testsMichel Weststratebefore adoption385c0a4

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 121–125why, one step back
121 result = iterator.next()
122 }123 }124}125

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 127–129why, one step back
127 return new DraftSet(target, parent)
128}129

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 144why, one step back
144}