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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. }
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var Action_1 = require("./Action");
  17. var AsyncAction = (function (_super) {
  18. __extends(AsyncAction, _super);
  19. function AsyncAction(scheduler, work) {
  20. var _this = _super.call(this, scheduler, work) || this;
  21. _this.scheduler = scheduler;
  22. _this.work = work;
  23. _this.pending = false;
  24. return _this;
  25. }
  26. AsyncAction.prototype.schedule = function (state, delay) {
  27. if (delay === void 0) { delay = 0; }
  28. if (this.closed) {
  29. return this;
  30. }
  31. this.state = state;
  32. var id = this.id;
  33. var scheduler = this.scheduler;
  34. if (id != null) {
  35. this.id = this.recycleAsyncId(scheduler, id, delay);
  36. }
  37. this.pending = true;
  38. this.delay = delay;
  39. this.id = this.id || this.requestAsyncId(scheduler, this.id, delay);
  40. return this;
  41. };
  42. AsyncAction.prototype.requestAsyncId = function (scheduler, id, delay) {
  43. if (delay === void 0) { delay = 0; }
  44. return setInterval(scheduler.flush.bind(scheduler, this), delay);
  45. };
  46. AsyncAction.prototype.recycleAsyncId = function (scheduler, id, delay) {
  47. if (delay === void 0) { delay = 0; }
  48. if (delay !== null && this.delay === delay && this.pending === false) {
  49. return id;
  50. }
  51. clearInterval(id);
  52. };
  53. AsyncAction.prototype.execute = function (state, delay) {
  54. if (this.closed) {
  55. return new Error('executing a cancelled action');
  56. }
  57. this.pending = false;
  58. var error = this._execute(state, delay);
  59. if (error) {
  60. return error;
  61. }
  62. else if (this.pending === false && this.id != null) {
  63. this.id = this.recycleAsyncId(this.scheduler, this.id, null);
  64. }
  65. };
  66. AsyncAction.prototype._execute = function (state, delay) {
  67. var errored = false;
  68. var errorValue = undefined;
  69. try {
  70. this.work(state);
  71. }
  72. catch (e) {
  73. errored = true;
  74. errorValue = !!e && e || new Error(e);
  75. }
  76. if (errored) {
  77. this.unsubscribe();
  78. return errorValue;
  79. }
  80. };
  81. AsyncAction.prototype._unsubscribe = function () {
  82. var id = this.id;
  83. var scheduler = this.scheduler;
  84. var actions = scheduler.actions;
  85. var index = actions.indexOf(this);
  86. this.work = null;
  87. this.state = null;
  88. this.pending = false;
  89. this.scheduler = null;
  90. if (index !== -1) {
  91. actions.splice(index, 1);
  92. }
  93. if (id != null) {
  94. this.id = this.recycleAsyncId(scheduler, id, null);
  95. }
  96. this.delay = null;
  97. };
  98. return AsyncAction;
  99. }(Action_1.Action));
  100. exports.AsyncAction = AsyncAction;
  101. //# sourceMappingURL=AsyncAction.js.map