32 lines
774 B
TypeScript
32 lines
774 B
TypeScript
import { KeyValueStorage, MemoryStorage, ScopedStorage } from "./basic";
|
|
import { BrowserLocalStorage } from "./browser";
|
|
import { NodeDirectoryStorage } from "./node";
|
|
|
|
/**
|
|
* Base type for storage usage
|
|
*/
|
|
export type Storage = KeyValueStorage
|
|
|
|
/**
|
|
* Get the best "local" storage available
|
|
*/
|
|
export function getLocalStorage(appname: string): Storage {
|
|
try {
|
|
return new ScopedStorage(new BrowserLocalStorage(), appname);
|
|
} catch {
|
|
try {
|
|
return new NodeDirectoryStorage(appname);
|
|
} catch {
|
|
console.warn("No persistent storage available, using in-memory volatile storage");
|
|
return new MemoryStorage();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get a in-memory volatile storage
|
|
*/
|
|
export function getMemoryStorage(): Storage {
|
|
return new MemoryStorage();
|
|
}
|