UNPKG

tsioc

Version:

tsioc is AOP, Ioc container, via typescript decorator

113 lines (111 loc) 4.08 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); var index_1 = require("../utils/index"); var index_2 = require("./decorators/index"); /** * cache manager. * * @export * @class CacheManager * @implements {ICacheManager} */ var CacheManager = /** @class */ (function () { function CacheManager(container) { this.container = container; } CacheManager.prototype.isChecking = function () { return !!this.timeout; }; CacheManager.prototype.hasCache = function (targetType) { return this.cacheTokens.has(targetType); }; CacheManager.prototype.cache = function (targetType, target, expires) { var cache; if (this.hasCache(targetType)) { cache = this.cacheTokens.get(targetType); cache.expires = Date.now() + expires; } else { cache = { target: target, expires: Date.now() + expires }; } this.cacheTokens.set(targetType, cache); if (!this.isChecking()) { this.checkExpires(); } }; CacheManager.prototype.get = function (targetType, expires) { var result = null; var cache = this.cacheTokens.get(targetType); if (cache.expires <= Date.now()) { result = cache.target; if (index_1.isNumber(expires) && expires > 0) { cache.expires = Date.now() + expires; this.cacheTokens.set(targetType, cache); } } else { this.destroy(targetType, cache.target); } return result; }; CacheManager.prototype.checkExpires = function () { var _this = this; if (this.timeout) { clearTimeout(this.timeout); this.timeout = 0; } if (this.cacheTokens.size > 0) { var timeoutCaches_1 = []; this.cacheTokens.forEach(function (cache, targetType) { if (cache.expires >= Date.now()) { timeoutCaches_1.push(targetType); } }); if (timeoutCaches_1.length) { timeoutCaches_1.forEach(function (targetType) { _this.destroy(targetType, _this.cacheTokens.get(targetType).target); }); } this.timeout = setTimeout(function () { _this.checkExpires(); }, 60000); } }; CacheManager.prototype.destroy = function (targetType, target) { if (!this.hasCache(targetType)) { return; } if (!target) { target = this.cacheTokens.get(targetType).target; } try { var component = target; if (index_1.isFunction(component.onDestroy)) { this.container.syncInvoke(targetType, 'onDestroy', target); } this.cacheTokens.delete(targetType); } catch (err) { console.error && console.error(err); } }; CacheManager = __decorate([ index_2.NonePointcut(), __metadata("design:paramtypes", [Object]) ], CacheManager); return CacheManager; }()); exports.CacheManager = CacheManager; //# sourceMappingURL=../sourcemaps/core/CacheManager.js.map