observable.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
  3. var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.Observable = exports.Subscriber = void 0;
  8. var _typeof2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/typeof"));
  9. var _assign = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/assign"));
  10. var _create = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/create"));
  11. var _setPrototypeOf = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/set-prototype-of"));
  12. var __extends = void 0 && (void 0).__extends || function () {
  13. var _extendStatics = function extendStatics(d, b) {
  14. _extendStatics = _setPrototypeOf["default"] || {
  15. __proto__: []
  16. } instanceof Array && function (d, b) {
  17. d.__proto__ = b;
  18. } || function (d, b) {
  19. for (var p in b) {
  20. if (b.hasOwnProperty(p)) d[p] = b[p];
  21. }
  22. };
  23. return _extendStatics(d, b);
  24. };
  25. return function (d, b) {
  26. _extendStatics(d, b);
  27. function __() {
  28. this.constructor = d;
  29. }
  30. d.prototype = b === null ? (0, _create["default"])(b) : (__.prototype = b.prototype, new __());
  31. };
  32. }();
  33. var __assign = void 0 && (void 0).__assign || function () {
  34. __assign = _assign["default"] || function (t) {
  35. for (var s, i = 1, n = arguments.length; i < n; i++) {
  36. s = arguments[i];
  37. for (var p in s) {
  38. if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  39. }
  40. }
  41. return t;
  42. };
  43. return __assign.apply(this, arguments);
  44. };
  45. /** 表示可清理的资源,比如 Observable 的执行 */
  46. var Subscription =
  47. /** @class */
  48. function () {
  49. function Subscription() {
  50. /** 用来标示该 Subscription 是否被取消订阅的标示位 */
  51. this.closed = false;
  52. }
  53. /** 取消 observer 的订阅 */
  54. Subscription.prototype.unsubscribe = function () {
  55. if (this.closed) {
  56. return;
  57. }
  58. this.closed = true;
  59. if (this._unsubscribe) {
  60. this._unsubscribe();
  61. }
  62. };
  63. /** 添加一个 tear down 在该 Subscription 的 unsubscribe() 期间调用 */
  64. Subscription.prototype.add = function (teardown) {
  65. this._unsubscribe = teardown;
  66. };
  67. return Subscription;
  68. }();
  69. /**
  70. * 实现 Observer 接口并且继承 Subscription 类,Observer 是消费 Observable 值的公有 API
  71. * 所有 Observers 都转化成了 Subscriber,以便提供类似 Subscription 的能力,比如 unsubscribe
  72. */
  73. var Subscriber =
  74. /** @class */
  75. function (_super) {
  76. __extends(Subscriber, _super);
  77. function Subscriber(observerOrNext, error, complete) {
  78. var _this = _super.call(this) || this;
  79. _this.isStopped = false;
  80. if (observerOrNext && (0, _typeof2["default"])(observerOrNext) === 'object') {
  81. _this.destination = observerOrNext;
  82. } else {
  83. _this.destination = __assign(__assign(__assign({}, observerOrNext && {
  84. next: observerOrNext
  85. }), error && {
  86. error: error
  87. }), complete && {
  88. complete: complete
  89. });
  90. }
  91. return _this;
  92. }
  93. Subscriber.prototype.unsubscribe = function () {
  94. if (this.closed) {
  95. return;
  96. }
  97. this.isStopped = true;
  98. _super.prototype.unsubscribe.call(this);
  99. };
  100. Subscriber.prototype.next = function (value) {
  101. if (!this.isStopped && this.destination.next) {
  102. this.destination.next(value);
  103. }
  104. };
  105. Subscriber.prototype.error = function (err) {
  106. if (!this.isStopped && this.destination.error) {
  107. this.isStopped = true;
  108. this.destination.error(err);
  109. }
  110. };
  111. Subscriber.prototype.complete = function (result) {
  112. if (!this.isStopped && this.destination.complete) {
  113. this.isStopped = true;
  114. this.destination.complete(result);
  115. }
  116. };
  117. return Subscriber;
  118. }(Subscription);
  119. exports.Subscriber = Subscriber;
  120. /** 可观察对象,当前的上传事件的集合 */
  121. var Observable =
  122. /** @class */
  123. function () {
  124. function Observable(_subscribe) {
  125. this._subscribe = _subscribe;
  126. }
  127. Observable.prototype.subscribe = function (observerOrNext, error, complete) {
  128. var sink = new Subscriber(observerOrNext, error, complete);
  129. sink.add(this._subscribe(sink));
  130. return sink;
  131. };
  132. return Observable;
  133. }();
  134. exports.Observable = Observable;