rvx
Version:
A signal based rendering library
59 lines • 2.02 kB
JavaScript
import { $, batch } from "../core/signals.js";
import { ProbeMap } from "./probes.js";
export function createReactiveProxy(target, barrier) {
const iterators = $();
const getProbes = new ProbeMap(key => target[key]);
const hasProbes = new ProbeMap(key => key in target);
const proto = Object.getPrototypeOf(target);
function isReactive(prop) {
if (proto !== null && prop in proto) {
return false;
}
return true;
}
return new Proxy(target, {
get(target, prop, recv) {
if (isReactive(prop)) {
getProbes.access(prop);
}
return barrier.wrap(Reflect.get(target, prop, recv));
},
has(target, prop) {
if (isReactive(prop)) {
hasProbes.access(prop);
}
return Reflect.has(target, prop);
},
set(target, prop, value, recv) {
value = barrier.unwrap(value);
if (isReactive(prop)) {
return batch(() => {
const ok = Reflect.set(target, prop, value, recv);
if (ok) {
iterators.notify();
getProbes.update(prop, value);
hasProbes.update(prop, true);
}
return ok;
});
}
return Reflect.set(target, prop, value, recv);
},
deleteProperty(target, prop) {
return batch(() => {
const ok = Reflect.deleteProperty(target, prop);
if (ok && isReactive(prop)) {
iterators.notify();
getProbes.update(prop, undefined);
hasProbes.update(prop, false);
}
return ok;
});
},
ownKeys(target) {
iterators.access();
return Reflect.ownKeys(target);
},
});
}
//# sourceMappingURL=reactive-proxy.js.map