traceScheduler.tsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. (cb as any)(...args);
  50. this.off(eventName, wrapper);
  51. };
  52. this.on(eventName, wrapper);
  53. }
  54. on<K extends keyof TraceEvents>(
  55. eventName: K,
  56. cb: TraceEvents[K],
  57. priority: TraceEventPriority = 10
  58. ): void {
  59. const arr = this.events[eventName];
  60. if (!arr || arr.some(a => a[1] === cb)) {
  61. return;
  62. }
  63. arr.push([priority, cb]);
  64. arr.sort((a, b) => a[0] - b[0]);
  65. }
  66. off<K extends keyof TraceEvents>(eventName: K, cb: TraceEvents[K]): void {
  67. const arr = this.events[eventName];
  68. if (!arr) {
  69. return;
  70. }
  71. (this.events as any)[eventName] = arr.filter(a => a[1] !== cb) as unknown as Array<
  72. [TraceEventPriority, K]
  73. >;
  74. }
  75. dispatch<K extends keyof TraceEvents>(
  76. eventName: K,
  77. ...args: ArgumentTypes<TraceEvents[K]>
  78. ): void {
  79. for (const [_priority, handler] of this.events[eventName]) {
  80. (handler as any)(...args);
  81. }
  82. }
  83. }