Skip to content

Commit

Permalink
🆑 added functions to remove saved sessions (#709)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejpurves authored Nov 21, 2023
1 parent 358b62e commit fda8021
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-gorillas-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'thebe-core': patch
---

Added and exported utility functions `clearAllSavedSessions`,`clearSavedSession` to assist apps with managing saved binder sessions in local storage
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from './manager';
export * from './rendermime';
export * from './types';
export * from './config';
export { clearAllSavedSessions, clearSavedSession } from './sessions';
35 changes: 35 additions & 0 deletions packages/core/src/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,38 @@ export async function getExistingServer(

return existingSettings;
}

/**
* Remove all saved sessions items from local storage based on the storagePrefix provided.
* The appropriate (default) storage prefix will be available in the SavedSessionOptions object
* in the Config object.
*
* @param storagePrefix
*/
export function clearAllSavedSessions(storagePrefix: string) {
const keysToRemove: string[] = [];
for (let i = 0; i < window.localStorage.length; i++) {
const key = window.localStorage.key(i);
if (key?.startsWith(storagePrefix)) {
keysToRemove.push(key);
}
}
console.debug(
`thebe:clearAllSavedSessions - removing ${keysToRemove.length} saved sessions`,
keysToRemove.join(','),
);
keysToRemove.forEach((key) => window.localStorage.removeItem(key));
}

/**
* Remove all saved sessions items from local storage based on the storagePrefix provided.
* The appropriate (default) storage prefix will be available in the SavedSessionOptions object
* in the Config object.
*
* @param storagePrefix
* @param url
*/
export function clearSavedSession(storagePrefix: string, url: string) {
console.debug(`thebe:clearSavedSession - removing ${makeStorageKey(storagePrefix, url)}`);
window.localStorage.removeItem(makeStorageKey(storagePrefix, url));
}

0 comments on commit fda8021

Please sign in to comment.