Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | import { Subject } from './Subject';
|
---|
| 2 | import { Subscriber } from './Subscriber';
|
---|
| 3 | import { Subscription } from './Subscription';
|
---|
| 4 | import { SubscriptionLike } from './types';
|
---|
| 5 | import { ObjectUnsubscribedError } from './util/ObjectUnsubscribedError';
|
---|
| 6 |
|
---|
| 7 | /**
|
---|
| 8 | * A variant of Subject that requires an initial value and emits its current
|
---|
| 9 | * value whenever it is subscribed to.
|
---|
| 10 | *
|
---|
| 11 | * @class BehaviorSubject<T>
|
---|
| 12 | */
|
---|
| 13 | export class BehaviorSubject<T> extends Subject<T> {
|
---|
| 14 |
|
---|
| 15 | constructor(private _value: T) {
|
---|
| 16 | super();
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | get value(): T {
|
---|
| 20 | return this.getValue();
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | /** @deprecated This is an internal implementation detail, do not use. */
|
---|
| 24 | _subscribe(subscriber: Subscriber<T>): Subscription {
|
---|
| 25 | const subscription = super._subscribe(subscriber);
|
---|
| 26 | if (subscription && !(<SubscriptionLike>subscription).closed) {
|
---|
| 27 | subscriber.next(this._value);
|
---|
| 28 | }
|
---|
| 29 | return subscription;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | getValue(): T {
|
---|
| 33 | if (this.hasError) {
|
---|
| 34 | throw this.thrownError;
|
---|
| 35 | } else if (this.closed) {
|
---|
| 36 | throw new ObjectUnsubscribedError();
|
---|
| 37 | } else {
|
---|
| 38 | return this._value;
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | next(value: T): void {
|
---|
| 43 | super.next(this._value = value);
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.