AsyncAction.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { __extends } from "tslib";
  2. import { Action } from './Action';
  3. import { intervalProvider } from './intervalProvider';
  4. import { arrRemove } from '../util/arrRemove';
  5. var AsyncAction = (function (_super) {
  6. __extends(AsyncAction, _super);
  7. function AsyncAction(scheduler, work) {
  8. var _this = _super.call(this, scheduler, work) || this;
  9. _this.scheduler = scheduler;
  10. _this.work = work;
  11. _this.pending = false;
  12. return _this;
  13. }
  14. AsyncAction.prototype.schedule = function (state, delay) {
  15. var _a;
  16. if (delay === void 0) { delay = 0; }
  17. if (this.closed) {
  18. return this;
  19. }
  20. this.state = state;
  21. var id = this.id;
  22. var scheduler = this.scheduler;
  23. if (id != null) {
  24. this.id = this.recycleAsyncId(scheduler, id, delay);
  25. }
  26. this.pending = true;
  27. this.delay = delay;
  28. this.id = (_a = this.id) !== null && _a !== void 0 ? _a : this.requestAsyncId(scheduler, this.id, delay);
  29. return this;
  30. };
  31. AsyncAction.prototype.requestAsyncId = function (scheduler, _id, delay) {
  32. if (delay === void 0) { delay = 0; }
  33. return intervalProvider.setInterval(scheduler.flush.bind(scheduler, this), delay);
  34. };
  35. AsyncAction.prototype.recycleAsyncId = function (_scheduler, id, delay) {
  36. if (delay === void 0) { delay = 0; }
  37. if (delay != null && this.delay === delay && this.pending === false) {
  38. return id;
  39. }
  40. if (id != null) {
  41. intervalProvider.clearInterval(id);
  42. }
  43. return undefined;
  44. };
  45. AsyncAction.prototype.execute = function (state, delay) {
  46. if (this.closed) {
  47. return new Error('executing a cancelled action');
  48. }
  49. this.pending = false;
  50. var error = this._execute(state, delay);
  51. if (error) {
  52. return error;
  53. }
  54. else if (this.pending === false && this.id != null) {
  55. this.id = this.recycleAsyncId(this.scheduler, this.id, null);
  56. }
  57. };
  58. AsyncAction.prototype._execute = function (state, _delay) {
  59. var errored = false;
  60. var errorValue;
  61. try {
  62. this.work(state);
  63. }
  64. catch (e) {
  65. errored = true;
  66. errorValue = e ? e : new Error('Scheduled action threw falsy error');
  67. }
  68. if (errored) {
  69. this.unsubscribe();
  70. return errorValue;
  71. }
  72. };
  73. AsyncAction.prototype.unsubscribe = function () {
  74. if (!this.closed) {
  75. var _a = this, id = _a.id, scheduler = _a.scheduler;
  76. var actions = scheduler.actions;
  77. this.work = this.state = this.scheduler = null;
  78. this.pending = false;
  79. arrRemove(actions, this);
  80. if (id != null) {
  81. this.id = this.recycleAsyncId(scheduler, id, null);
  82. }
  83. this.delay = null;
  84. _super.prototype.unsubscribe.call(this);
  85. }
  86. };
  87. return AsyncAction;
  88. }(Action));
  89. export { AsyncAction };
  90. //# sourceMappingURL=AsyncAction.js.map