traceScheduler.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. type ArgumentTypes<F> = F extends (...args: infer A) => any ? A : never;
  2. type EventStore = {
  3. [K in keyof TraceEvents]: Array<[number, TraceEvents[K]]>;
  4. };
  5. export enum TraceEventPriority {
  6. LOW = 10,
  7. MEDIUM = 5,
  8. HIGH = 1,
  9. }
  10. export interface TraceEvents {
  11. ['divider resize']: (view: {list: number; span_list: number}) => void;
  12. ['divider resize end']: (list_width: number) => void;
  13. ['draw']: (options?: {list?: number; span_list?: number}) => void;
  14. ['initialize trace space']: (
  15. space: [x: number, y: number, width: number, height: number]
  16. ) => void;
  17. ['initialize virtualized list']: () => void;
  18. ['set container physical space']: (
  19. container_space: [x: number, y: number, width: number, height: number]
  20. ) => void;
  21. ['set trace space']: (
  22. space: [x: number, y: number, width: number, height: number]
  23. ) => void;
  24. ['set trace view']: (view: {width?: number; x?: number}) => void;
  25. }
  26. export class TraceScheduler {
  27. events: EventStore = {
  28. ['initialize virtualized list']: new Array<
  29. [TraceEventPriority, TraceEvents['initialize virtualized list']]
  30. >(),
  31. ['set container physical space']: new Array<
  32. [TraceEventPriority, TraceEvents['set container physical space']]
  33. >(),
  34. ['initialize trace space']: new Array<
  35. [TraceEventPriority, TraceEvents['initialize trace space']]
  36. >(),
  37. ['set trace space']: new Array<
  38. [TraceEventPriority, TraceEvents['set trace space']]
  39. >(),
  40. ['divider resize end']: new Array<
  41. [TraceEventPriority, TraceEvents['divider resize end']]
  42. >(),
  43. ['divider resize']: new Array<[TraceEventPriority, TraceEvents['divider resize']]>(),
  44. ['set trace view']: new Array<[TraceEventPriority, TraceEvents['set trace view']]>(),
  45. ['draw']: new Array<[TraceEventPriority, TraceEvents['draw']]>(),
  46. };
  47. once<K extends keyof TraceEvents>(eventName: K, cb: TraceEvents[K]) {
  48. const wrapper = (...args: any[]) => {
  49. // @ts-expect-error
  50. cb(...args);
  51. this.off(eventName, wrapper);
  52. };
  53. this.on(eventName, wrapper);
  54. }
  55. on<K extends keyof TraceEvents>(
  56. eventName: K,
  57. cb: TraceEvents[K],
  58. priority: TraceEventPriority = 10
  59. ): void {
  60. const arr = this.events[eventName];
  61. if (!arr || arr.some(a => a[1] === cb)) {
  62. return;
  63. }
  64. arr.push([priority, cb]);
  65. arr.sort((a, b) => a[0] - b[0]);
  66. }
  67. off<K extends keyof TraceEvents>(eventName: K, cb: TraceEvents[K]): void {
  68. const arr = this.events[eventName];
  69. if (!arr) {
  70. return;
  71. }
  72. // @ts-expect-error - filter out the callback
  73. this.events[eventName] = arr.filter(a => a[1] !== cb) as unknown as Array<
  74. [TraceEventPriority, K]
  75. >;
  76. }
  77. dispatch<K extends keyof TraceEvents>(
  78. eventName: K,
  79. ...args: ArgumentTypes<TraceEvents[K]>
  80. ): void {
  81. for (const [_priority, handler] of this.events[eventName]) {
  82. // @ts-expect-error
  83. handler(...args);
  84. }
  85. }
  86. }