cancel.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. "use strict";
  2. module.exports = function(Promise, PromiseArray, apiRejection, debug) {
  3. var util = require("./util");
  4. var tryCatch = util.tryCatch;
  5. var errorObj = util.errorObj;
  6. var async = Promise._async;
  7. Promise.prototype["break"] = Promise.prototype.cancel = function() {
  8. if (!debug.cancellation()) return this._warn("cancellation is disabled");
  9. var promise = this;
  10. var child = promise;
  11. while (promise.isCancellable()) {
  12. if (!promise._cancelBy(child)) {
  13. if (child._isFollowing()) {
  14. child._followee().cancel();
  15. } else {
  16. child._cancelBranched();
  17. }
  18. break;
  19. }
  20. var parent = promise._cancellationParent;
  21. if (parent == null || !parent.isCancellable()) {
  22. if (promise._isFollowing()) {
  23. promise._followee().cancel();
  24. } else {
  25. promise._cancelBranched();
  26. }
  27. break;
  28. } else {
  29. if (promise._isFollowing()) promise._followee().cancel();
  30. child = promise;
  31. promise = parent;
  32. }
  33. }
  34. };
  35. Promise.prototype._branchHasCancelled = function() {
  36. this._branchesRemainingToCancel--;
  37. };
  38. Promise.prototype._enoughBranchesHaveCancelled = function() {
  39. return this._branchesRemainingToCancel === undefined ||
  40. this._branchesRemainingToCancel <= 0;
  41. };
  42. Promise.prototype._cancelBy = function(canceller) {
  43. if (canceller === this) {
  44. this._branchesRemainingToCancel = 0;
  45. this._invokeOnCancel();
  46. return true;
  47. } else {
  48. this._branchHasCancelled();
  49. if (this._enoughBranchesHaveCancelled()) {
  50. this._invokeOnCancel();
  51. return true;
  52. }
  53. }
  54. return false;
  55. };
  56. Promise.prototype._cancelBranched = function() {
  57. if (this._enoughBranchesHaveCancelled()) {
  58. this._cancel();
  59. }
  60. };
  61. Promise.prototype._cancel = function() {
  62. if (!this.isCancellable()) return;
  63. this._setCancelled();
  64. async.invoke(this._cancelPromises, this, undefined);
  65. };
  66. Promise.prototype._cancelPromises = function() {
  67. if (this._length() > 0) this._settlePromises();
  68. };
  69. Promise.prototype._unsetOnCancel = function() {
  70. this._onCancelField = undefined;
  71. };
  72. Promise.prototype.isCancellable = function() {
  73. return this.isPending() && !this.isCancelled();
  74. };
  75. Promise.prototype._doInvokeOnCancel = function(onCancelCallback, internalOnly) {
  76. if (util.isArray(onCancelCallback)) {
  77. for (var i = 0; i < onCancelCallback.length; ++i) {
  78. this._doInvokeOnCancel(onCancelCallback[i], internalOnly);
  79. }
  80. } else if (onCancelCallback !== undefined) {
  81. if (typeof onCancelCallback === "function") {
  82. if (!internalOnly) {
  83. var e = tryCatch(onCancelCallback).call(this._boundValue());
  84. if (e === errorObj) {
  85. this._attachExtraTrace(e.e);
  86. async.throwLater(e.e);
  87. }
  88. }
  89. } else {
  90. onCancelCallback._resultCancelled(this);
  91. }
  92. }
  93. };
  94. Promise.prototype._invokeOnCancel = function() {
  95. var onCancelCallback = this._onCancel();
  96. this._unsetOnCancel();
  97. async.invoke(this._doInvokeOnCancel, this, onCancelCallback);
  98. };
  99. Promise.prototype._invokeInternalOnCancel = function() {
  100. if (this.isCancellable()) {
  101. this._doInvokeOnCancel(this._onCancel(), true);
  102. this._unsetOnCancel();
  103. }
  104. };
  105. Promise.prototype._resultCancelled = function() {
  106. this.cancel();
  107. };
  108. };