UNPKG

1.5 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Subject = exports.Observable = void 0;
4const observable_fns_1 = require("observable-fns");
5Object.defineProperty(exports, "Observable", { enumerable: true, get: function () { return observable_fns_1.Observable; } });
6const $observers = Symbol("observers");
7/**
8 * Observable subject. Implements the Observable interface, but also exposes
9 * the `next()`, `error()`, `complete()` methods to initiate observable
10 * updates "from the outside".
11 *
12 * Use `Observable.from(subject)` to derive an observable that proxies all
13 * values, errors and the completion raised on this subject, but does not
14 * expose the `next()`, `error()`, `complete()` methods.
15 */
16class Subject extends observable_fns_1.Observable {
17 constructor() {
18 super(observer => {
19 this[$observers] = [
20 ...(this[$observers] || []),
21 observer
22 ];
23 const unsubscribe = () => {
24 this[$observers] = this[$observers].filter(someObserver => someObserver !== observer);
25 };
26 return unsubscribe;
27 });
28 this[$observers] = [];
29 }
30 complete() {
31 this[$observers].forEach(observer => observer.complete());
32 }
33 error(error) {
34 this[$observers].forEach(observer => observer.error(error));
35 }
36 next(value) {
37 this[$observers].forEach(observer => observer.next(value));
38 }
39}
40exports.Subject = Subject;