rvx
Version:
A signal based rendering library
92 lines • 2.63 kB
JavaScript
import { $, batch } from "../core/signals.js";
import { ProbeMap } from "./probes.js";
export class ReactiveSet extends Set {
#target;
#barrier;
#size;
#iterators;
#probes;
constructor(target, barrier) {
super();
this.#target = target;
this.#barrier = barrier;
this.#size = $(target.size);
this.#iterators = $();
this.#probes = new ProbeMap(key => target.has(key));
}
get size() {
this.#size.access();
return this.#target.size;
}
has(value) {
value = this.#barrier.unwrap(value);
this.#probes.access(value);
return this.#target.has(value);
}
add(value) {
batch(() => {
value = this.#barrier.unwrap(value);
this.#target.add(value);
this.#size.value = this.#target.size;
this.#iterators.notify();
this.#probes.update(value, true);
});
return this;
}
delete(value) {
return batch(() => {
value = this.#barrier.unwrap(value);
const deleted = this.#target.delete(value);
if (deleted) {
this.#size.value = this.#target.size;
this.#iterators.notify();
this.#probes.update(value, false);
}
return deleted;
});
}
clear() {
batch(() => {
this.#target.clear();
this.#size.value = 0;
this.#iterators.notify();
this.#probes.fill(false);
});
}
*entries() {
this.#iterators.access();
for (const entry of this.#target.entries()) {
const value = this.#barrier.wrap(entry[0]);
yield [value, value];
}
}
*keys() {
this.#iterators.access();
for (const key of this.#target.keys()) {
yield this.#barrier.wrap(key);
}
}
*values() {
this.#iterators.access();
for (const value of this.#target.values()) {
yield this.#barrier.wrap(value);
}
}
forEach(callback, thisArg) {
this.#iterators.access();
return this.#target.forEach(value => {
value = this.#barrier.wrap(value);
callback.call(thisArg, value, value, this);
}, thisArg);
}
*[Symbol.iterator]() {
this.#iterators.access();
for (const value of this.#target) {
yield this.#barrier.wrap(value);
}
}
get [Symbol.toStringTag]() {
return this.#target[Symbol.toStringTag];
}
}
//# sourceMappingURL=reactive-set.js.map