UNPKG

@atomist/sdm-core

Version:

Atomist Software Delivery Machine - Implementation

107 lines 3.91 kB
"use strict"; /* * Copyright © 2019 Atomist, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 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 }); const sdm_1 = require("@atomist/sdm"); /** * Abstract PreferenceStore implementation to handle ttl and key scoping */ class AbstractPreferenceStore { constructor(ctx) { this.ctx = ctx; } get(key, options = {}) { return __awaiter(this, void 0, void 0, function* () { const pref = yield this.doGet(key, this.scope(options.scope)); const defaultValue = !!options ? options.defaultValue : undefined; if (!pref) { return defaultValue; } if (!!pref.ttl && pref.ttl < Date.now()) { return defaultValue; } else { return JSON.parse(pref.value); } }); } put(key, value, options = {}) { return __awaiter(this, void 0, void 0, function* () { const pref = { name: key, namespace: this.scope(options.scope), value: JSON.stringify(value), ttl: options.ttl, }; yield this.doPut(pref); return value; }); } list(scope) { return __awaiter(this, void 0, void 0, function* () { const prefs = yield this.doList(this.scope(scope)); if (!prefs) { return []; } else { const values = prefs.map(pref => { if (!!pref.ttl && pref.ttl < Date.now()) { return undefined; } else { return { key: pref.name, value: JSON.parse(pref.value) }; } }); return values.filter(v => !!v); } }); } delete(key, options = {}) { return __awaiter(this, void 0, void 0, function* () { return this.doDelete(key, this.scope(options.scope)); }); } scopeKey(key, scope) { if (!!scope && scope.length > 0) { return `${scope}_$_${key}`; } return key; } scope(scope) { if (!!scope) { switch (scope) { case sdm_1.PreferenceScope.Sdm: return this.ctx.configuration.name; case sdm_1.PreferenceScope.Workspace: return ""; default: return scope; } } return ""; } } exports.AbstractPreferenceStore = AbstractPreferenceStore; //# sourceMappingURL=AbstractPreferenceStore.js.map