| 1 |
import { Subject } from './Subject'; |
| 2 |
import { Subscriber } from './Subscriber'; |
| 3 |
|
| 4 |
/** |
| 5 |
* A variant of Subject that only emits a value when it completes. It will emit |
| 6 |
* its latest value to all its observers on completion. |
| 7 |
*/ |
| 8 |
export class AsyncSubject<T> extends Subject<T> { |
| 9 |
private _value: T | null = null; |
| 10 |
private _hasValue = false; |
| 11 |
private _isComplete = false; |
| 12 |
|
| 13 |
/** @internal */ |
| 14 |
protected _checkFinalizedStatuses(subscriber: Subscriber<T>) { |
| 15 |
const { hasError, _hasValue, _value, thrownError, isStopped, _isComplete } = this; |
| 16 |
if (hasError) { |
| 17 |
subscriber.error(thrownError); |
| 18 |
} else if (isStopped || _isComplete) { |
| 19 |
_hasValue && subscriber.next(_value!); |
| 20 |
subscriber.complete(); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
next(value: T): void { |
| 25 |
if (!this.isStopped) { |
| 26 |
this._value = value; |
| 27 |
this._hasValue = true; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
complete(): void { |
| 32 |
const { _hasValue, _value, _isComplete } = this; |
| 33 |
if (!_isComplete) { |
| 34 |
this._isComplete = true; |
| 35 |
_hasValue && super.next(_value!); |
| 36 |
super.complete(); |
| 37 |
} |
| 38 |
} |
| 39 |
} |