a zip code crypto-currency system good for red ONLY

VirtualTimeScheduler.js 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /**
  11. * Prompt the Scheduler to execute all of its queued actions, therefore
  12. * clearing its queue.
  13. * @return {void}
  14. */
  15. flush() {
  16. const { actions, maxFrames } = this;
  17. let error, action;
  18. while ((action = actions.shift()) && (this.frame = action.delay) <= maxFrames) {
  19. if (error = action.execute(action.state, action.delay)) {
  20. break;
  21. }
  22. }
  23. if (error) {
  24. while (action = actions.shift()) {
  25. action.unsubscribe();
  26. }
  27. throw error;
  28. }
  29. }
  30. }
  31. VirtualTimeScheduler.frameTimeFactor = 10;
  32. /**
  33. * We need this JSDoc comment for affecting ESDoc.
  34. * @ignore
  35. * @extends {Ignored}
  36. */
  37. export class VirtualAction extends AsyncAction {
  38. constructor(scheduler, work, index = scheduler.index += 1) {
  39. super(scheduler, work);
  40. this.scheduler = scheduler;
  41. this.work = work;
  42. this.index = index;
  43. this.active = true;
  44. this.index = scheduler.index = index;
  45. }
  46. schedule(state, delay = 0) {
  47. if (!this.id) {
  48. return super.schedule(state, delay);
  49. }
  50. this.active = false;
  51. // If an action is rescheduled, we save allocations by mutating its state,
  52. // pushing it to the end of the scheduler queue, and recycling the action.
  53. // But since the VirtualTimeScheduler is used for testing, VirtualActions
  54. // must be immutable so they can be inspected later.
  55. const action = new VirtualAction(this.scheduler, this.work);
  56. this.add(action);
  57. return action.schedule(state, delay);
  58. }
  59. requestAsyncId(scheduler, id, delay = 0) {
  60. this.delay = scheduler.frame + delay;
  61. const { actions } = scheduler;
  62. actions.push(this);
  63. actions.sort(VirtualAction.sortActions);
  64. return true;
  65. }
  66. recycleAsyncId(scheduler, id, delay = 0) {
  67. return undefined;
  68. }
  69. _execute(state, delay) {
  70. if (this.active === true) {
  71. return super._execute(state, delay);
  72. }
  73. }
  74. static sortActions(a, b) {
  75. if (a.delay === b.delay) {
  76. if (a.index === b.index) {
  77. return 0;
  78. }
  79. else if (a.index > b.index) {
  80. return 1;
  81. }
  82. else {
  83. return -1;
  84. }
  85. }
  86. else if (a.delay > b.delay) {
  87. return 1;
  88. }
  89. else {
  90. return -1;
  91. }
  92. }
  93. }
  94. //# sourceMappingURL=VirtualTimeScheduler.js.map