Leave and switch encounters
Closing or reloading the iframe while canBeClosed is false can lose audio or unsaved note edits. It is your role to prevent users from closing the page or the iframe while unsaved changes are still pending.
Before leaving
Before a route change, a patient switch, or a tab close:
- If
canBeClosedisfalse, callstopRecording()orstartNoteGeneration()whenavailableActionsallows it. Both stop the recording.stopRecording()only stops it.startNoteGeneration()stops it first if needed, then starts the generation. The generation itself runs on Nabla's servers and does not keepcanBeClosedfalse. - Wait with
await client.waitUntilClosable(). - Tear down.
For navigation inside your app, use these steps before you tear down the page.
Closing or reloading the tab
beforeunload cannot wait for a promise. To warn users who close or reload the tab, check the current state synchronously:
window.addEventListener("beforeunload", (event) => {
if (client.getCurrentState()?.canBeClosed === false) {
event.preventDefault();
}
});
Switching encounters
One iframe load shows one encounter. To move from encounter A to B:
try {
if (clientA.getCurrentState()?.availableActions.includes("STOP_RECORDING")) {
await clientA.stopRecording(); // or startNoteGeneration(), to get the note first
}
await clientA.waitUntilClosable(); // no timeout: audio may still be uploading
} finally {
clientA.destroy();
}
Then call create again with the URL of a new POST /encounters, or of POST /encounters/url for an encounter you already created. An encounter URL is single-use and expires in 10 minutes, so a new client always needs a fresh one. See Launch encounters.
You can use the same iframe element or a new one. Do not set src yourself: the SDK does.
Destroying the client
A client is bound to one iframe. If another client is created on it, the first one emits CONTEXT_CHANGED and stops, so call destroy() first.
destroy() is idempotent and rejects pending commands with DESTROYED. Call it before you remove or replace the iframe.