CancellationToken.js 3.1 KB

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