configcat-common
Version:
ConfigCat is a configuration as a service that lets you manage your features and configurations without actually deploying new code.
108 lines (107 loc) • 5.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExternalConfigCache = exports.InMemoryConfigCache = void 0;
var tslib_1 = require("tslib");
var ProjectConfig_1 = require("./ProjectConfig");
var Utils_1 = require("./Utils");
var InMemoryConfigCache = /** @class */ (function () {
function InMemoryConfigCache() {
this.cachedConfig = ProjectConfig_1.ProjectConfig.empty;
}
InMemoryConfigCache.prototype.set = function (_key, config) {
this.cachedConfig = config;
};
InMemoryConfigCache.prototype.get = function (_key) {
return this.cachedConfig;
};
InMemoryConfigCache.prototype.getInMemory = function () {
return this.cachedConfig;
};
return InMemoryConfigCache;
}());
exports.InMemoryConfigCache = InMemoryConfigCache;
var ExternalConfigCache = /** @class */ (function () {
function ExternalConfigCache(cache, logger) {
this.cache = cache;
this.logger = logger;
this.cachedConfig = ProjectConfig_1.ProjectConfig.empty;
}
ExternalConfigCache.prototype.set = function (key, config) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var err_1;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
if (!config.isEmpty) {
this.cachedSerializedConfig = ProjectConfig_1.ProjectConfig.serialize(config);
this.cachedConfig = config;
}
else {
// We may have empty entries with timestamp > 0 (see the flooding prevention logic in ConfigServiceBase.fetchLogicAsync).
// In such cases we want to preserve the timestamp locally but don't want to store those entries into the external cache.
this.cachedSerializedConfig = void 0;
this.cachedConfig = config;
return [2 /*return*/];
}
return [4 /*yield*/, this.cache.set(key, this.cachedSerializedConfig)];
case 1:
_a.sent();
return [3 /*break*/, 3];
case 2:
err_1 = _a.sent();
this.logger.configServiceCacheWriteError(err_1);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
});
};
ExternalConfigCache.prototype.updateCachedConfig = function (externalSerializedConfig) {
if (externalSerializedConfig == null || externalSerializedConfig === this.cachedSerializedConfig) {
return;
}
this.cachedConfig = ProjectConfig_1.ProjectConfig.deserialize(externalSerializedConfig);
this.cachedSerializedConfig = externalSerializedConfig;
};
ExternalConfigCache.prototype.get = function (key) {
var _this = this;
try {
var cacheGetResult = this.cache.get(key);
// Take the async path only when the IConfigCatCache.get operation is asynchronous.
if (Utils_1.isPromiseLike(cacheGetResult)) {
return (function (cacheGetPromise) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var _a, err_2;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
_a = this.updateCachedConfig;
return [4 /*yield*/, cacheGetPromise];
case 1:
_a.apply(this, [_b.sent()]);
return [3 /*break*/, 3];
case 2:
err_2 = _b.sent();
this.logger.configServiceCacheReadError(err_2);
return [3 /*break*/, 3];
case 3: return [2 /*return*/, this.cachedConfig];
}
});
}); })(cacheGetResult);
}
// Otherwise, keep the code flow synchronous so the config services can sync up
// with the cache in their ctors synchronously (see ConfigServiceBase.syncUpWithCache).
this.updateCachedConfig(cacheGetResult);
}
catch (err) {
this.logger.configServiceCacheReadError(err);
}
return Promise.resolve(this.cachedConfig);
};
ExternalConfigCache.prototype.getInMemory = function () {
return this.cachedConfig;
};
return ExternalConfigCache;
}());
exports.ExternalConfigCache = ExternalConfigCache;