-
Notifications
You must be signed in to change notification settings - Fork 5
/
storage-manager.ts
39 lines (33 loc) · 1.02 KB
/
storage-manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { IStorage, IStorageConstructable } from "./interfaces";
import { DefaultStorage } from "./providers/default-storage";
/**
* Provides storage mangement integration
*/
export class StorageManager {
private static _storageConstructor: IStorageConstructable;
/**
* Get an instance of storage manager
* @returns
*/
public static getInstance(password: string, throwErrorIfNotAvailable = true): IStorage {
let storage: IStorage;
if (StorageManager._storageConstructor) {
storage = new StorageManager._storageConstructor(password);
} else {
storage = new DefaultStorage(password);
}
if (storage && storage.isAvailable()) {
return storage;
}
if (throwErrorIfNotAvailable) {
throw new Error(`No storage is available, please implement one with IStorage and register with StorageManager.register`);
}
}
/**
* Provides a custom storage
* @param storage
*/
public static register(storage: IStorageConstructable) {
this._storageConstructor = storage;
}
}