set.ts
This file
Path
src/set.ts
141 lines · 3.3 KiB · TypeScript
Last changed by
Enums FTWMichel Weststratebefore adoptionfd76f1c
History of this fileTake it
Processed bunch of todosMichel Weststratebefore adoption00425e1
line 1why, one step back1import {DRAFT_STATE, latest, isDraftable, iteratorSymbol} from "./common"added set and iteror stuffMichel Weststratebefore adoption08f4682
line 2why, one step back2Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 3why, one step back3import {ImmerScope} from "./scope"Enums FTWMichel Weststratebefore adoptionfd76f1c
line 4why, one step back4import {AnySet, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"added set and iteror stuffMichel Weststratebefore adoption08f4682
line 5why, one step back5Processed bunch of todosMichel Weststratebefore adoption00425e1
line 6why, one step back6export interface SetState extends ImmerBaseState {Enums FTWMichel Weststratebefore adoptionfd76f1c
line 7why, one step back7 type: ProxyType.SetSome initial cleanupMichel Weststratebefore adoptionc53a75f
line 8why, one step back8 finalizing: booleanIntroduced proper state typesMichel Weststratebefore adoption3842f85
lines 9–10why, one step back9 copy: AnySet | undefined10 base: AnySetTS type improvementsMichel Weststratebefore adoptiondd1808e
line 11why, one step back11 drafts: Map<any, Drafted> // maps the original value to the draft value in the new setSome initial cleanupMichel Weststratebefore adoptionc53a75f
line 12why, one step back12 revoke(): voidIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 13why, one step back13 draft: Drafted<AnySet, SetState>added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 14–16why, one step back14}1516// Make sure DraftSet declarion doesn't die if Map is not avialable...Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 17–18why, one step back17const SetBase: SetConstructor =18 typeof Set !== "undefined" ? Set : (function FakeSet() {} as any)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 19–21why, one step back1920export class DraftSet<K, V> extends SetBase implements Set<V> {21 [DRAFT_STATE]: SetStateTS type improvementsMichel Weststratebefore adoptiondd1808e
line 22why, one step back22 constructor(target: AnySet, parent?: ImmerState) {added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 23–24why, one step back23 super()24 this[DRAFT_STATE] = {Enums FTWMichel Weststratebefore adoptionfd76f1c
line 25why, one step back25 type: ProxyType.Set,added set and iteror stuffMichel Weststratebefore adoption08f4682
line 26why, one step back26 parent,TS type improvementsMichel Weststratebefore adoptiondd1808e
line 27why, one step back27 scope: parent ? parent.scope : ImmerScope.current!,added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 28–32why, one step back28 modified: false,29 finalized: false,30 finalizing: false,31 copy: undefined,32 base: target,Processed bunch of todosMichel Weststratebefore adoption00425e1
line 33why, one step back33 draft: this,fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
line 34why, one step back34 drafts: new Map(),added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 35–36why, one step back35 revoke() {36 // TODO: make sure this marks the Map as revoked, and assert everywhereProcessed bunch of todosMichel Weststratebefore adoption00425e1
lines 37–38why, one step back37 },38 isManual: falseSome initial cleanupMichel Weststratebefore adoptionc53a75f
line 39why, one step back39 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 40–45why, one step back40 }4142 get size(): number {43 return latest(this[DRAFT_STATE]).size44 }45fixed most testsMichel Weststratebefore adoption385c0a4
line 46why, one step back46 has(value: V): boolean {fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
lines 47–51why, one step back47 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 back52 if (state.copy.has(value)) return true53 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))54 return true55 return falsefixed most testsMichel Weststratebefore adoption385c0a4
lines 56–57why, one step back56 }57added set and iteror stuffMichel Weststratebefore adoption08f4682
line 58why, one step back58 add(value: V): this {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 59why, one step back59 const state = this[DRAFT_STATE]fixed most testsMichel Weststratebefore adoption385c0a4
lines 60–62why, one step back60 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 back63 prepareCopy(state)Processed bunch of todosMichel Weststratebefore adoption00425e1
line 64why, one step back64 state.scope.immer.markChanged(state)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 65–70why, one step back65 state.copy!.add(value)66 }67 return this68 }6970 delete(value: V): boolean {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 71why, one step back71 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
line 72why, one step back72 if (!this.has(value)) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 73why, one step back73 return falseadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 74–77why, one step back74 }7576 prepareCopy(state)77 state.scope.immer.markChanged(state)Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 78–83why, one step back78 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 back84 }8586 clear() {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 87why, one step back87 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 88–92why, one step back88 prepareCopy(state)89 state.scope.immer.markChanged(state)90 return state.copy!.clear()91 }92fixed most testsMichel Weststratebefore adoption385c0a4
lines 93–96why, one step back93 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 back97 }98fixed most testsMichel Weststratebefore adoption385c0a4
lines 99–103why, one step back99 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 back104fixed most testsMichel Weststratebefore adoption385c0a4
lines 105–108why, one step back105 keys(): IterableIterator<V> {106 return this.values()107 }108Processed bunch of todosMichel Weststratebefore adoption00425e1
line 109why, one step back109 [iteratorSymbol]() {fixed most testsMichel Weststratebefore adoption385c0a4
lines 110–112why, one step back110 return this.values()111 }112TS type improvementsMichel Weststratebefore adoptiondd1808e
line 113why, one step back113 forEach(cb: (value: V, key: V, self: this) => void, thisArg?: any) {fixed most testsMichel Weststratebefore adoption385c0a4
line 114why, one step back114 const iterator = this.values()added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 115–116why, one step back115 let result = iterator.next()116 while (!result.done) {TS type improvementsMichel Weststratebefore adoptiondd1808e
line 117why, one step back117 cb.call(thisArg, result.value, result.value, this)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 118–122why, one step back118 result = iterator.next()119 }120 }121}122TS type improvementsMichel Weststratebefore adoptiondd1808e
line 123why, one step back123export function proxySet(target: AnySet, parent?: ImmerState) {added set and iteror stuffMichel Weststratebefore adoption08f4682
line 124why, one step back124 return new DraftSet(target, parent)TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 125–140why, one step back125}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 back141}