Skip to content
Myr

set.ts

This file

Path

src/set.ts

172 lines · 3.5 KiB · TypeScript

Last changed by

fixed most testsMichel Weststratebefore adoption385c0a4

History of this file

Take it

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 1–13why, one step back
1import {
2 each,3 has,4 is,5 isDraft,6 isDraftable,7 isEnumerable,8 isMap,9 isSet,10 hasSymbol,11 shallowCopy,12 DRAFT_STATE,13 makeIterable,

fixed most testsMichel Weststratebefore adoption385c0a4

lines 14–16why, one step back
14 latest,
15 original,16 makeIterable2

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 17–44why, one step back
17} 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) {

fixed most testsMichel Weststratebefore adoption385c0a4

lines 45–49why, one step back
45 // create drafts for all entries to preserve insertion order
46 state.copy = new Set()47 state.base.forEach(value => {48 state.copy!.add(state.scope.immer.createProxy(value, state))49 })

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 50–79why, one step back
50 }
51}5253// Make sure DraftSet declarion doesn't die if Map is not avialable...54const SetBase: SetConstructor = typeof Set !== "undefined" ? Set : function FakeSet() {} as any5556// TODO: fix types for drafts57export class DraftSet<K, V> extends SetBase implements Set<V> {58 [DRAFT_STATE]: SetState59 constructor(target, parent) {60 super()61 this[DRAFT_STATE] = {62 parent,63 scope: parent ? parent.scope : ImmerScope.current,64 modified: false,65 finalized: false,66 finalizing: false,67 copy: undefined,68 base: target,69 draft: this as any, // TODO: fix typing70 revoke() {71 // TODO: make sure this marks the Map as revoked, and assert everywhere72 }73 };74 }7576 get size(): number {77 return latest(this[DRAFT_STATE]).size78 }79

fixed most testsMichel Weststratebefore adoption385c0a4

lines 80–83why, one step back
80 has(value: V): boolean {
81 return latest(this[DRAFT_STATE]).has(value)82 }83

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 84–85why, one step back
84 add(value: V): this {
85 const state = this[DRAFT_STATE];

fixed most testsMichel Weststratebefore adoption385c0a4

lines 86–88why, one step back
86 if (state.copy) {
87 state.copy.add(value)88 } else if (!state.base.has(value)) {

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 89–103why, one step back
89 prepareCopy(state)
90 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly91 state.copy!.add(value)92 }93 return this94 }9596 delete(value: V): boolean {97 if (!this.has(value)) {98 return false;99 }100101 const state = this[DRAFT_STATE];102 prepareCopy(state)103 state.scope.immer.markChanged(state)

fixed most testsMichel Weststratebefore adoption385c0a4

line 104why, one step back
104 return state.copy!.delete(value)

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 105–113why, one step back
105 }
106107 clear() {108 const state = this[DRAFT_STATE];109 prepareCopy(state)110 state.scope.immer.markChanged(state)111 return state.copy!.clear()112 }113

fixed most testsMichel Weststratebefore adoption385c0a4

lines 114–117why, one step back
114 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 back
118 }
119

fixed most testsMichel Weststratebefore adoption385c0a4

lines 120–124why, one step back
120 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 back
125

fixed most testsMichel Weststratebefore adoption385c0a4

lines 126–134why, one step back
126 keys(): IterableIterator<V> {
127 return this.values()128 }129130 // TODO: factor out symbol131 [Symbol.iterator]() {132 return this.values()133 }134

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 135–136why, one step back
135 forEach(cb, thisArg) {
136 const state = this[DRAFT_STATE]

fixed most testsMichel Weststratebefore adoption385c0a4

line 137why, one step back
137 const iterator = this.values()

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 138–153why, one step back
138 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}145146147export function proxySet(target, parent) {148 if (target instanceof DraftSet) {149 return target; // TODO: or copy?150 }151 return new DraftSet(target, parent)152}153

fixed most testsMichel Weststratebefore adoption385c0a4

line 154why, one step back
154// const iterateSetValues = makeIterateSetValues()

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 155–172why, one step back
155
156157158export function hasSetChanges(state) {159 const {base, draft} = state160161 if (base.size !== draft.size) return true162163 // IE11 supports only forEach iteration164 let hasChanges = false165 // TODO: optimize: break on first diff166 draft.forEach(function(value, key) {167 if (!hasChanges) {168 hasChanges = isDraftable(value) ? value.modified : !base.has(key)169 }170 })171 return hasChanges172}