IfObservable.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { Observable } from '../Observable';
  2. import { subscribeToResult } from '../util/subscribeToResult';
  3. import { OuterSubscriber } from '../OuterSubscriber';
  4. /**
  5. * We need this JSDoc comment for affecting ESDoc.
  6. * @extends {Ignored}
  7. * @hide true
  8. */
  9. export class IfObservable extends Observable {
  10. constructor(condition, thenSource, elseSource) {
  11. super();
  12. this.condition = condition;
  13. this.thenSource = thenSource;
  14. this.elseSource = elseSource;
  15. }
  16. static create(condition, thenSource, elseSource) {
  17. return new IfObservable(condition, thenSource, elseSource);
  18. }
  19. /** @deprecated internal use only */ _subscribe(subscriber) {
  20. const { condition, thenSource, elseSource } = this;
  21. return new IfSubscriber(subscriber, condition, thenSource, elseSource);
  22. }
  23. }
  24. class IfSubscriber extends OuterSubscriber {
  25. constructor(destination, condition, thenSource, elseSource) {
  26. super(destination);
  27. this.condition = condition;
  28. this.thenSource = thenSource;
  29. this.elseSource = elseSource;
  30. this.tryIf();
  31. }
  32. tryIf() {
  33. const { condition, thenSource, elseSource } = this;
  34. let result;
  35. try {
  36. result = condition();
  37. const source = result ? thenSource : elseSource;
  38. if (source) {
  39. this.add(subscribeToResult(this, source));
  40. }
  41. else {
  42. this._complete();
  43. }
  44. }
  45. catch (err) {
  46. this._error(err);
  47. }
  48. }
  49. }
  50. //# sourceMappingURL=IfObservable.js.map