157 lines
7.4 KiB
JavaScript
157 lines
7.4 KiB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
function step(op) {
|
|
if (f) throw new TypeError("Generator is already executing.");
|
|
while (_) try {
|
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
switch (op[0]) {
|
|
case 0: case 1: t = op; break;
|
|
case 4: _.label++; return { value: op[1], done: false };
|
|
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
default:
|
|
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
if (t[2]) _.ops.pop();
|
|
_.trys.pop(); continue;
|
|
}
|
|
op = body.call(thisArg, _);
|
|
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
}
|
|
};
|
|
import defaultsDeep from "lodash-es/defaultsDeep";
|
|
import omit from "lodash-es/omit";
|
|
import pick from "lodash-es/pick";
|
|
import cloneDeep from "lodash-es/cloneDeep";
|
|
import { DBService } from "./db";
|
|
import LocalStorage from "lowdb/adapters/LocalStorage";
|
|
import SessionStorage from "./adapters/SessionStorage";
|
|
var DEFAULT_NAME = "vuex-along";
|
|
var ROOT_KEY = "root";
|
|
var VuexAlong = /** @class */ (function () {
|
|
function VuexAlong(_a) {
|
|
var _this = this;
|
|
var local = _a.local, session = _a.session, _b = _a.name, name = _b === void 0 ? DEFAULT_NAME : _b, _c = _a.justSession, justSession = _c === void 0 ? false : _c, adapterOptions = _a.adapterOptions;
|
|
var _d = adapterOptions || {}, _e = _d.local, localAdapter = _e === void 0 ? LocalStorage : _e, _f = _d.session, sessionAdapter = _f === void 0 ? SessionStorage : _f,
|
|
// Make sure your adapter is syncing. Then you can get the synchronized state
|
|
_g = _d.sync,
|
|
// Make sure your adapter is syncing. Then you can get the synchronized state
|
|
sync = _g === void 0 ? true : _g;
|
|
this.local = local;
|
|
this.session = session;
|
|
this.justSession = justSession;
|
|
this.sync = !!((!localAdapter && !sessionAdapter) || sync);
|
|
if (!justSession) {
|
|
this.localDBService = new DBService(name, localAdapter);
|
|
}
|
|
if (session) {
|
|
this.sessionDBService = new DBService(name, sessionAdapter);
|
|
}
|
|
if (window) {
|
|
window.clearVuexAlong = function (local, session) {
|
|
_this.clear(local, session);
|
|
};
|
|
}
|
|
}
|
|
Object.defineProperty(VuexAlong.prototype, "ready", {
|
|
get: function () {
|
|
var _a, _b;
|
|
if (this.sync) {
|
|
return true;
|
|
}
|
|
return Promise.all([
|
|
(_a = this.localDBService) === null || _a === void 0 ? void 0 : _a.ready,
|
|
(_b = this.sessionDBService) === null || _b === void 0 ? void 0 : _b.ready,
|
|
]);
|
|
},
|
|
enumerable: true,
|
|
configurable: true
|
|
});
|
|
VuexAlong.prototype.saveLocalData = function (state) {
|
|
var _a;
|
|
if (this.justSession) {
|
|
return;
|
|
}
|
|
this._dataHandler(state, (_a = this.local, (_a !== null && _a !== void 0 ? _a : { list: [] })), this.localDBService);
|
|
};
|
|
VuexAlong.prototype.saveSessionData = function (state) {
|
|
var session = this.session;
|
|
if (!session) {
|
|
return;
|
|
}
|
|
this._dataHandler(state, session, this.sessionDBService);
|
|
};
|
|
VuexAlong.prototype._dataHandler = function (state, options, db) {
|
|
var _a;
|
|
if (!db) {
|
|
return;
|
|
}
|
|
var duplicateState = cloneDeep(state);
|
|
var key = ROOT_KEY;
|
|
var list = options.list, isFilter = options.isFilter;
|
|
if ((_a = list) === null || _a === void 0 ? void 0 : _a.length) {
|
|
duplicateState = isFilter
|
|
? omit(duplicateState, list)
|
|
: pick(duplicateState, list);
|
|
}
|
|
db.set(key, duplicateState).catch(logger);
|
|
};
|
|
VuexAlong.prototype.saveData = function (state) {
|
|
this.saveLocalData(state);
|
|
this.saveSessionData(state);
|
|
};
|
|
VuexAlong.prototype.restoreData = function (store) {
|
|
var _a, _b;
|
|
var key = ROOT_KEY;
|
|
store.replaceState(defaultsDeep((_a = this.sessionDBService) === null || _a === void 0 ? void 0 : _a.get(key), (_b = this.localDBService) === null || _b === void 0 ? void 0 : _b.get(key), store.state));
|
|
};
|
|
VuexAlong.prototype.clear = function (local, session) {
|
|
if (local === void 0) { local = true; }
|
|
if (session === void 0) { session = false; }
|
|
var _a, _b;
|
|
local && ((_a = this.localDBService) === null || _a === void 0 ? void 0 : _a.unset(ROOT_KEY).catch(logger));
|
|
session && ((_b = this.sessionDBService) === null || _b === void 0 ? void 0 : _b.unset(ROOT_KEY).catch(logger));
|
|
};
|
|
return VuexAlong;
|
|
}());
|
|
export default function (options) {
|
|
var _this = this;
|
|
if (options === void 0) { options = {}; }
|
|
var vuexAlong = new VuexAlong(options);
|
|
return function (store) { return __awaiter(_this, void 0, void 0, function () {
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0:
|
|
if (!(vuexAlong.ready !== true)) return [3 /*break*/, 2];
|
|
return [4 /*yield*/, vuexAlong.ready];
|
|
case 1:
|
|
_a.sent();
|
|
_a.label = 2;
|
|
case 2:
|
|
vuexAlong.restoreData(store);
|
|
store.subscribe(function (_mutation, state) { return vuexAlong.saveData(state); });
|
|
return [2 /*return*/];
|
|
}
|
|
});
|
|
}); };
|
|
}
|
|
// utils
|
|
function logger(reason) {
|
|
console.error(reason);
|
|
}
|