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

22 lines
536 B
JavaScript

import { id, registerSubscriptions, registerFire } from './util.js';
export function prop(store, hash = id) {
let subscribers = [];
let id = hash(store);
const accessor = function _prop(newVal) {
const newId = hash(newVal);
if (newVal !== undefined && id !== newId) {
id = newId;
store = newVal;
accessor.fire(store);
}
return store;
};
accessor.subscribe = registerSubscriptions(subscribers);
accessor.fire = registerFire(subscribers);
accessor.unsubscribeAll = () => (subscribers = []);
return accessor;
}