All files / src/core StorageFactory.ts

100% Statements 30/30
100% Branches 18/18
100% Functions 1/1
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 381x         1x 1x 441x 441x 88x 87x 441x 88x 87x 441x 88x 87x 441x 90x 87x 87x 4x 1x 1x 4x 1x 1x 1x   441x 87x 441x 441x 1x   1x    
import { IndexedDBStrategy, LocalStorageStrategy, MemoryStrategy, SessionStorageStrategy } from "./strategies";
import { type StorageBase, StorageEngine } from "./types";
import { Utils } from "./utils";
 
/** @ignore */
class StorageFactory {
	createStorage(type: StorageEngine, baseName: string, storeName?: string): StorageBase {
		switch (type) {
			case StorageEngine.SessionStorage:
				if (!Utils.isSessionStorageAvailable()) throw new Error("SessionStorage is not available");
				return new SessionStorageStrategy(baseName);
			case StorageEngine.LocalStorage:
				if (!Utils.isLocalStorageAvailable()) throw new Error("LocalStorage is not available");
				return new LocalStorageStrategy(baseName);
			case StorageEngine.IndexedDB:
				if (!Utils.isIndexedDBAvailable()) throw new Error("IndexedDB is not available");
				return new IndexedDBStrategy(baseName, storeName);
			case StorageEngine.Auto:
				if (Utils.isLocalStorageAvailable()) {
					return new LocalStorageStrategy(baseName);
				}
				if (Utils.isIndexedDBAvailable()) {
					return new IndexedDBStrategy(baseName, storeName);
				}
				if (Utils.isSessionStorageAvailable()) {
					return new SessionStorageStrategy(baseName);
				}
				return new MemoryStrategy();
 
			default:
				return new MemoryStrategy();
		}
	}
}
 
const storageFactory = new StorageFactory();
export { storageFactory as StorageFactory };