UNPKG

@grafana/faro-core

Version:
104 lines 3.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.merge = exports.Observable = void 0; var Observable = /** @class */ (function () { function Observable() { this.subscribers = []; } /** * Subscribes a callback function to the observable. * @param callback - The function to call when the observable emits a value. * @returns A subscription object with an unsubscribe method to cancel the subscription. */ Observable.prototype.subscribe = function (callback) { var _this = this; this.subscribers.push(callback); return { unsubscribe: function () { return _this.unsubscribe(callback); }, }; }; /** * Notifies all subscribers with the given data. * @param data - The data to emit to all subscribers. */ Observable.prototype.notify = function (data) { this.subscribers.forEach(function (callback) { return callback(data); }); }; /** * Subscribes a callback function to the observable and automatically unsubscribes after the first emission. * @param callback - The function to call when the observable emits a value. * @returns A subscription object with an unsubscribe method to cancel the subscription. */ Observable.prototype.first = function (callback) { var subscription = this.subscribe(function (data) { callback(data); subscription.unsubscribe(); }); return subscription; }; /** * Emits values from the source observable until the provided predicate function returns false. * @param predicate - A function that evaluates each value emitted by the source observable. * @returns A new observable that emits values from the source observable while the predicate returns true. */ Observable.prototype.takeWhile = function (predicate) { var result = new Observable(); var subscription = this.subscribe(function (value) { if (predicate(value)) { result.notify(value); } else { subscription.unsubscribe(); } }); return result; }; /** * Unsubscribes all subscribers by clearing the subscribers array. * * @remarks * This method sets the `subscribers` array to an empty array, effectively removing all current subscribers. */ Observable.prototype.unsubscribeAll = function () { this.subscribers = []; }; Observable.prototype.unsubscribe = function (callback) { this.subscribers = this.subscribers.filter(function (sub) { return sub !== callback; }); }; return Observable; }()); exports.Observable = Observable; /** * Merges multiple observables into a single observable. * * @template T - The type of the values emitted by the observables. * @param {...Observable[]} observables - The observables to merge. * @returns {Observable} A new observable that emits values from all input observables. */ function merge() { var observables = []; for (var _i = 0; _i < arguments.length; _i++) { observables[_i] = arguments[_i]; } var mainObservable = new Observable(); var subscriptions = []; observables.forEach(function (observable) { var subscription = observable.subscribe(function (value) { mainObservable.notify(value); }); subscriptions.push(subscription); }); return { subscribe: mainObservable.subscribe.bind(mainObservable), takeWhile: mainObservable.takeWhile.bind(mainObservable), first: mainObservable.first.bind(mainObservable), notify: mainObservable.notify.bind(mainObservable), unsubscribeAll: function () { observables.forEach(function (observable) { observable.unsubscribeAll(); }); }, }; } exports.merge = merge; //# sourceMappingURL=reactive.js.map