| 1 |
import { Operator } from './Operator'; |
| 2 |
import { Observable } from './Observable'; |
| 3 |
import { Subscriber } from './Subscriber'; |
| 4 |
import { Subscription, EMPTY_SUBSCRIPTION } from './Subscription'; |
| 5 |
import { Observer, SubscriptionLike, TeardownLogic } from './types'; |
| 6 |
import { ObjectUnsubscribedError } from './util/ObjectUnsubscribedError'; |
| 7 |
import { arrRemove } from './util/arrRemove'; |
| 8 |
import { errorContext } from './util/errorContext'; |
| 9 |
|
| 10 |
/** |
| 11 |
* A Subject is a special type of Observable that allows values to be |
| 12 |
* multicasted to many Observers. Subjects are like EventEmitters. |
| 13 |
* |
| 14 |
* Every Subject is an Observable and an Observer. You can subscribe to a |
| 15 |
* Subject, and you can call next to feed values as well as error and complete. |
| 16 |
*/ |
| 17 |
export class Subject<T> extends Observable<T> implements SubscriptionLike { |
| 18 |
closed = false; |
| 19 |
|
| 20 |
private currentObservers: Observer<T>[] | null = null; |
| 21 |
|
| 22 |
/** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */ |
| 23 |
observers: Observer<T>[] = []; |
| 24 |
/** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */ |
| 25 |
isStopped = false; |
| 26 |
/** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */ |
| 27 |
hasError = false; |
| 28 |
/** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */ |
| 29 |
thrownError: any = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Creates a "subject" by basically gluing an observer to an observable. |
| 33 |
* |
| 34 |
* @deprecated Recommended you do not use. Will be removed at some point in the future. Plans for replacement still under discussion. |
| 35 |
*/ |
| 36 |
static create: (...args: any[]) => any = <T>(destination: Observer<T>, source: Observable<T>): AnonymousSubject<T> => { |
| 37 |
return new AnonymousSubject<T>(destination, source); |
| 38 |
}; |
| 39 |
|
| 40 |
constructor() { |
| 41 |
// NOTE: This must be here to obscure Observable's constructor. |
| 42 |
super(); |
| 43 |
} |
| 44 |
|
| 45 |
/** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */ |
| 46 |
lift<R>(operator: Operator<T, R>): Observable<R> { |
| 47 |
const subject = new AnonymousSubject(this, this); |
| 48 |
subject.operator = operator as any; |
| 49 |
return subject as any; |
| 50 |
} |
| 51 |
|
| 52 |
/** @internal */ |
| 53 |
protected _throwIfClosed() { |
| 54 |
if (this.closed) { |
| 55 |
throw new ObjectUnsubscribedError(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
next(value: T) { |
| 60 |
errorContext(() => { |
| 61 |
this._throwIfClosed(); |
| 62 |
if (!this.isStopped) { |
| 63 |
if (!this.currentObservers) { |
| 64 |
this.currentObservers = Array.from(this.observers); |
| 65 |
} |
| 66 |
for (const observer of this.currentObservers) { |
| 67 |
observer.next(value); |
| 68 |
} |
| 69 |
} |
| 70 |
}); |
| 71 |
} |
| 72 |
|
| 73 |
error(err: any) { |
| 74 |
errorContext(() => { |
| 75 |
this._throwIfClosed(); |
| 76 |
if (!this.isStopped) { |
| 77 |
this.hasError = this.isStopped = true; |
| 78 |
this.thrownError = err; |
| 79 |
const { observers } = this; |
| 80 |
while (observers.length) { |
| 81 |
observers.shift()!.error(err); |
| 82 |
} |
| 83 |
} |
| 84 |
}); |
| 85 |
} |
| 86 |
|
| 87 |
complete() { |
| 88 |
errorContext(() => { |
| 89 |
this._throwIfClosed(); |
| 90 |
if (!this.isStopped) { |
| 91 |
this.isStopped = true; |
| 92 |
const { observers } = this; |
| 93 |
while (observers.length) { |
| 94 |
observers.shift()!.complete(); |
| 95 |
} |
| 96 |
} |
| 97 |
}); |
| 98 |
} |
| 99 |
|
| 100 |
unsubscribe() { |
| 101 |
this.isStopped = this.closed = true; |
| 102 |
this.observers = this.currentObservers = null!; |
| 103 |
} |
| 104 |
|
| 105 |
get observed() { |
| 106 |
return this.observers?.length > 0; |
| 107 |
} |
| 108 |
|
| 109 |
/** @internal */ |
| 110 |
protected _trySubscribe(subscriber: Subscriber<T>): TeardownLogic { |
| 111 |
this._throwIfClosed(); |
| 112 |
return super._trySubscribe(subscriber); |
| 113 |
} |
| 114 |
|
| 115 |
/** @internal */ |
| 116 |
protected _subscribe(subscriber: Subscriber<T>): Subscription { |
| 117 |
this._throwIfClosed(); |
| 118 |
this._checkFinalizedStatuses(subscriber); |
| 119 |
return this._innerSubscribe(subscriber); |
| 120 |
} |
| 121 |
|
| 122 |
/** @internal */ |
| 123 |
protected _innerSubscribe(subscriber: Subscriber<any>) { |
| 124 |
const { hasError, isStopped, observers } = this; |
| 125 |
if (hasError || isStopped) { |
| 126 |
return EMPTY_SUBSCRIPTION; |
| 127 |
} |
| 128 |
this.currentObservers = null; |
| 129 |
observers.push(subscriber); |
| 130 |
return new Subscription(() => { |
| 131 |
this.currentObservers = null; |
| 132 |
arrRemove(observers, subscriber); |
| 133 |
}); |
| 134 |
} |
| 135 |
|
| 136 |
/** @internal */ |
| 137 |
protected _checkFinalizedStatuses(subscriber: Subscriber<any>) { |
| 138 |
const { hasError, thrownError, isStopped } = this; |
| 139 |
if (hasError) { |
| 140 |
subscriber.error(thrownError); |
| 141 |
} else if (isStopped) { |
| 142 |
subscriber.complete(); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Creates a new Observable with this Subject as the source. You can do this |
| 148 |
* to create custom Observer-side logic of the Subject and conceal it from |
| 149 |
* code that uses the Observable. |
| 150 |
* @return Observable that this Subject casts to. |
| 151 |
*/ |
| 152 |
asObservable(): Observable<T> { |
| 153 |
const observable: any = new Observable<T>(); |
| 154 |
observable.source = this; |
| 155 |
return observable; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
export class AnonymousSubject<T> extends Subject<T> { |
| 160 |
constructor( |
| 161 |
/** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */ |
| 162 |
public destination?: Observer<T>, |
| 163 |
source?: Observable<T> |
| 164 |
) { |
| 165 |
super(); |
| 166 |
this.source = source; |
| 167 |
} |
| 168 |
|
| 169 |
next(value: T) { |
| 170 |
this.destination?.next?.(value); |
| 171 |
} |
| 172 |
|
| 173 |
error(err: any) { |
| 174 |
this.destination?.error?.(err); |
| 175 |
} |
| 176 |
|
| 177 |
complete() { |
| 178 |
this.destination?.complete?.(); |
| 179 |
} |
| 180 |
|
| 181 |
/** @internal */ |
| 182 |
protected _subscribe(subscriber: Subscriber<T>): Subscription { |
| 183 |
return this.source?.subscribe(subscriber) ?? EMPTY_SUBSCRIPTION; |
| 184 |
} |
| 185 |
} |