useTraceTimelineChangeSync.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import {useLayoutEffect} from 'react';
  2. import type {TraceTree} from './traceModels/traceTree';
  3. import type {TraceScheduler} from './traceRenderers/traceScheduler';
  4. // Observer around the tree model that track trace space changes and dispatches
  5. // them to the trace scheduler. This exists because sometimes, the trace space
  6. // changes (tree gets new data, or the precision of events in the tree overflows the current trace duration)
  7. export function useTraceTimelineChangeSync(props: {
  8. traceScheduler: TraceScheduler;
  9. tree: TraceTree;
  10. }) {
  11. useLayoutEffect(() => {
  12. if (props.tree.type !== 'trace') {
  13. return undefined;
  14. }
  15. props.traceScheduler.dispatch('initialize trace space', [
  16. props.tree.root.space[0],
  17. 0,
  18. props.tree.root.space[1],
  19. 1,
  20. ]);
  21. // Whenever the timeline changes, update the trace space and trigger a redraw
  22. const onTraceTimelineChange = (s: [number, number]) => {
  23. props.traceScheduler.dispatch('set trace space', [s[0], 0, s[1], 1]);
  24. };
  25. props.tree.on('trace timeline change', onTraceTimelineChange);
  26. return () => {
  27. props.tree.off('trace timeline change', onTraceTimelineChange);
  28. };
  29. }, [props.traceScheduler, props.tree]);
  30. }