ha-frontend-cdce8p/src/util/nuclear-behavior.js
2016-04-28 16:19:27 +02:00

30 lines
765 B
JavaScript

export default function NuclearObserver(reactor) {
return {
attached() {
this.nuclearUnwatchFns = Object.keys(this.properties).reduce(
(unwatchFns, key) => {
if (!('bindNuclear' in this.properties[key])) {
return unwatchFns;
}
const getter = this.properties[key].bindNuclear;
if (!getter) {
throw new Error(`Undefined getter specified for key ${key}`);
}
this[key] = reactor.evaluate(getter);
return unwatchFns.concat(reactor.observe(getter, (val) => {
this[key] = val;
}));
}, []);
},
detached() {
while (this.nuclearUnwatchFns.length) {
this.nuclearUnwatchFns.shift()();
}
},
};
}