UsingObservable.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 UsingObservable = (function (_super) {
  16. __extends(UsingObservable, _super);
  17. function UsingObservable(resourceFactory, observableFactory) {
  18. _super.call(this);
  19. this.resourceFactory = resourceFactory;
  20. this.observableFactory = observableFactory;
  21. }
  22. UsingObservable.create = function (resourceFactory, observableFactory) {
  23. return new UsingObservable(resourceFactory, observableFactory);
  24. };
  25. /** @deprecated internal use only */ UsingObservable.prototype._subscribe = function (subscriber) {
  26. var _a = this, resourceFactory = _a.resourceFactory, observableFactory = _a.observableFactory;
  27. var resource;
  28. try {
  29. resource = resourceFactory();
  30. return new UsingSubscriber(subscriber, resource, observableFactory);
  31. }
  32. catch (err) {
  33. subscriber.error(err);
  34. }
  35. };
  36. return UsingObservable;
  37. }(Observable_1.Observable));
  38. exports.UsingObservable = UsingObservable;
  39. var UsingSubscriber = (function (_super) {
  40. __extends(UsingSubscriber, _super);
  41. function UsingSubscriber(destination, resource, observableFactory) {
  42. _super.call(this, destination);
  43. this.resource = resource;
  44. this.observableFactory = observableFactory;
  45. destination.add(resource);
  46. this.tryUse();
  47. }
  48. UsingSubscriber.prototype.tryUse = function () {
  49. try {
  50. var source = this.observableFactory.call(this, this.resource);
  51. if (source) {
  52. this.add(subscribeToResult_1.subscribeToResult(this, source));
  53. }
  54. }
  55. catch (err) {
  56. this._error(err);
  57. }
  58. };
  59. return UsingSubscriber;
  60. }(OuterSubscriber_1.OuterSubscriber));
  61. //# sourceMappingURL=UsingObservable.js.map