storage/src/index.ts
Michaël Lemaire 79f68a6706 Renamed exported Storage type to TKStorage
To avoid conflict with existing web type
2019-11-14 18:25:37 +01:00

32 lines
780 B
TypeScript

import { KeyValueStorage, MemoryStorage, ScopedStorage } from "./basic";
import { BrowserLocalStorage } from "./browser";
import { NodeDirectoryStorage } from "./node";
/**
* Base type for storage usage
*/
export type TKStorage = KeyValueStorage
/**
* Get the best "local" storage available
*/
export function getLocalStorage(appname: string): TKStorage {
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(): TKStorage {
return new MemoryStorage();
}