Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Subscription.js 5.0KB

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