Subscription.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var isArray_1 = require("./util/isArray");
  4. var isObject_1 = require("./util/isObject");
  5. var isFunction_1 = require("./util/isFunction");
  6. var UnsubscriptionError_1 = require("./util/UnsubscriptionError");
  7. var Subscription = (function () {
  8. function Subscription(unsubscribe) {
  9. this.closed = false;
  10. this._parent = null;
  11. this._parents = null;
  12. this._subscriptions = null;
  13. if (unsubscribe) {
  14. this._unsubscribe = unsubscribe;
  15. }
  16. }
  17. Subscription.prototype.unsubscribe = function () {
  18. var hasErrors = false;
  19. var errors;
  20. if (this.closed) {
  21. return;
  22. }
  23. var _a = this, _parent = _a._parent, _parents = _a._parents, _unsubscribe = _a._unsubscribe, _subscriptions = _a._subscriptions;
  24. this.closed = true;
  25. this._parent = null;
  26. this._parents = null;
  27. this._subscriptions = null;
  28. var index = -1;
  29. var len = _parents ? _parents.length : 0;
  30. while (_parent) {
  31. _parent.remove(this);
  32. _parent = ++index < len && _parents[index] || null;
  33. }
  34. if (isFunction_1.isFunction(_unsubscribe)) {
  35. try {
  36. _unsubscribe.call(this);
  37. }
  38. catch (e) {
  39. hasErrors = true;
  40. errors = e instanceof UnsubscriptionError_1.UnsubscriptionError ? flattenUnsubscriptionErrors(e.errors) : [e];
  41. }
  42. }
  43. if (isArray_1.isArray(_subscriptions)) {
  44. index = -1;
  45. len = _subscriptions.length;
  46. while (++index < len) {
  47. var sub = _subscriptions[index];
  48. if (isObject_1.isObject(sub)) {
  49. try {
  50. sub.unsubscribe();
  51. }
  52. catch (e) {
  53. hasErrors = true;
  54. errors = errors || [];
  55. if (e instanceof UnsubscriptionError_1.UnsubscriptionError) {
  56. errors = errors.concat(flattenUnsubscriptionErrors(e.errors));
  57. }
  58. else {
  59. errors.push(e);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. if (hasErrors) {
  66. throw new UnsubscriptionError_1.UnsubscriptionError(errors);
  67. }
  68. };
  69. Subscription.prototype.add = function (teardown) {
  70. var subscription = teardown;
  71. switch (typeof teardown) {
  72. case 'function':
  73. subscription = new Subscription(teardown);
  74. case 'object':
  75. if (subscription === this || subscription.closed || typeof subscription.unsubscribe !== 'function') {
  76. return subscription;
  77. }
  78. else if (this.closed) {
  79. subscription.unsubscribe();
  80. return subscription;
  81. }
  82. else if (!(subscription instanceof Subscription)) {
  83. var tmp = subscription;
  84. subscription = new Subscription();
  85. subscription._subscriptions = [tmp];
  86. }
  87. break;
  88. default: {
  89. if (!teardown) {
  90. return Subscription.EMPTY;
  91. }
  92. throw new Error('unrecognized teardown ' + teardown + ' added to Subscription.');
  93. }
  94. }
  95. if (subscription._addParent(this)) {
  96. var subscriptions = this._subscriptions;
  97. if (subscriptions) {
  98. subscriptions.push(subscription);
  99. }
  100. else {
  101. this._subscriptions = [subscription];
  102. }
  103. }
  104. return subscription;
  105. };
  106. Subscription.prototype.remove = function (subscription) {
  107. var subscriptions = this._subscriptions;
  108. if (subscriptions) {
  109. var subscriptionIndex = subscriptions.indexOf(subscription);
  110. if (subscriptionIndex !== -1) {
  111. subscriptions.splice(subscriptionIndex, 1);
  112. }
  113. }
  114. };
  115. Subscription.prototype._addParent = function (parent) {
  116. var _a = this, _parent = _a._parent, _parents = _a._parents;
  117. if (_parent === parent) {
  118. return false;
  119. }
  120. else if (!_parent) {
  121. this._parent = parent;
  122. return true;
  123. }
  124. else if (!_parents) {
  125. this._parents = [parent];
  126. return true;
  127. }
  128. else if (_parents.indexOf(parent) === -1) {
  129. _parents.push(parent);
  130. return true;
  131. }
  132. return false;
  133. };
  134. Subscription.EMPTY = (function (empty) {
  135. empty.closed = true;
  136. return empty;
  137. }(new Subscription()));
  138. return Subscription;
  139. }());
  140. exports.Subscription = Subscription;
  141. function flattenUnsubscriptionErrors(errors) {
  142. return errors.reduce(function (errs, err) { return errs.concat((err instanceof UnsubscriptionError_1.UnsubscriptionError) ? err.errors : err); }, []);
  143. }
  144. //# sourceMappingURL=Subscription.js.map