-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Subject.ts
205 lines (163 loc) · 4.61 KB
/
Subject.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import {Operator} from './Operator';
import {Observer} from './Observer';
import {Observable} from './Observable';
import {Subscriber} from './Subscriber';
import {Subscription} from './Subscription';
import {SubjectSubscription} from './subject/SubjectSubscription';
import {rxSubscriber} from './symbol/rxSubscriber';
export class Subject<T> extends Observable<T> implements Observer<T>, Subscription {
static create: Function = <T>(source: Observable<T>, destination: Observer<T>): Subject<T> => {
return new Subject<T>(source, destination);
};
constructor(source?: Observable<T>, destination?: Observer<T>) {
super();
this.source = source;
this.destination = destination;
}
public observers: Observer<T>[] = [];
public isUnsubscribed: boolean = false;
protected destination: Observer<T>;
protected isStopped: boolean = false;
protected hasErrored: boolean = false;
protected errorValue: any;
protected dispatching: boolean = false;
protected hasCompleted: boolean = false;
lift<T, R>(operator: Operator<T, R>): Observable<T> {
const subject = new Subject(this, this.destination || this);
subject.operator = operator;
return <any>subject;
}
add(subscription: Subscription|Function|void): void {
Subscription.prototype.add.call(this, subscription);
}
remove(subscription: Subscription): void {
Subscription.prototype.remove.call(this, subscription);
}
unsubscribe(): void {
Subscription.prototype.unsubscribe.call(this);
}
_subscribe(subscriber: Subscriber<T>): Subscription | Function | void {
if (this.source) {
return this.source.subscribe(subscriber);
} else {
if (subscriber.isUnsubscribed) {
return;
} else if (this.hasErrored) {
return subscriber.error(this.errorValue);
} else if (this.hasCompleted) {
return subscriber.complete();
} else if (this.isUnsubscribed) {
throw new Error('Cannot subscribe to a disposed Subject.');
}
const subscription = new SubjectSubscription(this, subscriber);
this.observers.push(subscriber);
return subscription;
}
}
_unsubscribe(): void {
this.source = null;
this.isStopped = true;
this.observers = null;
this.destination = null;
}
next(value: T): void {
if (this.isStopped) {
return;
}
this.dispatching = true;
this._next(value);
this.dispatching = false;
if (this.hasErrored) {
this._error(this.errorValue);
} else if (this.hasCompleted) {
this._complete();
}
}
error(err?: any): void {
if (this.isStopped) {
return;
}
this.isStopped = true;
this.hasErrored = true;
this.errorValue = err;
if (this.dispatching) {
return;
}
this._error(err);
}
complete(): void {
if (this.isStopped) {
return;
}
this.isStopped = true;
this.hasCompleted = true;
if (this.dispatching) {
return;
}
this._complete();
}
protected _next(value: T): void {
if (this.destination) {
this.destination.next(value);
} else {
this._finalNext(value);
}
}
protected _finalNext(value: T): void {
let index = -1;
const observers = this.observers.slice(0);
const len = observers.length;
while (++index < len) {
observers[index].next(value);
}
}
protected _error(err: any): void {
if (this.destination) {
this.destination.error(err);
} else {
this._finalError(err);
}
}
protected _finalError(err: any): void {
let index = -1;
const observers = this.observers;
// optimization to block our SubjectSubscriptions from
// splicing themselves out of the observers list one by one.
this.observers = null;
this.isUnsubscribed = true;
if (observers) {
const len = observers.length;
while (++index < len) {
observers[index].error(err);
}
}
this.isUnsubscribed = false;
this.unsubscribe();
}
protected _complete(): void {
if (this.destination) {
this.destination.complete();
} else {
this._finalComplete();
}
}
protected _finalComplete(): void {
let index = -1;
const observers = this.observers;
// optimization to block our SubjectSubscriptions from
// splicing themselves out of the observers list one by one.
this.observers = null;
this.isUnsubscribed = true;
if (observers) {
const len = observers.length;
while (++index < len) {
observers[index].complete();
}
}
this.isUnsubscribed = false;
this.unsubscribe();
}
[rxSubscriber]() {
return new Subscriber<T>(this);
}
}