Skip to content
Myr

set.ts

This file

Path

src/set.ts

167 lines · 3.9 KiB · TypeScript

Last changed by

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 4why, one step back
4import {assertUnrevoked} from "./es5"

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 7–11why, one step back
7
8// TODO: create own states9// TODO: clean up the maps and such from ES5 / Proxy states1011export interface SetState {

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 13–17why, one step back
13 parent: any // TODO: type
14 scope: ImmerScope15 modified: boolean16 finalizing: boolean17 finalized: boolean

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 20–21why, one step back
20 drafts: Map<any, any> // maps the original value to the draft value in the new set
21 revoke(): void

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 23–26why, one step back
23}
2425function prepareCopy(state: SetState) {26 if (!state.copy) {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 40–46why, one step back
40
41// TODO: fix types for drafts42export 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–147why, 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 if (target instanceof DraftSet) {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 148why, one step back
148 return target // TODO: or copy?

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 149–167why, one step back
149 }
150 return new DraftSet(target, parent)151}152153export function hasSetChanges(state) {154 const {base, draft} = state155156 if (base.size !== draft.size) return true157158 // IE11 supports only forEach iteration159 let hasChanges = false160 // TODO: optimize: break on first diff161 draft.forEach(function(value, key) {162 if (!hasChanges) {163 hasChanges = isDraftable(value) ? value.modified : !base.has(key)164 }165 })166 return hasChanges167}