-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utilisation de l'API expérimentale StorageManager pour demander au navigateur un stockage plus pérenne. Fixes #69
- Loading branch information
Showing
4 changed files
with
71 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const localforage = import('localforage'); // Can fail on node | ||
|
||
async function ensure_persisted() { | ||
if (typeof navigator !== 'object' || !navigator.storage || !navigator.storage.persist) | ||
return false; | ||
return await navigator.storage.persist(); | ||
} | ||
|
||
export async function storage_is_volatile(): Promise<boolean | undefined> { | ||
if (typeof navigator !== 'object' || !navigator.storage || !navigator.storage.persisted) | ||
return undefined; | ||
return !(await navigator.storage.persisted()); | ||
} | ||
|
||
export async function storage_usage_ratio(): Promise<number | undefined> { | ||
if (typeof navigator !== 'object' || !navigator.storage || !navigator.storage.estimate) | ||
return undefined; | ||
const { quota, usage } = await navigator.storage.estimate(); | ||
if (!quota || !usage) return undefined; | ||
return usage / quota; | ||
} | ||
|
||
export async function store_locally(key: string, value: any): Promise<boolean> { | ||
if (typeof window !== 'object') return false; | ||
const persisted = await ensure_persisted(); | ||
await (await localforage).setItem(key, value); | ||
return persisted; | ||
} | ||
|
||
export async function get_from_local_store<T>(key: string): Promise<T | undefined> { | ||
if (typeof window !== 'object') return undefined; // Browser-only | ||
const element = await (await localforage).getItem(key); | ||
return element as T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters