doddle
Version:
Tiny yet feature-packed (async) iteration toolkit.
65 lines • 2.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.doddlify = doddlify;
const index_js_1 = require("../doddle/index.js");
// Unique counter to help name anonymous members when no name/description exists.
let index = 0;
function makeSymbolName(name) {
// Increment for every decorator application.
const id = String(index++);
if (typeof name === "string")
return name;
if (typeof name === "symbol") {
const desc = name.description;
if (desc != null && desc !== "")
return desc;
}
return id;
}
function wrapMethod(method, name) {
const symName = makeSymbolName(name);
const cacheKey = Symbol(`doddlify:${symName}`);
return function () {
let cached = this[cacheKey];
if (cached == null) {
cached = this[cacheKey] = (0, index_js_1.doddle)(() => method.call(this));
}
return cached.pull();
};
}
function doddlify(a, b, c) {
// ES decorators path: (fn, context)
if (typeof a === "function" && c === undefined) {
const fn = a;
const ctx = b;
if (ctx?.kind === "getter") {
return wrapMethod(fn, ctx?.name);
}
if (ctx?.kind === "method") {
// Support only zero-arg methods
if (fn.length === 0)
return wrapMethod(fn, ctx?.name);
return fn;
}
return fn;
}
// TypeScript legacy path: (target, key, descriptor)
const descriptor = c;
if (!descriptor)
return descriptor;
// Getter
if (typeof descriptor.get === "function") {
descriptor.get = wrapMethod(descriptor.get, b);
return descriptor;
}
// Method (zero-arg only)
if (typeof descriptor.value === "function") {
const fn = descriptor.value;
if (fn.length === 0) {
descriptor.value = wrapMethod(fn, b);
}
return descriptor;
}
return descriptor;
}
//# sourceMappingURL=index.js.map