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 fileTake it
Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 1why, one step back1import {isDraftable, DRAFT_STATE, latest} from "./common"added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 2–3why, one step back23// TODO: kill:Introduced proper state typesMichel Weststratebefore adoption3842f85
line 4why, one step back4import {assertUnrevoked} from "./es5"Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 5why, one step back5import {ImmerScope} from "./scope"Introduced proper state typesMichel Weststratebefore adoption3842f85
line 6why, one step back6import {AnySet, Drafted} from "./types"added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 7–11why, one step back78// 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 back12 type: "set"Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 13–17why, one step back13 parent: any // TODO: type14 scope: ImmerScope15 modified: boolean16 finalizing: boolean17 finalized: booleanIntroduced proper state typesMichel Weststratebefore adoption3842f85
lines 18–19why, one step back18 copy: AnySet | undefined19 base: AnySetSome initial cleanupMichel Weststratebefore adoptionc53a75f
lines 20–21why, one step back20 drafts: Map<any, any> // maps the original value to the draft value in the new set21 revoke(): voidIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 22why, one step back22 draft: Drafted<AnySet, SetState>added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 23–26why, one step back23}2425function prepareCopy(state: SetState) {26 if (!state.copy) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 27–33why, one step back27 // create drafts for all entries to preserve insertion order28 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 back34 }35}3637// Make sure DraftSet declarion doesn't die if Map is not avialable...Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 38–39why, one step back38const SetBase: SetConstructor =39 typeof Set !== "undefined" ? Set : (function FakeSet() {} as any)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 40–46why, one step back4041// 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 back47 type: "set",added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 48–55why, one step back48 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 typingfixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
line 56why, one step back56 drafts: new Map(),added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 57–59why, one step back57 revoke() {58 // TODO: make sure this marks the Map as revoked, and assert everywhere59 }Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 60why, one step back60 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 61–66why, one step back61 }6263 get size(): number {64 return latest(this[DRAFT_STATE]).size65 }66fixed most testsMichel Weststratebefore adoption385c0a4
line 67why, one step back67 has(value: V): boolean {fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
lines 68–72why, one step back68 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 back73 if (state.copy.has(value)) return true74 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))75 return true76 return falsefixed most testsMichel Weststratebefore adoption385c0a4
lines 77–78why, one step back77 }78added set and iteror stuffMichel Weststratebefore adoption08f4682
line 79why, one step back79 add(value: V): this {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 80why, one step back80 const state = this[DRAFT_STATE]fixed most testsMichel Weststratebefore adoption385c0a4
lines 81–83why, one step back81 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 back84 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 back92 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
line 93why, one step back93 if (!this.has(value)) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 94why, one step back94 return falseadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 95–98why, one step back95 }9697 prepareCopy(state)98 state.scope.immer.markChanged(state)Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 99–104why, one step back99 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 back105 }106107 clear() {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 108why, one step back108 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 109–113why, one step back109 prepareCopy(state)110 state.scope.immer.markChanged(state)111 return state.copy!.clear()112 }113fixed most testsMichel Weststratebefore adoption385c0a4
lines 114–117why, one step back114 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 back118 }119fixed most testsMichel Weststratebefore adoption385c0a4
lines 120–124why, one step back120 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 back125fixed most testsMichel Weststratebefore adoption385c0a4
lines 126–134why, one step back126 keys(): IterableIterator<V> {127 return this.values()128 }129130 // TODO: factor out symbol131 [Symbol.iterator]() {132 return this.values()133 }134added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 135–136why, one step back135 forEach(cb, thisArg) {136 const state = this[DRAFT_STATE]fixed most testsMichel Weststratebefore adoption385c0a4
line 137why, one step back137 const iterator = this.values()added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 138–147why, one step back138 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 back148 return target // TODO: or copy?added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 149–167why, one step back149 }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}