UNPKG

nakedobjects.spa

Version:

Single Page Application client for a Naked Objects application.

69 lines 2.77 kB
var TypeResultCache = (function () { function TypeResultCache(context) { this.context = context; this.resultCache = {}; this.regexCache = []; this.subtypeCache = []; } TypeResultCache.prototype.addType = function (type, result) { this.resultCache[type] = result; }; TypeResultCache.prototype.addMatch = function (matcher, result) { this.regexCache.push({ regex: matcher, result: result }); }; TypeResultCache.prototype.addSubtype = function (type, result) { this.subtypeCache.push({ type: type, result: result }); }; TypeResultCache.prototype.setDefault = function (def) { this.default = def; }; TypeResultCache.prototype.cacheAndReturn = function (type, result) { this.resultCache[type] = result; return Promise.resolve(result); }; TypeResultCache.prototype.isSubtypeOf = function (subtype, index, count) { var _this = this; if (index >= count) { return Promise.reject(""); } var entry = this.subtypeCache[index]; return this.context.isSubTypeOf(subtype, entry.type).then(function (b) { return b ? Promise.resolve(entry.result) : _this.isSubtypeOf(subtype, index + 1, count); }); }; TypeResultCache.prototype.isSubtype = function (subtype) { var _this = this; var subtypeChecks = this.subtypeCache.length; if (subtypeChecks > 0) { return this.isSubtypeOf(subtype, 0, subtypeChecks) .then(function (c) { return _this.cacheAndReturn(subtype, c); }) .catch(function () { return _this.cacheAndReturn(subtype, _this.default); }); } return this.cacheAndReturn(subtype, this.default); }; TypeResultCache.prototype.getResult = function (type) { // 1 cache // 2 match regex // 3 match subtype // this is potentially expensive - need to filter out non ref types ASAP if (!type || type === "string" || type === "number" || type === "boolean") { return Promise.resolve(this.default); } var cachedEntry = this.resultCache[type]; if (cachedEntry) { return Promise.resolve(cachedEntry); } for (var _i = 0, _a = this.regexCache; _i < _a.length; _i++) { var entry = _a[_i]; if (entry.regex.test(type)) { return this.cacheAndReturn(type, entry.result); } } return this.isSubtype(type); }; return TypeResultCache; }()); export { TypeResultCache }; //# sourceMappingURL=type-result-cache.js.map