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

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