set.ts
This file
Path
src/set.ts
174 lines · 3.6 KiB · TypeScript
Last changed by
added set and iteror stuffMichel Weststratebefore adoption08f4682
History of this fileTake it
added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 1–174why, one step back1import {2 each,3 has,4 is,5 isDraft,6 isDraftable,7 isEnumerable,8 isMap,9 isSet,10 hasSymbol,11 shallowCopy,12 DRAFT_STATE,13 iterateMapValues,14 makeIterable,15 makeIterateSetValues,16 latest17} from "./common"1819// TODO: kill:20import {21 assertUnrevoked,22 ES5Draft,23} from "./es5"24import { ImmerScope } from "./scope";25import { Immer } from "./immer";2627// TODO: create own states28// TODO: clean up the maps and such from ES5 / Proxy states2930export interface SetState {31 parent: any; // TODO: type32 scope: ImmerScope;33 modified: boolean;34 finalizing: boolean;35 finalized: boolean;36 copy: Set<any> | undefined;37 // assigned: Map<any, boolean> | undefined;38 base: Set<any>;39 revoke(): void;40 draft: ES5Draft;41}4243function prepareCopy(state: SetState) {44 if (!state.copy) {45 state.copy = new Set(state.base);46 }47}4849// Make sure DraftSet declarion doesn't die if Map is not avialable...50const SetBase: SetConstructor = typeof Set !== "undefined" ? Set : function FakeSet() {} as any5152// TODO: fix types for drafts53export class DraftSet<K, V> extends SetBase implements Set<V> {54 [DRAFT_STATE]: SetState55 constructor(target, parent) {56 super()57 this[DRAFT_STATE] = {58 parent,59 scope: parent ? parent.scope : ImmerScope.current,60 modified: false,61 finalized: false,62 finalizing: false,63 copy: undefined,64 base: target,65 draft: this as any, // TODO: fix typing66 revoke() {67 // TODO: make sure this marks the Map as revoked, and assert everywhere68 }69 };70 }7172 get size(): number {73 return latest(this[DRAFT_STATE]).size74 }7576 add(value: V): this {77 const state = this[DRAFT_STATE];78 if (latest(state).has(value) !== value) {79 prepareCopy(state)80 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly81 state.copy!.add(value)82 }83 return this84 }8586 delete(value: V): boolean {87 if (!this.has(value)) {88 return false;89 }9091 const state = this[DRAFT_STATE];92 prepareCopy(state)93 state.scope.immer.markChanged(state)94 state.copy!.delete(value)95 return true96 }9798 clear() {99 const state = this[DRAFT_STATE];100 prepareCopy(state)101 state.scope.immer.markChanged(state)102 return state.copy!.clear()103 }104105 keys() {106 return latest(this[DRAFT_STATE]).keys();107 }108109 // TODO: values and entries iterators110 // @ts-ignore TODO:111 entries = iterateSetValues112 // values: iterateMapValues,113 // @ts-ignore TODO:114 values= iterateSetValues115 // entries: iterateMapValues116117 forEach(cb, thisArg) {118 const state = this[DRAFT_STATE]119 const iterator = iterateSetValues(state)()120 let result = iterator.next()121 while (!result.done) {122 cb.call(thisArg, result.value, result.value, state.draft)123 result = iterator.next()124 }125 }126}127128129export function proxySet(target, parent) {130 if (target instanceof DraftSet) {131 return target; // TODO: or copy?132 }133 return new DraftSet(target, parent)134}135136if (hasSymbol) {137 Object.defineProperty(138 DraftSet.prototype,139 Symbol.iterator,140 // @ts-ignore TODO fix141 proxyMethod(iterateMapValues)142 )143}144145const iterateSetValues = makeIterateSetValues()146147// TODO: kill?148function proxyMethod(trap, key) {149 return {150 get() {151 return function(this: ES5Draft, ...args) {152 const state = this[DRAFT_STATE]153 assertUnrevoked(state)154 return trap(state, key, state.draft)(...args)155 }156 }157 }158}159160export function hasSetChanges(state) {161 const {base, draft} = state162163 if (base.size !== draft.size) return true164165 // IE11 supports only forEach iteration166 let hasChanges = false167 // TODO: optimize: break on first diff168 draft.forEach(function(value, key) {169 if (!hasChanges) {170 hasChanges = isDraftable(value) ? value.modified : !base.has(key)171 }172 })173 return hasChanges174}