UNPKG

vasille

Version:

The same framework which is designed to build bulletproof frontends (core library).

34 lines (33 loc) 706 B
import { safe } from "../functional/safety.js"; /** * A reactive object * @class Reactive * @extends Destroyable */ export class Reactive { /** * A list of user-defined bindings */ linked = []; onDestroy; bind(value) { this.linked.push(value); } runOnDestroy(func) { const existing = this.onDestroy; if (existing) { this.onDestroy = () => { existing(); safe(func)(); }; } else { this.onDestroy = safe(func); } } destroy() { this.onDestroy?.(); this.linked.forEach(item => item.destroy()); this.linked.splice(0); } }