CancellationToken.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.CancellationError = exports.CancellationToken = void 0;
  6. function _bluebirdLst() {
  7. const data = _interopRequireDefault(require("bluebird-lst"));
  8. _bluebirdLst = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _events() {
  14. const data = require("events");
  15. _events = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  21. class CancellationToken extends _events().EventEmitter {
  22. // babel cannot compile ... correctly for super calls
  23. constructor(parent) {
  24. super();
  25. this.parentCancelHandler = null;
  26. this._parent = null;
  27. this._cancelled = false;
  28. if (parent != null) {
  29. this.parent = parent;
  30. }
  31. }
  32. get cancelled() {
  33. return this._cancelled || this._parent != null && this._parent.cancelled;
  34. }
  35. set parent(value) {
  36. this.removeParentCancelHandler();
  37. this._parent = value;
  38. this.parentCancelHandler = () => this.cancel();
  39. this._parent.onCancel(this.parentCancelHandler);
  40. }
  41. cancel() {
  42. this._cancelled = true;
  43. this.emit("cancel");
  44. }
  45. onCancel(handler) {
  46. if (this.cancelled) {
  47. handler();
  48. } else {
  49. this.once("cancel", handler);
  50. }
  51. }
  52. createPromise(callback) {
  53. if (this.cancelled) {
  54. return Promise.reject(new CancellationError());
  55. }
  56. let cancelHandler = null;
  57. return new (_bluebirdLst().default)((resolve, reject) => {
  58. let addedCancelHandler = null;
  59. cancelHandler = () => {
  60. try {
  61. if (addedCancelHandler != null) {
  62. addedCancelHandler();
  63. addedCancelHandler = null;
  64. }
  65. } finally {
  66. reject(new CancellationError());
  67. }
  68. };
  69. if (this.cancelled) {
  70. cancelHandler();
  71. return;
  72. }
  73. this.onCancel(cancelHandler);
  74. callback(resolve, reject, callback => {
  75. addedCancelHandler = callback;
  76. });
  77. }).finally(() => {
  78. if (cancelHandler != null) {
  79. this.removeListener("cancel", cancelHandler);
  80. cancelHandler = null;
  81. }
  82. });
  83. }
  84. removeParentCancelHandler() {
  85. const parent = this._parent;
  86. if (parent != null && this.parentCancelHandler != null) {
  87. parent.removeListener("cancel", this.parentCancelHandler);
  88. this.parentCancelHandler = null;
  89. }
  90. }
  91. dispose() {
  92. try {
  93. this.removeParentCancelHandler();
  94. } finally {
  95. this.removeAllListeners();
  96. this._parent = null;
  97. }
  98. }
  99. }
  100. exports.CancellationToken = CancellationToken;
  101. class CancellationError extends Error {
  102. constructor() {
  103. super("Cancelled");
  104. }
  105. } exports.CancellationError = CancellationError;
  106. //# sourceMappingURL=CancellationToken.js.map