Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

BehaviorSubject.ts 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. * A variant of Subject that requires an initial value and emits its current
  8. * value whenever it is subscribed to.
  9. *
  10. * @class BehaviorSubject<T>
  11. */
  12. export class BehaviorSubject<T> extends Subject<T> {
  13. constructor(private _value: T) {
  14. super();
  15. }
  16. get value(): T {
  17. return this.getValue();
  18. }
  19. /** @deprecated This is an internal implementation detail, do not use. */
  20. _subscribe(subscriber: Subscriber<T>): Subscription {
  21. const subscription = super._subscribe(subscriber);
  22. if (subscription && !(<SubscriptionLike>subscription).closed) {
  23. subscriber.next(this._value);
  24. }
  25. return subscription;
  26. }
  27. getValue(): T {
  28. if (this.hasError) {
  29. throw this.thrownError;
  30. } else if (this.closed) {
  31. throw new ObjectUnsubscribedError();
  32. } else {
  33. return this._value;
  34. }
  35. }
  36. next(value: T): void {
  37. super.next(this._value = value);
  38. }
  39. }