@diyaner/ding
Version:
dingiyan常用ts/js工具
31 lines • 1.64 kB
JavaScript
;
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.InstanceCache = void 0;
/** 一个实现实例级缓存数据的装饰器 */
function InstanceCache() {
return function (target, propertyKey, descriptor) {
const oldMethod = descriptor.value;
const cacheKey = Symbol.for(`${target.constructor.name}:${propertyKey}:cacheKey`);
descriptor.value = function (...args) {
return __awaiter(this, void 0, void 0, function* () {
const cacheValue = this[cacheKey]; // 此处this应该是实例对象,不是原型对象。
if (cacheValue !== undefined)
return Promise.resolve(cacheValue);
const result = yield oldMethod.apply(this, args);
this[cacheKey] = result;
return result;
});
};
};
}
exports.InstanceCache = InstanceCache;
//# sourceMappingURL=decorator.js.map