-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathset.ts
185 lines (181 loc) · 5.62 KB
/
set.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { ProxyDraft } from './interface';
import { dataTypes, iteratorSymbol } from './constant';
import { internal } from './internal';
import {
ensureShallowCopy,
getProxyDraft,
isDraftable,
markChanged,
markFinalization,
} from './utils';
import { checkReadable } from './unsafe';
import { generatePatches } from './patch';
const getNextIterator =
(
target: ProxyDraft<any>,
iterator: IterableIterator<any>,
{ isValuesIterator }: { isValuesIterator: boolean }
) =>
() => {
const result = iterator.next();
if (result.done) return result;
const key = result.value as any;
let value = target.setMap!.get(key);
const currentDraft = getProxyDraft(value);
const mutable =
target.options.mark?.(value, dataTypes) === dataTypes.mutable;
if (target.options.strict) {
checkReadable(key, target.options, mutable);
}
if (
!mutable &&
!currentDraft &&
isDraftable(key, target.options) &&
!target.finalized &&
target.original!.has(key)
) {
// draft a draftable original set item
const proxy = internal.createDraft({
original: key,
parentDraft: target,
key,
finalities: target.finalities,
options: target.options,
});
target.setMap!.set(key, proxy);
value = proxy;
} else if (currentDraft) {
// drafted
value = currentDraft.proxy;
}
return {
done: false,
value: isValuesIterator ? value : [value, value],
};
};
export const setHandler = {
get size() {
const target: ProxyDraft<any> = getProxyDraft(this)!;
return target.setMap!.size;
},
has(value: any) {
const target = getProxyDraft(this)!;
// reassigned or non-draftable values
if (target.setMap!.has(value)) return true;
ensureShallowCopy(target);
const valueProxyDraft = getProxyDraft(value)!;
// drafted
if (valueProxyDraft && target.setMap!.has(valueProxyDraft.original))
return true;
return false;
},
add(value: any) {
const target = getProxyDraft(this)!;
if (!this.has(value)) {
ensureShallowCopy(target);
markChanged(target);
target.assignedMap!.set(value, true);
target.setMap!.set(value, value);
markFinalization(target, value, value, generatePatches);
}
return this;
},
delete(value: any): boolean {
if (!this.has(value)) {
return false;
}
const target = getProxyDraft(this)!;
ensureShallowCopy(target);
markChanged(target);
const valueProxyDraft = getProxyDraft(value)!;
if (valueProxyDraft && target.setMap!.has(valueProxyDraft.original)) {
// delete drafted
target.assignedMap!.set(valueProxyDraft.original, false);
return target.setMap!.delete(valueProxyDraft.original);
}
if (!valueProxyDraft && target.setMap!.has(value)) {
// non-draftable values
target.assignedMap!.set(value, false);
} else {
// reassigned
target.assignedMap!.delete(value);
}
// delete reassigned or non-draftable values
return target.setMap!.delete(value);
},
clear() {
if (!this.size) return;
const target = getProxyDraft(this)!;
ensureShallowCopy(target);
markChanged(target);
for (const value of target.original) {
target.assignedMap!.set(value, false);
}
target.setMap!.clear();
},
values(): IterableIterator<any> {
const target = getProxyDraft(this)!;
ensureShallowCopy(target);
const iterator = target.setMap!.keys();
return {
[Symbol.iterator]: () => this.values(),
next: getNextIterator(target, iterator, { isValuesIterator: true }),
};
},
entries(): IterableIterator<[any, any]> {
const target = getProxyDraft(this)!;
ensureShallowCopy(target);
const iterator = target.setMap!.keys();
return {
[Symbol.iterator]: () => this.entries(),
next: getNextIterator(target, iterator, {
isValuesIterator: false,
}) as () => IteratorReturnResult<any>,
};
},
keys(): IterableIterator<any> {
return this.values();
},
[iteratorSymbol]() {
return this.values();
},
forEach(callback: any, thisArg?: any) {
const iterator = this.values();
let result = iterator.next();
while (!result.done) {
callback.call(thisArg, result.value, result.value, this);
result = iterator.next();
}
},
};
if (Set.prototype.difference) {
// for compatibility with new Set methods
// https://github.com/tc39/proposal-set-methods
Object.assign(setHandler, {
intersection(this: Set<any>, other: ReadonlySetLike<any>): Set<any> {
return Set.prototype.intersection.call(new Set(this.values()), other);
},
union(this: Set<any>, other: ReadonlySetLike<any>): Set<any> {
return Set.prototype.union.call(new Set(this.values()), other);
},
difference(this: Set<any>, other: ReadonlySetLike<any>): Set<any> {
return Set.prototype.difference.call(new Set(this.values()), other);
},
symmetricDifference(this: Set<any>, other: ReadonlySetLike<any>): Set<any> {
return Set.prototype.symmetricDifference.call(
new Set(this.values()),
other
);
},
isSubsetOf(this: Set<any>, other: ReadonlySetLike<any>): boolean {
return Set.prototype.isSubsetOf.call(new Set(this.values()), other);
},
isSupersetOf(this: Set<any>, other: ReadonlySetLike<any>): boolean {
return Set.prototype.isSupersetOf.call(new Set(this.values()), other);
},
isDisjointFrom(this: Set<any>, other: ReadonlySetLike<any>): boolean {
return Set.prototype.isDisjointFrom.call(new Set(this.values()), other);
},
});
}
export const setHandlerKeys = Reflect.ownKeys(setHandler);