From fda8021bfff62ce3d63a8b98b9033333a433207f Mon Sep 17 00:00:00 2001 From: Steve Purves Date: Tue, 21 Nov 2023 20:43:29 +0000 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=86=91=20added=20functions=20to=20remo?= =?UTF-8?q?ve=20saved=20sessions=20(#709)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/funny-gorillas-brake.md | 5 +++++ packages/core/src/index.ts | 1 + packages/core/src/sessions.ts | 35 ++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 .changeset/funny-gorillas-brake.md diff --git a/.changeset/funny-gorillas-brake.md b/.changeset/funny-gorillas-brake.md new file mode 100644 index 00000000..31ff658f --- /dev/null +++ b/.changeset/funny-gorillas-brake.md @@ -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 diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 35db1b62..903fa123 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -14,3 +14,4 @@ export * from './manager'; export * from './rendermime'; export * from './types'; export * from './config'; +export { clearAllSavedSessions, clearSavedSession } from './sessions'; diff --git a/packages/core/src/sessions.ts b/packages/core/src/sessions.ts index cec5fa5b..1a80ed56 100644 --- a/packages/core/src/sessions.ts +++ b/packages/core/src/sessions.ts @@ -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)); +}