Repository URL to install this package:
|
Version:
1.2.8 ▾
|
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @todo replace oneStorage everywhere
*/
const indexdb_1 = __importDefault(require("./adapters/indexdb"));
const local_storage_1 = __importDefault(require("./adapters/local-storage"));
const in_memory_1 = __importDefault(require("./adapters/in-memory"));
const IS_BROWSER = typeof window === 'object';
const HAS_INDEX_DB = IS_BROWSER && 'indexedDB' in window;
/**
* keys, values...
* @alias OmniStorage
*/
class OneStorage {
constructor() {
this.inMemory = in_memory_1.default;
this.indexdb = indexdb_1.default;
this.ls = local_storage_1.default;
}
// parse when reading back out of ls...?
get(key) {
if (in_memory_1.default.has(key) === true) {
return in_memory_1.default.get(key);
}
if (local_storage_1.default.has(key) === true) {
return local_storage_1.default.get(key);
}
// not sure this is needed
const existing = in_memory_1.default.get(key);
// could node-async-fs it, or mongo
if (HAS_INDEX_DB) {
indexdb_1.default.get(key).then(saved => {
if (existing !== saved) {
in_memory_1.default.set(key, saved);
}
});
}
return existing;
}
// @todo is it faster to "graphql-client-cache" ignore if it always fills?
set(key, value) {
// store as string for graphql? perf?
// const final = isString(value) ? JSON.parse(value) : value
// return indexdb.set(key, final)
// @note @important - we start indexdb transaction first, it's async
const resolving = HAS_INDEX_DB
? indexdb_1.default.set(key, value)
: Promise.resolve(-42);
// not putting it in ls, took 1s
if (key !== 'graphql-client-cache') {
local_storage_1.default.set(key, value);
}
in_memory_1.default.set(key, value);
return resolving;
// indexdb.set(key, value)
// if (isObj(value)) {
// indexdb.set(key, JSON.stringify(value))
// } else {
// indexdb.set(key, value)
// }
}
has(key) {
return !!this.get(key);
}
remove(key) {
if (HAS_INDEX_DB) {
indexdb_1.default.delete(key);
}
in_memory_1.default.delete(key);
local_storage_1.default.remove(key);
}
clear() {
if (HAS_INDEX_DB) {
indexdb_1.default.clear();
}
in_memory_1.default.clear();
local_storage_1.default.clear();
}
}
exports.OneStorage = OneStorage;
// eslint-disable-next-line
OneStorage.prototype['delete'] = OneStorage.prototype.remove;
OneStorage.prototype['removeItem'] = OneStorage.prototype.remove;
OneStorage.prototype['getItem'] = OneStorage.prototype.get;
OneStorage.prototype['setItem'] = OneStorage.prototype.set;
OneStorage.prototype['hasItem'] = OneStorage.prototype.has;
exports.default = OneStorage;
//# sourceMappingURL=OneStorage.js.map