set.ts
This file
Path
src/set.ts
152 lines · 3.6 KiB · TypeScript
Last changed by
TS type improvementsMichel Weststratebefore adoptiondd1808e
History of this fileTake it
TS type improvementsMichel Weststratebefore adoptiondd1808e
line 1why, one step back1import {DRAFT_STATE, latest, isDraftable} from "./common"added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 2–3why, one step back23// TODO: kill:Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 4why, one step back4import {ImmerScope} from "./scope"TS type improvementsMichel Weststratebefore adoptiondd1808e
line 5why, one step back5import {AnySet, Drafted, ImmerState} from "./types"added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 6–10why, one step back67// 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 back11 type: "set"TS type improvementsMichel Weststratebefore adoptiondd1808e
line 12why, one step back12 parent?: ImmerStateSome initial cleanupMichel Weststratebefore adoptionc53a75f
lines 13–16why, one step back13 scope: ImmerScope14 modified: boolean15 finalizing: boolean16 finalized: booleanIntroduced proper state typesMichel Weststratebefore adoption3842f85
lines 17–18why, one step back17 copy: AnySet | undefined18 base: AnySetTS type improvementsMichel Weststratebefore adoptiondd1808e
line 19why, one step back19 drafts: Map<any, Drafted> // maps the original value to the draft value in the new setSome initial cleanupMichel Weststratebefore adoptionc53a75f
line 20why, one step back20 revoke(): voidIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 21why, one step back21 draft: Drafted<AnySet, SetState>added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 22–24why, one step back22}2324// Make sure DraftSet declarion doesn't die if Map is not avialable...Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 25–26why, one step back25const SetBase: SetConstructor =26 typeof Set !== "undefined" ? Set : (function FakeSet() {} as any)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 27–28why, one step back2728// TODO: fix types for draftsFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 29why, one step back29// TODO: assert unrevoked, use freeze for thatadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 30–31why, one step back30export class DraftSet<K, V> extends SetBase implements Set<V> {31 [DRAFT_STATE]: SetStateTS type improvementsMichel Weststratebefore adoptiondd1808e
line 32why, one step back32 constructor(target: AnySet, parent?: ImmerState) {added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 33–34why, one step back33 super()34 this[DRAFT_STATE] = {Introduced proper state typesMichel Weststratebefore adoption3842f85
line 35why, one step back35 type: "set",added set and iteror stuffMichel Weststratebefore adoption08f4682
line 36why, one step back36 parent,TS type improvementsMichel Weststratebefore adoptiondd1808e
line 37why, one step back37 scope: parent ? parent.scope : ImmerScope.current!,added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 38–43why, one step back38 modified: false,39 finalized: false,40 finalizing: false,41 copy: undefined,42 base: target,43 draft: this as any, // TODO: fix typingfixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
line 44why, one step back44 drafts: new Map(),added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 45–47why, one step back45 revoke() {46 // TODO: make sure this marks the Map as revoked, and assert everywhere47 }Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 48why, one step back48 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 49–54why, one step back49 }5051 get size(): number {52 return latest(this[DRAFT_STATE]).size53 }54fixed most testsMichel Weststratebefore adoption385c0a4
line 55why, one step back55 has(value: V): boolean {fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
lines 56–60why, one step back56 const state = this[DRAFT_STATE]57 // bit of trickery here, to be able to recognize both the value, and the draft of its value58 if (!state.copy) {59 return state.base.has(value)60 }Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 61–64why, one step back61 if (state.copy.has(value)) return true62 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))63 return true64 return falsefixed most testsMichel Weststratebefore adoption385c0a4
lines 65–66why, one step back65 }66added set and iteror stuffMichel Weststratebefore adoption08f4682
line 67why, one step back67 add(value: V): this {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 68why, one step back68 const state = this[DRAFT_STATE]fixed most testsMichel Weststratebefore adoption385c0a4
lines 69–71why, one step back69 if (state.copy) {70 state.copy.add(value)71 } else if (!state.base.has(value)) {added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 72–79why, one step back72 prepareCopy(state)73 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly74 state.copy!.add(value)75 }76 return this77 }7879 delete(value: V): boolean {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 80why, one step back80 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
line 81why, one step back81 if (!this.has(value)) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 82why, one step back82 return falseadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 83–86why, one step back83 }8485 prepareCopy(state)86 state.scope.immer.markChanged(state)Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 87–92why, one step back87 return (88 state.copy!.delete(value) ||89 (state.drafts.has(value)90 ? state.copy!.delete(state.drafts.get(value))91 : false)92 )added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 93–95why, one step back93 }9495 clear() {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 96why, one step back96 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 97–101why, one step back97 prepareCopy(state)98 state.scope.immer.markChanged(state)99 return state.copy!.clear()100 }101fixed most testsMichel Weststratebefore adoption385c0a4
lines 102–105why, one step back102 values(): IterableIterator<V> {103 const state = this[DRAFT_STATE]104 prepareCopy(state)105 return state.copy!.values()added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 106–107why, one step back106 }107fixed most testsMichel Weststratebefore adoption385c0a4
lines 108–112why, one step back108 entries(): IterableIterator<[V, V]> {109 const state = this[DRAFT_STATE]110 prepareCopy(state)111 return state.copy!.entries()112 }added set and iteror stuffMichel Weststratebefore adoption08f4682
line 113why, one step back113fixed most testsMichel Weststratebefore adoption385c0a4
lines 114–122why, one step back114 keys(): IterableIterator<V> {115 return this.values()116 }117118 // TODO: factor out symbol119 [Symbol.iterator]() {120 return this.values()121 }122TS type improvementsMichel Weststratebefore adoptiondd1808e
line 123why, one step back123 forEach(cb: (value: V, key: V, self: this) => void, thisArg?: any) {added set and iteror stuffMichel Weststratebefore adoption08f4682
line 124why, one step back124 const state = this[DRAFT_STATE]fixed most testsMichel Weststratebefore adoption385c0a4
line 125why, one step back125 const iterator = this.values()added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 126–127why, one step back126 let result = iterator.next()127 while (!result.done) {TS type improvementsMichel Weststratebefore adoptiondd1808e
line 128why, one step back128 cb.call(thisArg, result.value, result.value, this)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 129–133why, one step back129 result = iterator.next()130 }131 }132}133TS type improvementsMichel Weststratebefore adoptiondd1808e
line 134why, one step back134export function proxySet(target: AnySet, parent?: ImmerState) {added set and iteror stuffMichel Weststratebefore adoption08f4682
line 135why, one step back135 return new DraftSet(target, parent)TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 136–151why, one step back136}137138function prepareCopy(state: SetState) {139 if (!state.copy) {140 // create drafts for all entries to preserve insertion order141 state.copy = new Set()142 state.base.forEach(value => {143 if (isDraftable(value)) {144 const draft = state.scope.immer.createProxy(value, state)145 state.drafts.set(value, draft)146 state.copy!.add(draft)147 } else {148 state.copy!.add(value)149 }150 })151 }added set and iteror stuffMichel Weststratebefore adoption08f4682
line 152why, one step back152}