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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { AsyncAction } from './AsyncAction';
  2. import { AsyncScheduler } from './AsyncScheduler';
  3. export class VirtualTimeScheduler extends AsyncScheduler {
  4. constructor(SchedulerAction = VirtualAction, maxFrames = Number.POSITIVE_INFINITY) {
  5. super(SchedulerAction, () => this.frame);
  6. this.maxFrames = maxFrames;
  7. this.frame = 0;
  8. this.index = -1;
  9. }
  10. flush() {
  11. const { actions, maxFrames } = this;
  12. let error, action;
  13. while ((action = actions[0]) && action.delay <= maxFrames) {
  14. actions.shift();
  15. this.frame = action.delay;
  16. if (error = action.execute(action.state, action.delay)) {
  17. break;
  18. }
  19. }
  20. if (error) {
  21. while (action = actions.shift()) {
  22. action.unsubscribe();
  23. }
  24. throw error;
  25. }
  26. }
  27. }
  28. VirtualTimeScheduler.frameTimeFactor = 10;
  29. export class VirtualAction extends AsyncAction {
  30. constructor(scheduler, work, index = scheduler.index += 1) {
  31. super(scheduler, work);
  32. this.scheduler = scheduler;
  33. this.work = work;
  34. this.index = index;
  35. this.active = true;
  36. this.index = scheduler.index = index;
  37. }
  38. schedule(state, delay = 0) {
  39. if (!this.id) {
  40. return super.schedule(state, delay);
  41. }
  42. this.active = false;
  43. const action = new VirtualAction(this.scheduler, this.work);
  44. this.add(action);
  45. return action.schedule(state, delay);
  46. }
  47. requestAsyncId(scheduler, id, delay = 0) {
  48. this.delay = scheduler.frame + delay;
  49. const { actions } = scheduler;
  50. actions.push(this);
  51. actions.sort(VirtualAction.sortActions);
  52. return true;
  53. }
  54. recycleAsyncId(scheduler, id, delay = 0) {
  55. return undefined;
  56. }
  57. _execute(state, delay) {
  58. if (this.active === true) {
  59. return super._execute(state, delay);
  60. }
  61. }
  62. static sortActions(a, b) {
  63. if (a.delay === b.delay) {
  64. if (a.index === b.index) {
  65. return 0;
  66. }
  67. else if (a.index > b.index) {
  68. return 1;
  69. }
  70. else {
  71. return -1;
  72. }
  73. }
  74. else if (a.delay > b.delay) {
  75. return 1;
  76. }
  77. else {
  78. return -1;
  79. }
  80. }
  81. }
  82. //# sourceMappingURL=VirtualTimeScheduler.js.map