Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AsyncAction.js 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /** PURE_IMPORTS_START tslib,_Action PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Action } from './Action';
  4. var AsyncAction = /*@__PURE__*/ (function (_super) {
  5. tslib_1.__extends(AsyncAction, _super);
  6. function AsyncAction(scheduler, work) {
  7. var _this = _super.call(this, scheduler, work) || this;
  8. _this.scheduler = scheduler;
  9. _this.work = work;
  10. _this.pending = false;
  11. return _this;
  12. }
  13. AsyncAction.prototype.schedule = function (state, delay) {
  14. if (delay === void 0) {
  15. delay = 0;
  16. }
  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 = this.id || this.requestAsyncId(scheduler, this.id, delay);
  29. return this;
  30. };
  31. AsyncAction.prototype.requestAsyncId = function (scheduler, id, delay) {
  32. if (delay === void 0) {
  33. delay = 0;
  34. }
  35. return setInterval(scheduler.flush.bind(scheduler, this), delay);
  36. };
  37. AsyncAction.prototype.recycleAsyncId = function (scheduler, id, delay) {
  38. if (delay === void 0) {
  39. delay = 0;
  40. }
  41. if (delay !== null && this.delay === delay && this.pending === false) {
  42. return id;
  43. }
  44. clearInterval(id);
  45. };
  46. AsyncAction.prototype.execute = function (state, delay) {
  47. if (this.closed) {
  48. return new Error('executing a cancelled action');
  49. }
  50. this.pending = false;
  51. var error = this._execute(state, delay);
  52. if (error) {
  53. return error;
  54. }
  55. else if (this.pending === false && this.id != null) {
  56. this.id = this.recycleAsyncId(this.scheduler, this.id, null);
  57. }
  58. };
  59. AsyncAction.prototype._execute = function (state, delay) {
  60. var errored = false;
  61. var errorValue = undefined;
  62. try {
  63. this.work(state);
  64. }
  65. catch (e) {
  66. errored = true;
  67. errorValue = !!e && e || new Error(e);
  68. }
  69. if (errored) {
  70. this.unsubscribe();
  71. return errorValue;
  72. }
  73. };
  74. AsyncAction.prototype._unsubscribe = function () {
  75. var id = this.id;
  76. var scheduler = this.scheduler;
  77. var actions = scheduler.actions;
  78. var index = actions.indexOf(this);
  79. this.work = null;
  80. this.state = null;
  81. this.pending = false;
  82. this.scheduler = null;
  83. if (index !== -1) {
  84. actions.splice(index, 1);
  85. }
  86. if (id != null) {
  87. this.id = this.recycleAsyncId(scheduler, id, null);
  88. }
  89. this.delay = null;
  90. };
  91. return AsyncAction;
  92. }(Action));
  93. export { AsyncAction };
  94. //# sourceMappingURL=AsyncAction.js.map