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.

VirtualTimeScheduler.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 AsyncAction_1 = require("./AsyncAction");
  17. var AsyncScheduler_1 = require("./AsyncScheduler");
  18. var VirtualTimeScheduler = (function (_super) {
  19. __extends(VirtualTimeScheduler, _super);
  20. function VirtualTimeScheduler(SchedulerAction, maxFrames) {
  21. if (SchedulerAction === void 0) { SchedulerAction = VirtualAction; }
  22. if (maxFrames === void 0) { maxFrames = Number.POSITIVE_INFINITY; }
  23. var _this = _super.call(this, SchedulerAction, function () { return _this.frame; }) || this;
  24. _this.maxFrames = maxFrames;
  25. _this.frame = 0;
  26. _this.index = -1;
  27. return _this;
  28. }
  29. VirtualTimeScheduler.prototype.flush = function () {
  30. var _a = this, actions = _a.actions, maxFrames = _a.maxFrames;
  31. var error, action;
  32. while ((action = actions.shift()) && (this.frame = action.delay) <= maxFrames) {
  33. if (error = action.execute(action.state, action.delay)) {
  34. break;
  35. }
  36. }
  37. if (error) {
  38. while (action = actions.shift()) {
  39. action.unsubscribe();
  40. }
  41. throw error;
  42. }
  43. };
  44. VirtualTimeScheduler.frameTimeFactor = 10;
  45. return VirtualTimeScheduler;
  46. }(AsyncScheduler_1.AsyncScheduler));
  47. exports.VirtualTimeScheduler = VirtualTimeScheduler;
  48. var VirtualAction = (function (_super) {
  49. __extends(VirtualAction, _super);
  50. function VirtualAction(scheduler, work, index) {
  51. if (index === void 0) { index = scheduler.index += 1; }
  52. var _this = _super.call(this, scheduler, work) || this;
  53. _this.scheduler = scheduler;
  54. _this.work = work;
  55. _this.index = index;
  56. _this.active = true;
  57. _this.index = scheduler.index = index;
  58. return _this;
  59. }
  60. VirtualAction.prototype.schedule = function (state, delay) {
  61. if (delay === void 0) { delay = 0; }
  62. if (!this.id) {
  63. return _super.prototype.schedule.call(this, state, delay);
  64. }
  65. this.active = false;
  66. var action = new VirtualAction(this.scheduler, this.work);
  67. this.add(action);
  68. return action.schedule(state, delay);
  69. };
  70. VirtualAction.prototype.requestAsyncId = function (scheduler, id, delay) {
  71. if (delay === void 0) { delay = 0; }
  72. this.delay = scheduler.frame + delay;
  73. var actions = scheduler.actions;
  74. actions.push(this);
  75. actions.sort(VirtualAction.sortActions);
  76. return true;
  77. };
  78. VirtualAction.prototype.recycleAsyncId = function (scheduler, id, delay) {
  79. if (delay === void 0) { delay = 0; }
  80. return undefined;
  81. };
  82. VirtualAction.prototype._execute = function (state, delay) {
  83. if (this.active === true) {
  84. return _super.prototype._execute.call(this, state, delay);
  85. }
  86. };
  87. VirtualAction.sortActions = function (a, b) {
  88. if (a.delay === b.delay) {
  89. if (a.index === b.index) {
  90. return 0;
  91. }
  92. else if (a.index > b.index) {
  93. return 1;
  94. }
  95. else {
  96. return -1;
  97. }
  98. }
  99. else if (a.delay > b.delay) {
  100. return 1;
  101. }
  102. else {
  103. return -1;
  104. }
  105. };
  106. return VirtualAction;
  107. }(AsyncAction_1.AsyncAction));
  108. exports.VirtualAction = VirtualAction;
  109. //# sourceMappingURL=VirtualTimeScheduler.js.map