a zip code crypto-currency system good for red ONLY

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. "use strict";
  2. var isArray_1 = require('./util/isArray');
  3. var isObject_1 = require('./util/isObject');
  4. var isFunction_1 = require('./util/isFunction');
  5. var tryCatch_1 = require('./util/tryCatch');
  6. var errorObject_1 = require('./util/errorObject');
  7. var UnsubscriptionError_1 = require('./util/UnsubscriptionError');
  8. /**
  9. * Represents a disposable resource, such as the execution of an Observable. A
  10. * Subscription has one important method, `unsubscribe`, that takes no argument
  11. * and just disposes the resource held by the subscription.
  12. *
  13. * Additionally, subscriptions may be grouped together through the `add()`
  14. * method, which will attach a child Subscription to the current Subscription.
  15. * When a Subscription is unsubscribed, all its children (and its grandchildren)
  16. * will be unsubscribed as well.
  17. *
  18. * @class Subscription
  19. */
  20. var Subscription = (function () {
  21. /**
  22. * @param {function(): void} [unsubscribe] A function describing how to
  23. * perform the disposal of resources when the `unsubscribe` method is called.
  24. */
  25. function Subscription(unsubscribe) {
  26. /**
  27. * A flag to indicate whether this Subscription has already been unsubscribed.
  28. * @type {boolean}
  29. */
  30. this.closed = false;
  31. this._parent = null;
  32. this._parents = null;
  33. this._subscriptions = null;
  34. if (unsubscribe) {
  35. this._unsubscribe = unsubscribe;
  36. }
  37. }
  38. /**
  39. * Disposes the resources held by the subscription. May, for instance, cancel
  40. * an ongoing Observable execution or cancel any other type of work that
  41. * started when the Subscription was created.
  42. * @return {void}
  43. */
  44. Subscription.prototype.unsubscribe = function () {
  45. var hasErrors = false;
  46. var errors;
  47. if (this.closed) {
  48. return;
  49. }
  50. var _a = this, _parent = _a._parent, _parents = _a._parents, _unsubscribe = _a._unsubscribe, _subscriptions = _a._subscriptions;
  51. this.closed = true;
  52. this._parent = null;
  53. this._parents = null;
  54. // null out _subscriptions first so any child subscriptions that attempt
  55. // to remove themselves from this subscription will noop
  56. this._subscriptions = null;
  57. var index = -1;
  58. var len = _parents ? _parents.length : 0;
  59. // if this._parent is null, then so is this._parents, and we
  60. // don't have to remove ourselves from any parent subscriptions.
  61. while (_parent) {
  62. _parent.remove(this);
  63. // if this._parents is null or index >= len,
  64. // then _parent is set to null, and the loop exits
  65. _parent = ++index < len && _parents[index] || null;
  66. }
  67. if (isFunction_1.isFunction(_unsubscribe)) {
  68. var trial = tryCatch_1.tryCatch(_unsubscribe).call(this);
  69. if (trial === errorObject_1.errorObject) {
  70. hasErrors = true;
  71. errors = errors || (errorObject_1.errorObject.e instanceof UnsubscriptionError_1.UnsubscriptionError ?
  72. flattenUnsubscriptionErrors(errorObject_1.errorObject.e.errors) : [errorObject_1.errorObject.e]);
  73. }
  74. }
  75. if (isArray_1.isArray(_subscriptions)) {
  76. index = -1;
  77. len = _subscriptions.length;
  78. while (++index < len) {
  79. var sub = _subscriptions[index];
  80. if (isObject_1.isObject(sub)) {
  81. var trial = tryCatch_1.tryCatch(sub.unsubscribe).call(sub);
  82. if (trial === errorObject_1.errorObject) {
  83. hasErrors = true;
  84. errors = errors || [];
  85. var err = errorObject_1.errorObject.e;
  86. if (err instanceof UnsubscriptionError_1.UnsubscriptionError) {
  87. errors = errors.concat(flattenUnsubscriptionErrors(err.errors));
  88. }
  89. else {
  90. errors.push(err);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. if (hasErrors) {
  97. throw new UnsubscriptionError_1.UnsubscriptionError(errors);
  98. }
  99. };
  100. /**
  101. * Adds a tear down to be called during the unsubscribe() of this
  102. * Subscription.
  103. *
  104. * If the tear down being added is a subscription that is already
  105. * unsubscribed, is the same reference `add` is being called on, or is
  106. * `Subscription.EMPTY`, it will not be added.
  107. *
  108. * If this subscription is already in an `closed` state, the passed
  109. * tear down logic will be executed immediately.
  110. *
  111. * @param {TeardownLogic} teardown The additional logic to execute on
  112. * teardown.
  113. * @return {Subscription} Returns the Subscription used or created to be
  114. * added to the inner subscriptions list. This Subscription can be used with
  115. * `remove()` to remove the passed teardown logic from the inner subscriptions
  116. * list.
  117. */
  118. Subscription.prototype.add = function (teardown) {
  119. if (!teardown || (teardown === Subscription.EMPTY)) {
  120. return Subscription.EMPTY;
  121. }
  122. if (teardown === this) {
  123. return this;
  124. }
  125. var subscription = teardown;
  126. switch (typeof teardown) {
  127. case 'function':
  128. subscription = new Subscription(teardown);
  129. case 'object':
  130. if (subscription.closed || typeof subscription.unsubscribe !== 'function') {
  131. return subscription;
  132. }
  133. else if (this.closed) {
  134. subscription.unsubscribe();
  135. return subscription;
  136. }
  137. else if (typeof subscription._addParent !== 'function' /* quack quack */) {
  138. var tmp = subscription;
  139. subscription = new Subscription();
  140. subscription._subscriptions = [tmp];
  141. }
  142. break;
  143. default:
  144. throw new Error('unrecognized teardown ' + teardown + ' added to Subscription.');
  145. }
  146. var subscriptions = this._subscriptions || (this._subscriptions = []);
  147. subscriptions.push(subscription);
  148. subscription._addParent(this);
  149. return subscription;
  150. };
  151. /**
  152. * Removes a Subscription from the internal list of subscriptions that will
  153. * unsubscribe during the unsubscribe process of this Subscription.
  154. * @param {Subscription} subscription The subscription to remove.
  155. * @return {void}
  156. */
  157. Subscription.prototype.remove = function (subscription) {
  158. var subscriptions = this._subscriptions;
  159. if (subscriptions) {
  160. var subscriptionIndex = subscriptions.indexOf(subscription);
  161. if (subscriptionIndex !== -1) {
  162. subscriptions.splice(subscriptionIndex, 1);
  163. }
  164. }
  165. };
  166. Subscription.prototype._addParent = function (parent) {
  167. var _a = this, _parent = _a._parent, _parents = _a._parents;
  168. if (!_parent || _parent === parent) {
  169. // If we don't have a parent, or the new parent is the same as the
  170. // current parent, then set this._parent to the new parent.
  171. this._parent = parent;
  172. }
  173. else if (!_parents) {
  174. // If there's already one parent, but not multiple, allocate an Array to
  175. // store the rest of the parent Subscriptions.
  176. this._parents = [parent];
  177. }
  178. else if (_parents.indexOf(parent) === -1) {
  179. // Only add the new parent to the _parents list if it's not already there.
  180. _parents.push(parent);
  181. }
  182. };
  183. Subscription.EMPTY = (function (empty) {
  184. empty.closed = true;
  185. return empty;
  186. }(new Subscription()));
  187. return Subscription;
  188. }());
  189. exports.Subscription = Subscription;
  190. function flattenUnsubscriptionErrors(errors) {
  191. return errors.reduce(function (errs, err) { return errs.concat((err instanceof UnsubscriptionError_1.UnsubscriptionError) ? err.errors : err); }, []);
  192. }
  193. //# sourceMappingURL=Subscription.js.map