dispose()
dispose() immediately releases the resources used by a MonitorDog SDK detector instance.
If logout() means user session termination, dispose() is the cleanup function you call when the SDK instance will no longer be used, such as page exit, component unmount, or detector instance disposal.
Role
When dispose() is called, the SDK internally performs the following work.
Stop webcam detection
-> Clean up camera stream
-> Terminate detection worker
-> Clean up pose tracker/runtime state
-> Remove SDK access token
dispose() does not send a logout activity event to the MonitorDog server. If the flow needs a user logout record, call logout() before dispose().
Parameters
dispose() does not take any parameters.
detector.dispose();
Unlike logout(), it does not return a Promise, so you do not need to await it.
When to Use
Call dispose() when the detector instance lifecycle ends.
Common moments include:
- React component unmount
- The user leaves the page where the MonitorDog SDK is running
- Discarding an existing instance before creating a new detector instance
- Ensuring resources are cleaned up after
login()orlogout()failure
Example:
useEffect(() => {
let disposed = false;
async function startDetector() {
const detector = await MonitorDogDetector.init({
apiBaseUrl: "https://api.monitor.dog/v1",
sessionTokenProvider,
onDetect,
});
if (disposed) {
detector.dispose();
return;
}
detectorRef.current = detector;
await detector.login({ email: currentUser.email });
await detector.start();
}
void startDetector();
return () => {
disposed = true;
detectorRef.current?.dispose();
detectorRef.current = null;
};
}, [currentUser.email]);
Return Value
dispose() does not return a value.
detector.dispose();
The return type is:
void
dispose() is a synchronous function. It starts cleaning up local resources held by the SDK immediately when called, and customer application code does not need to process a separate response value.
Difference from logout()
logout() is the user session termination flow. When possible, it sends a logout activity event and then cleans up the SDK token and session state.
dispose() is the resource release flow. It does not attempt a server logout API call or MonitorDog logout event transmission. It cleans up the camera, worker, and token state used by the current detector instance.
For a user logout button or another flow with session termination semantics, the recommended flow is:
try {
detector.stop();
await detector.logout();
} finally {
detector.dispose();
}
If the page simply disappears or the component unmounts, calling only dispose() is enough.
Failure Handling
dispose() is a synchronous cleanup function that does not perform network requests.
In normal use, a separate try/catch is not needed. However, when using it together with logout(), call it in finally so resources are cleaned up regardless of whether logout() fails.
Notes
dispose()does not log out the customer website session.dispose()does not send a MonitorDog logout activity event.- If a user logout record is needed, call
logout()first afterstop(). - After calling
dispose(), prefer creating a new instance withinit()instead of reusing the same detector instance. - On React unmount or route change, set the detector reference to
nullto avoid duplicate cleanup.