UNPKG

5.95 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var Observable_1 = require("./Observable");
14var Subscriber_1 = require("./Subscriber");
15var Subscription_1 = require("./Subscription");
16var ObjectUnsubscribedError_1 = require("./util/ObjectUnsubscribedError");
17var SubjectSubscription_1 = require("./SubjectSubscription");
18var rxSubscriber_1 = require("../internal/symbol/rxSubscriber");
19var SubjectSubscriber = (function (_super) {
20 __extends(SubjectSubscriber, _super);
21 function SubjectSubscriber(destination) {
22 var _this = _super.call(this, destination) || this;
23 _this.destination = destination;
24 return _this;
25 }
26 return SubjectSubscriber;
27}(Subscriber_1.Subscriber));
28exports.SubjectSubscriber = SubjectSubscriber;
29var Subject = (function (_super) {
30 __extends(Subject, _super);
31 function Subject() {
32 var _this = _super.call(this) || this;
33 _this.observers = [];
34 _this.closed = false;
35 _this.isStopped = false;
36 _this.hasError = false;
37 _this.thrownError = null;
38 return _this;
39 }
40 Subject.prototype[rxSubscriber_1.rxSubscriber] = function () {
41 return new SubjectSubscriber(this);
42 };
43 Subject.prototype.lift = function (operator) {
44 var subject = new AnonymousSubject(this, this);
45 subject.operator = operator;
46 return subject;
47 };
48 Subject.prototype.next = function (value) {
49 if (this.closed) {
50 throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
51 }
52 if (!this.isStopped) {
53 var observers = this.observers;
54 var len = observers.length;
55 var copy = observers.slice();
56 for (var i = 0; i < len; i++) {
57 copy[i].next(value);
58 }
59 }
60 };
61 Subject.prototype.error = function (err) {
62 if (this.closed) {
63 throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
64 }
65 this.hasError = true;
66 this.thrownError = err;
67 this.isStopped = true;
68 var observers = this.observers;
69 var len = observers.length;
70 var copy = observers.slice();
71 for (var i = 0; i < len; i++) {
72 copy[i].error(err);
73 }
74 this.observers.length = 0;
75 };
76 Subject.prototype.complete = function () {
77 if (this.closed) {
78 throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
79 }
80 this.isStopped = true;
81 var observers = this.observers;
82 var len = observers.length;
83 var copy = observers.slice();
84 for (var i = 0; i < len; i++) {
85 copy[i].complete();
86 }
87 this.observers.length = 0;
88 };
89 Subject.prototype.unsubscribe = function () {
90 this.isStopped = true;
91 this.closed = true;
92 this.observers = null;
93 };
94 Subject.prototype._trySubscribe = function (subscriber) {
95 if (this.closed) {
96 throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
97 }
98 else {
99 return _super.prototype._trySubscribe.call(this, subscriber);
100 }
101 };
102 Subject.prototype._subscribe = function (subscriber) {
103 if (this.closed) {
104 throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
105 }
106 else if (this.hasError) {
107 subscriber.error(this.thrownError);
108 return Subscription_1.Subscription.EMPTY;
109 }
110 else if (this.isStopped) {
111 subscriber.complete();
112 return Subscription_1.Subscription.EMPTY;
113 }
114 else {
115 this.observers.push(subscriber);
116 return new SubjectSubscription_1.SubjectSubscription(this, subscriber);
117 }
118 };
119 Subject.prototype.asObservable = function () {
120 var observable = new Observable_1.Observable();
121 observable.source = this;
122 return observable;
123 };
124 Subject.create = function (destination, source) {
125 return new AnonymousSubject(destination, source);
126 };
127 return Subject;
128}(Observable_1.Observable));
129exports.Subject = Subject;
130var AnonymousSubject = (function (_super) {
131 __extends(AnonymousSubject, _super);
132 function AnonymousSubject(destination, source) {
133 var _this = _super.call(this) || this;
134 _this.destination = destination;
135 _this.source = source;
136 return _this;
137 }
138 AnonymousSubject.prototype.next = function (value) {
139 var destination = this.destination;
140 if (destination && destination.next) {
141 destination.next(value);
142 }
143 };
144 AnonymousSubject.prototype.error = function (err) {
145 var destination = this.destination;
146 if (destination && destination.error) {
147 this.destination.error(err);
148 }
149 };
150 AnonymousSubject.prototype.complete = function () {
151 var destination = this.destination;
152 if (destination && destination.complete) {
153 this.destination.complete();
154 }
155 };
156 AnonymousSubject.prototype._subscribe = function (subscriber) {
157 var source = this.source;
158 if (source) {
159 return this.source.subscribe(subscriber);
160 }
161 else {
162 return Subscription_1.Subscription.EMPTY;
163 }
164 };
165 return AnonymousSubject;
166}(Subject));
167exports.AnonymousSubject = AnonymousSubject;
168//# sourceMappingURL=Subject.js.map
\No newline at end of file