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 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:Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 4–5why, one step back4import {assertUnrevoked, ES5Draft} from "./es5"5import {ImmerScope} from "./scope"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 {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 11–16why, one step back11 parent: any // TODO: type12 scope: ImmerScope13 modified: boolean14 finalizing: boolean15 finalized: boolean16 copy: Set<any> | undefinedadded set and iteror stuffMichel Weststratebefore adoption08f4682
line 17why, one step back17 // assigned: Map<any, boolean> | undefined;Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 18–21why, one step back18 base: Set<any>19 drafts: Map<any, any> // maps the original value to the draft value in the new set20 revoke(): void21 draft: ES5Draftadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 22–25why, one step back22}2324function prepareCopy(state: SetState) {25 if (!state.copy) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 26–32why, one step back26 // create drafts for all entries to preserve insertion order27 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 back33 }34}3536// Make sure DraftSet declarion doesn't die if Map is not avialable...Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 37–38why, one step back37const SetBase: SetConstructor =38 typeof Set !== "undefined" ? Set : (function FakeSet() {} as any)added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 39–53why, one step back3940// 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 typingfixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
line 54why, one step back54 drafts: new Map(),added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 55–57why, one step back55 revoke() {56 // TODO: make sure this marks the Map as revoked, and assert everywhere57 }Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 58why, one step back58 }added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 59–64why, one step back59 }6061 get size(): number {62 return latest(this[DRAFT_STATE]).size63 }64fixed most testsMichel Weststratebefore adoption385c0a4
line 65why, one step back65 has(value: V): boolean {fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
lines 66–70why, one step back66 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 back71 if (state.copy.has(value)) return true72 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))73 return true74 return falsefixed most testsMichel Weststratebefore adoption385c0a4
lines 75–76why, one step back75 }76added set and iteror stuffMichel Weststratebefore adoption08f4682
line 77why, one step back77 add(value: V): this {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 78why, one step back78 const state = this[DRAFT_STATE]fixed most testsMichel Weststratebefore adoption385c0a4
lines 79–81why, one step back79 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 back82 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 back90 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
line 91why, one step back91 if (!this.has(value)) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 92why, one step back92 return falseadded set and iteror stuffMichel Weststratebefore adoption08f4682
lines 93–96why, one step back93 }9495 prepareCopy(state)96 state.scope.immer.markChanged(state)Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 97–102why, one step back97 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 back103 }104105 clear() {Some initial cleanupMichel Weststratebefore adoptionc53a75f
line 106why, one step back106 const state = this[DRAFT_STATE]added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 107–111why, one step back107 prepareCopy(state)108 state.scope.immer.markChanged(state)109 return state.copy!.clear()110 }111fixed most testsMichel Weststratebefore adoption385c0a4
lines 112–115why, one step back112 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 back116 }117fixed most testsMichel Weststratebefore adoption385c0a4
lines 118–122why, one step back118 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 back123fixed most testsMichel Weststratebefore adoption385c0a4
lines 124–132why, one step back124 keys(): IterableIterator<V> {125 return this.values()126 }127128 // TODO: factor out symbol129 [Symbol.iterator]() {130 return this.values()131 }132added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 133–134why, one step back133 forEach(cb, thisArg) {134 const state = this[DRAFT_STATE]fixed most testsMichel Weststratebefore adoption385c0a4
line 135why, one step back135 const iterator = this.values()added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 136–145why, one step back136 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 back146 return target // TODO: or copy?added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 147–165why, one step back147 }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}