haunted
Version:
Hooks for web components
47 lines (46 loc) • 1.11 kB
JavaScript
import { setCurrent, clear } from "./interface";
import { hookSymbol, effectsSymbol, layoutEffectsSymbol, } from "./symbols";
class State {
update;
host;
virtual;
[hookSymbol];
[effectsSymbol];
[layoutEffectsSymbol];
constructor(update, host) {
this.update = update;
this.host = host;
this[hookSymbol] = new Map();
this[effectsSymbol] = [];
this[layoutEffectsSymbol] = [];
}
run(cb) {
setCurrent(this);
let res = cb();
clear();
return res;
}
_runEffects(phase) {
let effects = this[phase];
setCurrent(this);
for (let effect of effects) {
effect.call(this);
}
clear();
}
runEffects() {
this._runEffects(effectsSymbol);
}
runLayoutEffects() {
this._runEffects(layoutEffectsSymbol);
}
teardown() {
let hooks = this[hookSymbol];
hooks.forEach((hook) => {
if (typeof hook.teardown === "function") {
hook.teardown();
}
});
}
}
export { State };