canvasScheduler.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import {mat3} from 'gl-matrix';
  2. import {Rect} from './gl/utils';
  3. import {FlamegraphFrame} from './flamegraphFrame';
  4. type DrawFn = () => void;
  5. type ArgumentTypes<F> = F extends (...args: infer A) => any ? A : never;
  6. export interface FlamegraphEvents {
  7. ['highlight frame']: (
  8. frame: FlamegraphFrame | null,
  9. mode: 'hover' | 'selected'
  10. ) => void;
  11. ['reset zoom']: () => void;
  12. ['set config view']: (configView: Rect) => void;
  13. ['transform config view']: (transform: mat3) => void;
  14. ['zoom at frame']: (frame: FlamegraphFrame, strategy: 'min' | 'exact') => void;
  15. }
  16. type EventStore = {[K in keyof FlamegraphEvents]: Set<FlamegraphEvents[K]>};
  17. export class CanvasScheduler {
  18. beforeFrameCallbacks: Set<DrawFn> = new Set();
  19. afterFrameCallbacks: Set<DrawFn> = new Set();
  20. onDisposeCallbacks: Set<() => void> = new Set();
  21. requestAnimationFrame: number | null = null;
  22. events: EventStore = {
  23. ['reset zoom']: new Set<FlamegraphEvents['reset zoom']>(),
  24. ['highlight frame']: new Set<FlamegraphEvents['highlight frame']>(),
  25. ['set config view']: new Set<FlamegraphEvents['set config view']>(),
  26. ['transform config view']: new Set<FlamegraphEvents['transform config view']>(),
  27. ['zoom at frame']: new Set<FlamegraphEvents['zoom at frame']>(),
  28. };
  29. onDispose(cb: () => void): void {
  30. if (this.onDisposeCallbacks.has(cb)) {
  31. return;
  32. }
  33. this.onDisposeCallbacks.add(cb);
  34. }
  35. on<K extends keyof FlamegraphEvents>(eventName: K, cb: FlamegraphEvents[K]): void {
  36. const set = this.events[eventName] as unknown as Set<FlamegraphEvents[K]>;
  37. if (set.has(cb)) {
  38. return;
  39. }
  40. set.add(cb);
  41. }
  42. off<K extends keyof FlamegraphEvents>(eventName: K, cb: FlamegraphEvents[K]): void {
  43. const set = this.events[eventName] as unknown as Set<FlamegraphEvents[K]>;
  44. if (set.has(cb)) {
  45. set.delete(cb);
  46. }
  47. }
  48. dispatch<K extends keyof FlamegraphEvents>(
  49. event: K,
  50. ...args: ArgumentTypes<FlamegraphEvents[K]>
  51. ): void {
  52. for (const handler of this.events[event]) {
  53. // @ts-ignore
  54. handler(...args);
  55. }
  56. }
  57. private registerCallback(cb: DrawFn, pool: Set<DrawFn>) {
  58. if (pool.has(cb)) {
  59. return;
  60. }
  61. pool.add(cb);
  62. }
  63. private unregisterCallback(cb: DrawFn, pool: Set<DrawFn>) {
  64. if (pool.has(cb)) {
  65. pool.delete(cb);
  66. }
  67. }
  68. registerBeforeFrameCallback(cb: DrawFn): void {
  69. this.registerCallback(cb, this.beforeFrameCallbacks);
  70. }
  71. unregisterBeforeFrameCallback(cb: DrawFn): void {
  72. this.unregisterCallback(cb, this.beforeFrameCallbacks);
  73. }
  74. registerAfterFrameCallback(cb: DrawFn): void {
  75. this.registerCallback(cb, this.afterFrameCallbacks);
  76. }
  77. unregisterAfterFrameCallback(cb: DrawFn): void {
  78. this.unregisterCallback(cb, this.afterFrameCallbacks);
  79. }
  80. dispose(): void {
  81. for (const cb of this.onDisposeCallbacks) {
  82. this.onDisposeCallbacks.delete(cb);
  83. cb();
  84. }
  85. for (const type in this.events) {
  86. this.events[type].clear();
  87. }
  88. }
  89. drawSync(): void {
  90. for (const cb of this.beforeFrameCallbacks) {
  91. cb();
  92. }
  93. for (const cb of this.afterFrameCallbacks) {
  94. cb();
  95. }
  96. }
  97. draw(): void {
  98. if (this.requestAnimationFrame) {
  99. window.cancelAnimationFrame(this.requestAnimationFrame);
  100. }
  101. this.requestAnimationFrame = window.requestAnimationFrame(() => {
  102. for (const cb of this.beforeFrameCallbacks) {
  103. cb();
  104. }
  105. for (const cb of this.afterFrameCallbacks) {
  106. cb();
  107. }
  108. this.requestAnimationFrame = null;
  109. });
  110. }
  111. }
  112. export class CanvasPoolManager {
  113. schedulers: Set<CanvasScheduler> = new Set();
  114. registerScheduler(scheduler: CanvasScheduler): void {
  115. if (this.schedulers.has(scheduler)) {
  116. return;
  117. }
  118. this.schedulers.add(scheduler);
  119. }
  120. dispatch<K extends keyof FlamegraphEvents>(
  121. event: K,
  122. args: ArgumentTypes<FlamegraphEvents[K]>
  123. ): void {
  124. for (const scheduler of this.schedulers) {
  125. scheduler.dispatch(event, ...args);
  126. }
  127. }
  128. unregisterScheduler(scheduler: CanvasScheduler): void {
  129. if (this.schedulers.has(scheduler)) {
  130. scheduler.dispose();
  131. this.schedulers.delete(scheduler);
  132. }
  133. }
  134. drawSync(): void {
  135. for (const scheduler of this.schedulers) {
  136. scheduler.drawSync();
  137. }
  138. }
  139. draw(): void {
  140. for (const scheduler of this.schedulers) {
  141. scheduler.draw();
  142. }
  143. }
  144. }