Skip to content

Commit

Permalink
Added useTriggerFrame hook (#624)
Browse files Browse the repository at this point in the history
* Added useTriggerFrame hook

* Used useSyncedRef

* changed to useEvent for useTriggerFrame

* changed to useEvent for useTriggerFrame

---------

Co-authored-by: Michael Pühringer <[email protected]>
  • Loading branch information
dvmoritzschoefl and puehringer authored Nov 15, 2024
1 parent f15259c commit c4c2ba8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/vis/vishooks/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './useBrush';
export * from './useLasso';
export * from './useLinearScale';
export * from './useCanvas';
export * from './useTriggerFrame';
51 changes: 51 additions & 0 deletions src/vis/vishooks/hooks/useTriggerFrame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-disable react-compiler/react-compiler */
import * as React from 'react';

import isEqual from 'lodash/isEqual';
import { useEvent } from '../../../hooks';

/**
* Hook similar to useEffect that triggers a frame when dependencies change.
* It will deeply compare the dependencies and only trigger a frame if they have changed.
* Frames are debounced to the next animation frame to ensure consistency with the
* display refresh rate. The optional profileId gives insights on the frame timings.
*
* Usage:
*
* ```tsx
* useTriggerFrame(() => {
* // Your frame code here
* }, [dependencies]);
* ```
*/
export function useTriggerFrame(frame: () => void, deps: React.DependencyList, profileId?: string) {
const frameRef = React.useRef<number | undefined>(undefined);
const depsRef = React.useRef(deps);

const callbackEvent = useEvent(frame);

if (!isEqual(depsRef.current, deps)) {
depsRef.current = deps;

// Request new frame
if (frameRef.current === undefined) {
frameRef.current = requestAnimationFrame(() => {
frameRef.current = undefined;

if (profileId) {
let msg = '';
const t0 = performance.now();
msg += `Profile: ${profileId}`;

callbackEvent();

const t1 = performance.now();
msg += ` took ${t1 - t0} milliseconds.`;
console.log(msg);
} else {
callbackEvent();
}
});
}
}
}

0 comments on commit c4c2ba8

Please sign in to comment.