Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AsyncSubject.ts 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { Subject } from './Subject';
  2. import { Subscriber } from './Subscriber';
  3. import { Subscription } from './Subscription';
  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. * @class AsyncSubject<T>
  9. */
  10. export class AsyncSubject<T> extends Subject<T> {
  11. private value: T = null;
  12. private hasNext: boolean = false;
  13. private hasCompleted: boolean = false;
  14. /** @deprecated This is an internal implementation detail, do not use. */
  15. _subscribe(subscriber: Subscriber<any>): Subscription {
  16. if (this.hasError) {
  17. subscriber.error(this.thrownError);
  18. return Subscription.EMPTY;
  19. } else if (this.hasCompleted && this.hasNext) {
  20. subscriber.next(this.value);
  21. subscriber.complete();
  22. return Subscription.EMPTY;
  23. }
  24. return super._subscribe(subscriber);
  25. }
  26. next(value: T): void {
  27. if (!this.hasCompleted) {
  28. this.value = value;
  29. this.hasNext = true;
  30. }
  31. }
  32. error(error: any): void {
  33. if (!this.hasCompleted) {
  34. super.error(error);
  35. }
  36. }
  37. complete(): void {
  38. this.hasCompleted = true;
  39. if (this.hasNext) {
  40. super.next(this.value);
  41. }
  42. super.complete();
  43. }
  44. }