observable.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = function (d, b) {
  3. extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return extendStatics(d, b);
  7. };
  8. return function (d, b) {
  9. extendStatics(d, b);
  10. function __() { this.constructor = d; }
  11. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  12. };
  13. })();
  14. var __assign = (this && this.__assign) || function () {
  15. __assign = Object.assign || function(t) {
  16. for (var s, i = 1, n = arguments.length; i < n; i++) {
  17. s = arguments[i];
  18. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  19. t[p] = s[p];
  20. }
  21. return t;
  22. };
  23. return __assign.apply(this, arguments);
  24. };
  25. /** 表示可清理的资源,比如 Observable 的执行 */
  26. var Subscription = /** @class */ (function () {
  27. function Subscription() {
  28. /** 用来标示该 Subscription 是否被取消订阅的标示位 */
  29. this.closed = false;
  30. }
  31. /** 取消 observer 的订阅 */
  32. Subscription.prototype.unsubscribe = function () {
  33. if (this.closed) {
  34. return;
  35. }
  36. this.closed = true;
  37. if (this._unsubscribe) {
  38. this._unsubscribe();
  39. }
  40. };
  41. /** 添加一个 tear down 在该 Subscription 的 unsubscribe() 期间调用 */
  42. Subscription.prototype.add = function (teardown) {
  43. this._unsubscribe = teardown;
  44. };
  45. return Subscription;
  46. }());
  47. /**
  48. * 实现 Observer 接口并且继承 Subscription 类,Observer 是消费 Observable 值的公有 API
  49. * 所有 Observers 都转化成了 Subscriber,以便提供类似 Subscription 的能力,比如 unsubscribe
  50. */
  51. var Subscriber = /** @class */ (function (_super) {
  52. __extends(Subscriber, _super);
  53. function Subscriber(observerOrNext, error, complete) {
  54. var _this = _super.call(this) || this;
  55. _this.isStopped = false;
  56. if (observerOrNext && typeof observerOrNext === 'object') {
  57. _this.destination = observerOrNext;
  58. }
  59. else {
  60. _this.destination = __assign(__assign(__assign({}, observerOrNext && { next: observerOrNext }), error && { error: error }), complete && { complete: complete });
  61. }
  62. return _this;
  63. }
  64. Subscriber.prototype.unsubscribe = function () {
  65. if (this.closed) {
  66. return;
  67. }
  68. this.isStopped = true;
  69. _super.prototype.unsubscribe.call(this);
  70. };
  71. Subscriber.prototype.next = function (value) {
  72. if (!this.isStopped && this.destination.next) {
  73. this.destination.next(value);
  74. }
  75. };
  76. Subscriber.prototype.error = function (err) {
  77. if (!this.isStopped && this.destination.error) {
  78. this.isStopped = true;
  79. this.destination.error(err);
  80. }
  81. };
  82. Subscriber.prototype.complete = function (result) {
  83. if (!this.isStopped && this.destination.complete) {
  84. this.isStopped = true;
  85. this.destination.complete(result);
  86. }
  87. };
  88. return Subscriber;
  89. }(Subscription));
  90. export { Subscriber };
  91. /** 可观察对象,当前的上传事件的集合 */
  92. var Observable = /** @class */ (function () {
  93. function Observable(_subscribe) {
  94. this._subscribe = _subscribe;
  95. }
  96. Observable.prototype.subscribe = function (observerOrNext, error, complete) {
  97. var sink = new Subscriber(observerOrNext, error, complete);
  98. sink.add(this._subscribe(sink));
  99. return sink;
  100. };
  101. return Observable;
  102. }());
  103. export { Observable };
  104. //# sourceMappingURL=observable.js.map