AsyncAction.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.AsyncAction = void 0;
  19. var Action_1 = require("./Action");
  20. var intervalProvider_1 = require("./intervalProvider");
  21. var arrRemove_1 = require("../util/arrRemove");
  22. var AsyncAction = (function (_super) {
  23. __extends(AsyncAction, _super);
  24. function AsyncAction(scheduler, work) {
  25. var _this = _super.call(this, scheduler, work) || this;
  26. _this.scheduler = scheduler;
  27. _this.work = work;
  28. _this.pending = false;
  29. return _this;
  30. }
  31. AsyncAction.prototype.schedule = function (state, delay) {
  32. var _a;
  33. if (delay === void 0) { delay = 0; }
  34. if (this.closed) {
  35. return this;
  36. }
  37. this.state = state;
  38. var id = this.id;
  39. var scheduler = this.scheduler;
  40. if (id != null) {
  41. this.id = this.recycleAsyncId(scheduler, id, delay);
  42. }
  43. this.pending = true;
  44. this.delay = delay;
  45. this.id = (_a = this.id) !== null && _a !== void 0 ? _a : this.requestAsyncId(scheduler, this.id, delay);
  46. return this;
  47. };
  48. AsyncAction.prototype.requestAsyncId = function (scheduler, _id, delay) {
  49. if (delay === void 0) { delay = 0; }
  50. return intervalProvider_1.intervalProvider.setInterval(scheduler.flush.bind(scheduler, this), delay);
  51. };
  52. AsyncAction.prototype.recycleAsyncId = function (_scheduler, id, delay) {
  53. if (delay === void 0) { delay = 0; }
  54. if (delay != null && this.delay === delay && this.pending === false) {
  55. return id;
  56. }
  57. if (id != null) {
  58. intervalProvider_1.intervalProvider.clearInterval(id);
  59. }
  60. return undefined;
  61. };
  62. AsyncAction.prototype.execute = function (state, delay) {
  63. if (this.closed) {
  64. return new Error('executing a cancelled action');
  65. }
  66. this.pending = false;
  67. var error = this._execute(state, delay);
  68. if (error) {
  69. return error;
  70. }
  71. else if (this.pending === false && this.id != null) {
  72. this.id = this.recycleAsyncId(this.scheduler, this.id, null);
  73. }
  74. };
  75. AsyncAction.prototype._execute = function (state, _delay) {
  76. var errored = false;
  77. var errorValue;
  78. try {
  79. this.work(state);
  80. }
  81. catch (e) {
  82. errored = true;
  83. errorValue = e ? e : new Error('Scheduled action threw falsy error');
  84. }
  85. if (errored) {
  86. this.unsubscribe();
  87. return errorValue;
  88. }
  89. };
  90. AsyncAction.prototype.unsubscribe = function () {
  91. if (!this.closed) {
  92. var _a = this, id = _a.id, scheduler = _a.scheduler;
  93. var actions = scheduler.actions;
  94. this.work = this.state = this.scheduler = null;
  95. this.pending = false;
  96. arrRemove_1.arrRemove(actions, this);
  97. if (id != null) {
  98. this.id = this.recycleAsyncId(scheduler, id, null);
  99. }
  100. this.delay = null;
  101. _super.prototype.unsubscribe.call(this);
  102. }
  103. };
  104. return AsyncAction;
  105. }(Action_1.Action));
  106. exports.AsyncAction = AsyncAction;
  107. //# sourceMappingURL=AsyncAction.js.map