Skip to content
Myr

set.ts

This file

Path

src/set.ts

165 lines · 3.8 KiB · TypeScript

Last changed by

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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:

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 4–5why, one step back
4import {assertUnrevoked, ES5Draft} from "./es5"
5import {ImmerScope} from "./scope"

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 {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 11–16why, one step back
11 parent: any // TODO: type
12 scope: ImmerScope13 modified: boolean14 finalizing: boolean15 finalized: boolean16 copy: Set<any> | undefined

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 17why, one step back
17 // assigned: Map<any, boolean> | undefined;

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 18–21why, one step back
18 base: Set<any>
19 drafts: Map<any, any> // maps the original value to the draft value in the new set20 revoke(): void21 draft: ES5Draft

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–53why, one step back
39
40// TODO: fix types for drafts41export class DraftSet<K, V> extends SetBase implements Set<V> {42 [DRAFT_STATE]: SetState43 constructor(target, parent) {44 super()45 this[DRAFT_STATE] = {46 parent,47 scope: parent ? parent.scope : ImmerScope.current,48 modified: false,49 finalized: false,50 finalizing: false,51 copy: undefined,52 base: target,53 draft: this as any, // TODO: fix typing

fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 58why, one step back
58 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 59–64why, one step back
59 }
6061 get size(): number {62 return latest(this[DRAFT_STATE]).size63 }64

fixed most testsMichel Weststratebefore adoption385c0a4

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

fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 71–74why, one step back
71 if (state.copy.has(value)) return true
72 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))73 return true74 return false

fixed most testsMichel Weststratebefore adoption385c0a4

lines 75–76why, one step back
75 }
76

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

fixed most testsMichel Weststratebefore adoption385c0a4

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 82–89why, one step back
82 prepareCopy(state)
83 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly84 state.copy!.add(value)85 }86 return this87 }8889 delete(value: V): boolean {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

line 92why, one step back
92 return false

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 93–96why, one step back
93 }
9495 prepareCopy(state)96 state.scope.immer.markChanged(state)

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 103–105why, one step back
103 }
104105 clear() {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

fixed most testsMichel Weststratebefore adoption385c0a4

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 116–117why, one step back
116 }
117

fixed most testsMichel Weststratebefore adoption385c0a4

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 123why, one step back
123

fixed most testsMichel Weststratebefore adoption385c0a4

lines 124–132why, one step back
124 keys(): IterableIterator<V> {
125 return this.values()126 }127128 // TODO: factor out symbol129 [Symbol.iterator]() {130 return this.values()131 }132

added set and iteror stuffMichel Weststratebefore adoption08f4682

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

fixed most testsMichel Weststratebefore adoption385c0a4

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 136–145why, one step back
136 let result = iterator.next()
137 while (!result.done) {138 cb.call(thisArg, result.value, result.value, state.draft)139 result = iterator.next()140 }141 }142}143144export function proxySet(target, parent) {145 if (target instanceof DraftSet) {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 147–165why, one step back
147 }
148 return new DraftSet(target, parent)149}150151export function hasSetChanges(state) {152 const {base, draft} = state153154 if (base.size !== draft.size) return true155156 // IE11 supports only forEach iteration157 let hasChanges = false158 // TODO: optimize: break on first diff159 draft.forEach(function(value, key) {160 if (!hasChanges) {161 hasChanges = isDraftable(value) ? value.modified : !base.has(key)162 }163 })164 return hasChanges165}