IfObservable.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var Observable_1 = require('../Observable');
  8. var subscribeToResult_1 = require('../util/subscribeToResult');
  9. var OuterSubscriber_1 = require('../OuterSubscriber');
  10. /**
  11. * We need this JSDoc comment for affecting ESDoc.
  12. * @extends {Ignored}
  13. * @hide true
  14. */
  15. var IfObservable = (function (_super) {
  16. __extends(IfObservable, _super);
  17. function IfObservable(condition, thenSource, elseSource) {
  18. _super.call(this);
  19. this.condition = condition;
  20. this.thenSource = thenSource;
  21. this.elseSource = elseSource;
  22. }
  23. IfObservable.create = function (condition, thenSource, elseSource) {
  24. return new IfObservable(condition, thenSource, elseSource);
  25. };
  26. /** @deprecated internal use only */ IfObservable.prototype._subscribe = function (subscriber) {
  27. var _a = this, condition = _a.condition, thenSource = _a.thenSource, elseSource = _a.elseSource;
  28. return new IfSubscriber(subscriber, condition, thenSource, elseSource);
  29. };
  30. return IfObservable;
  31. }(Observable_1.Observable));
  32. exports.IfObservable = IfObservable;
  33. var IfSubscriber = (function (_super) {
  34. __extends(IfSubscriber, _super);
  35. function IfSubscriber(destination, condition, thenSource, elseSource) {
  36. _super.call(this, destination);
  37. this.condition = condition;
  38. this.thenSource = thenSource;
  39. this.elseSource = elseSource;
  40. this.tryIf();
  41. }
  42. IfSubscriber.prototype.tryIf = function () {
  43. var _a = this, condition = _a.condition, thenSource = _a.thenSource, elseSource = _a.elseSource;
  44. var result;
  45. try {
  46. result = condition();
  47. var source = result ? thenSource : elseSource;
  48. if (source) {
  49. this.add(subscribeToResult_1.subscribeToResult(this, source));
  50. }
  51. else {
  52. this._complete();
  53. }
  54. }
  55. catch (err) {
  56. this._error(err);
  57. }
  58. };
  59. return IfSubscriber;
  60. }(OuterSubscriber_1.OuterSubscriber));
  61. //# sourceMappingURL=IfObservable.js.map