fix(25-01): add timer and callback cleanup in useRecordingHelpers
- Add useEffect cleanup for waitingOnStopTimer on unmount - Add conditional cleanup for window.JK callbacks - Track callback references to avoid deleting shared callbacks - Prevents memory leaks from accumulated timers and callbacks
This commit is contained in:
parent
445e4834a6
commit
ef3d7a630e
|
|
@ -30,6 +30,16 @@ const useRecordingHelpers = (jamClient) => {
|
|||
|
||||
const waitingOnStopTimer = useRef(null);
|
||||
|
||||
// Cleanup timer on unmount
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (waitingOnStopTimer.current) {
|
||||
clearTimeout(waitingOnStopTimer.current);
|
||||
waitingOnStopTimer.current = null;
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
// Get state function
|
||||
const getState = useCallback(() => ({
|
||||
waitingOnClientStop,
|
||||
|
|
@ -452,19 +462,39 @@ const useRecordingHelpers = (jamClient) => {
|
|||
};
|
||||
}, [sessionId, jamClient, thisClientStartedRecording]);
|
||||
|
||||
// Track our callbacks for conditional cleanup
|
||||
const callbacksRef = useRef({});
|
||||
|
||||
// Initialize - register global handlers for native client callbacks
|
||||
// Note: We don't delete callbacks on cleanup because multiple components may use this hook.
|
||||
// JKSessionScreen stays mounted and needs callbacks to work even after modal closes.
|
||||
// Since we use Redux for state, the callbacks will update the shared store correctly.
|
||||
// Note: Multiple hook instances (JKSessionScreen + JKSessionRecordingModal) share window.JK callbacks.
|
||||
// Only delete if we still own the callback - another instance may have registered newer ones.
|
||||
useEffect(() => {
|
||||
if (window) {
|
||||
window.JK = window.JK || {};
|
||||
window.JK.HandleRecordingStartResult = handleRecordingStartResult;
|
||||
window.JK.HandleRecordingStopResult = handleRecordingStopResult;
|
||||
window.JK.HandleRecordingStopped = handleRecordingStopped;
|
||||
window.JK.HandleRecordingStarted = handleRecordingStarted;
|
||||
window.JK.HandleRecordingAborted = handleRecordingAborted;
|
||||
|
||||
// Store our callback references
|
||||
callbacksRef.current = {
|
||||
HandleRecordingStartResult: handleRecordingStartResult,
|
||||
HandleRecordingStopResult: handleRecordingStopResult,
|
||||
HandleRecordingStopped: handleRecordingStopped,
|
||||
HandleRecordingStarted: handleRecordingStarted,
|
||||
HandleRecordingAborted: handleRecordingAborted,
|
||||
};
|
||||
|
||||
// Register callbacks
|
||||
Object.assign(window.JK, callbacksRef.current);
|
||||
}
|
||||
|
||||
return () => {
|
||||
// Only delete if we still own the callback (another instance may have registered newer ones)
|
||||
if (window.JK && callbacksRef.current) {
|
||||
Object.keys(callbacksRef.current).forEach(key => {
|
||||
if (window.JK[key] === callbacksRef.current[key]) {
|
||||
delete window.JK[key];
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}, [handleRecordingStartResult, handleRecordingStopResult, handleRecordingStopped, handleRecordingStarted, handleRecordingAborted]);
|
||||
|
||||
return {
|
||||
|
|
|
|||
Loading…
Reference in New Issue