UNPKG

@use-services/dynconf

Version:
56 lines (55 loc) 2.26 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.init = exports.Service = void 0; class Service { constructor(option) { this.modifyAt = -1; this.data = option.args; this.redis = option.deps[0]; const ns = option.app + ":" + option.srvName; this.dateKey = ns + ":" + "date"; this.dataKey = ns + ":" + "data"; } save(change) { return __awaiter(this, void 0, void 0, function* () { const data = Object.assign(Object.assign({}, this.data), change); const modifyAt = Date.now(); yield this.redis .multi() .set(this.dateKey, modifyAt) .set(this.dataKey, JSON.stringify(data)) .exec(); this.data = data; this.modifyAt = modifyAt; }); } load() { return __awaiter(this, void 0, void 0, function* () { const modifyAt = parseInt(yield this.redis.get(this.dateKey)); if (modifyAt !== this.modifyAt) { const data = JSON.parse(yield this.redis.get(this.dataKey)) || {}; this.data = Object.assign(Object.assign({}, this.data), data); this.modifyAt = modifyAt; } return this.data; }); } } exports.Service = Service; function init(option) { return __awaiter(this, void 0, void 0, function* () { const srv = new (option.ctor || Service)(option); yield srv.load(); return srv; }); } exports.init = init;