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 fileTake it
Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 1why, one step back1import {DRAFT_STATE, latest, isDraftable, iteratorSymbol} from "./common"added set and iteror stuffMichel Weststratebefore adoption08f4682
line 2why, one step back2Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 3–5why, one step back3import {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 back6Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 7–13why, one step back7export 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 back14}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 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]: SetStateMerge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
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] = {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 25why, one step back25 type: ProxyType.Set,added set and iteror stuffMichel Weststratebefore adoption08f4682
line 26why, one step back26 parent,Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 27why, one step back27 scope: parent ? parent.scope : ImmerScope.current!,added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 28–31why, one step back28 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 back32 draft: this,fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
line 33why, one step back33 drafts: new Map(),Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 34–36why, one step back34 revoked: false,35 isManual: false36 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 37–42why, one step back37 }3839 get size(): number {40 return latest(this[DRAFT_STATE]).size41 }42fixed most testsMichel Weststratebefore adoption385c0a4
line 43why, one step back43 has(value: V): boolean {fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
line 44why, one step back44 const state = this[DRAFT_STATE]Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 45why, one step back45 assertUnrevoked(state)fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
lines 46–49why, one step back46 // bit of trickery here, to be able to recognize both the value, and the draft of its value47 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 back50 if (state.copy.has(value)) return true51 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))52 return true53 return falsefixed most testsMichel Weststratebefore adoption385c0a4
lines 54–55why, one step back54 }55added set and iteror stuffMichel Weststratebefore adoption08f4682
line 56why, one step back56 add(value: V): this {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 57–58why, one step back57 const state = this[DRAFT_STATE]58 assertUnrevoked(state)fixed most testsMichel Weststratebefore adoption385c0a4
lines 59–61why, one step back59 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 back62 prepareCopy(state)Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 63why, one step back63 state.scope.immer.markChanged(state)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 64–70why, one step back64 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 back71 return falseadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 72–73why, one step back72 }73Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 74–75why, one step back74 const state = this[DRAFT_STATE]75 assertUnrevoked(state)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 76–77why, one step back76 prepareCopy(state)77 state.scope.immer.markChanged(state)Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
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() {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 87–88why, one step back87 const state = this[DRAFT_STATE]88 assertUnrevoked(state)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 89–93why, one step back89 prepareCopy(state)90 state.scope.immer.markChanged(state)91 return state.copy!.clear()92 }93fixed most testsMichel Weststratebefore adoption385c0a4
lines 94–95why, one step back94 values(): IterableIterator<V> {95 const state = this[DRAFT_STATE]Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 96why, one step back96 assertUnrevoked(state)fixed most testsMichel Weststratebefore adoption385c0a4
lines 97–98why, one step back97 prepareCopy(state)98 return state.copy!.values()added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 99–100why, one step back99 }100fixed most testsMichel Weststratebefore adoption385c0a4
lines 101–102why, one step back101 entries(): IterableIterator<[V, V]> {102 const state = this[DRAFT_STATE]Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 103why, one step back103 assertUnrevoked(state)fixed most testsMichel Weststratebefore adoption385c0a4
lines 104–106why, one step back104 prepareCopy(state)105 return state.copy!.entries()106 }added set and iteror stuffMichel Weststratebefore adoption08f4682
line 107why, one step back107fixed most testsMichel Weststratebefore adoption385c0a4
lines 108–111why, one step back108 keys(): IterableIterator<V> {109 return this.values()110 }111Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 112why, one step back112 [iteratorSymbol]() {fixed most testsMichel Weststratebefore adoption385c0a4
lines 113–115why, one step back113 return this.values()114 }115Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 116why, one step back116 forEach(cb: (value: V, key: V, self: this) => void, thisArg?: any) {fixed most testsMichel Weststratebefore adoption385c0a4
line 117why, one step back117 const iterator = this.values()added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 118–119why, one step back118 let result = iterator.next()119 while (!result.done) {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 120why, one step back120 cb.call(thisArg, result.value, result.value, this)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 121–125why, one step back121 result = iterator.next()122 }123 }124}125Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 126why, one step back126export function proxySet(target: AnySet, parent?: ImmerState) {added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 127–129why, one step back127 return new DraftSet(target, parent)128}129Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 130–143why, one step back130function 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 back144}