UNPKG

react-obsidian

Version:

Dependency injection framework for React and React Native applications

41 lines 1.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Observable = void 0; class Observable { constructor(initialValue) { this.subscribers = new Set(); this.currentValue = initialValue; } get value() { return this.currentValue; } set value(value) { this.currentValue = value; this.subscribers.forEach((subscriber) => subscriber(value)); } async first() { if (this.currentValue) return this.currentValue; return new Promise((resolve) => { const unsubscribe = this.subscribe((value) => { unsubscribe(); resolve(value); }); }); } subscribe(onNext) { if (this.subscribers.has(onNext)) { throw new Error('Subscriber already subscribed'); } this.subscribers.add(onNext); return () => this.subscribers.delete(onNext); } unsubscribe(onNext) { if (!this.subscribers.has(onNext)) { throw new Error(`Can't unsubscribe, subscriber doesn't exist`); } this.subscribers.delete(onNext); } } exports.Observable = Observable; //# sourceMappingURL=Observable.js.map