UNPKG

vscroll

Version:
55 lines 1.16 kB
export class Reactive { constructor(value, options) { this.id = 0; if (value !== void 0) { this.value = value; this.initialValue = value; } this.options = options || {}; this.subscriptions = new Map(); } set(value) { if (this.value === value && !this.options.emitEqual) { return; } this.value = value; for (const [, sub] of this.subscriptions) { sub.emit(value); if (this.value !== value) { break; } } } get() { return this.value; } on(func) { const id = this.id++; const subscription = { emit: func, off: () => { subscription.emit = () => null; this.subscriptions.delete(id); } }; this.subscriptions.set(id, subscription); if (this.options.emitOnSubscribe) { subscription.emit(this.value); } return () => subscription.off(); } once(func) { const off = this.on(v => { off(); func(v); }); return off; } reset() { this.set(this.initialValue); } dispose() { this.subscriptions.forEach(sub => sub.off()); } } //# sourceMappingURL=reactive.js.map