storage/src/index.ts

32 lines
774 B
TypeScript
Raw Normal View History

2019-11-07 21:33:23 +00:00
import { KeyValueStorage, MemoryStorage, ScopedStorage } from "./basic";
import { BrowserLocalStorage } from "./browser";
2019-11-07 21:33:23 +00:00
import { NodeDirectoryStorage } from "./node";
/**
2019-11-14 17:21:44 +00:00
* Base type for storage usage
*/
2019-11-14 17:21:44 +00:00
export type Storage = KeyValueStorage
/**
* Get the best "local" storage available
*/
export function getLocalStorage(appname: string): Storage {
2019-11-07 21:33:23 +00:00
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();
}
}
}
2019-11-14 17:15:03 +00:00
/**
2019-11-14 17:21:44 +00:00
* Get a in-memory volatile storage
2019-11-14 17:15:03 +00:00
*/
2019-11-14 17:21:44 +00:00
export function getMemoryStorage(): Storage {
2019-11-14 17:15:03 +00:00
return new MemoryStorage();
}