mobx-persist-store
Version:
Mobx Persist Store
29 lines (28 loc) • 1.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.makePersistable = void 0;
const PersistStore_1 = require("./PersistStore");
const PersistStoreMap_1 = require("./PersistStoreMap");
const utils_1 = require("./utils");
const setMobxPersistStore = (target, persistStore) => {
if (process.env.NODE_ENV !== 'production') {
// @ts-ignore Type 'IterableIterator<[any, PersistStore<any, any>]>' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.
for (const [key, store] of PersistStoreMap_1.PersistStoreMap.entries()) {
if (store.storageName === persistStore.storageName) {
store.stopPersisting();
PersistStoreMap_1.PersistStoreMap.delete(key);
}
}
}
PersistStoreMap_1.PersistStoreMap.set(target, persistStore);
};
const makePersistable = async (target, storageOptions, reactionOptions) => {
const mobxPersistStore = new PersistStore_1.PersistStore(target, storageOptions, reactionOptions);
const hasPersistedStoreAlready = Array.from(PersistStoreMap_1.PersistStoreMap.values())
.map((item) => item.storageName)
.includes(mobxPersistStore.storageName);
(0, utils_1.duplicatedStoreWarningIf)(hasPersistedStoreAlready, mobxPersistStore.storageName);
setMobxPersistStore(target, mobxPersistStore);
return mobxPersistStore.init();
};
exports.makePersistable = makePersistable;