Timothy Farrell 1c8d52253d 3.0.0 refactor
Bundle is no longer necessary since all tools have the "set all subscribers dirty before updating" behavior.

Added additional guarantees around subscription call order.

tool.fire() can be called externally
2017-12-15 22:58:03 -06:00

57 lines
1.1 KiB
JavaScript

import { id, registerSubscriptions, registerFire } from './util.js';
export function container(store, hash) {
let subscribers = [];
let id = hash(store);
const containerMethods = {
subscribe: registerSubscriptions(subscribers),
fire: registerFire(subscribers),
unsubscribeAll: () => {
subscribers = [];
}
};
function checkUpdate(target) {
const newId = hash(target);
if (id !== newId) {
id = newId;
containerMethods.fire(target);
}
}
const p = new Proxy(store, {
apply: (target, context, args) => {
return target;
},
get: (target, name) => {
if (name in containerMethods) {
return containerMethods[name];
}
if (name === '_') {
return target;
}
const thing = target[name];
if (typeof thing === 'function') {
return (...args) => {
const ret = target[name](...args);
checkUpdate(target);
return ret;
};
}
return thing;
},
set: (target, name, newVal) => {
if (name in containerMethods) {
throw new ReferenceError(`Cannot set ${name} in ${target}`);
}
target[name] = newVal;
checkUpdate(target);
return newVal;
}
});
return p;
}