UNPKG

@d3vtool/ex-frame

Version:

This library enhances Express.js by providing a more organized structure for web API projects, along with improved control and error handling capabilities.

142 lines (141 loc) 4.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Memoize = Memoize; exports.Cache = Cache; exports.OnError = OnError; exports.Pipe = Pipe; exports.MeasureTime = MeasureTime; exports.Throttle = Throttle; exports.Debounce = Debounce; function Memoize() { return function (_targetFn, _propertyKey, descriptor) { if (!descriptor?.value) { throw new Error("Decorator can only be applied to methods."); } const memo = new Map(); const orignalMethod = descriptor.value; descriptor.value = async function (...args) { const key = JSON.stringify(args); if (memo.has(key)) { return memo.get(key); } else { const result = await orignalMethod.apply(this, args); memo.set(key, result); return result; } }; return descriptor; }; } function Cache(ttl) { const cache = new Map(); return function (_targetFn, _propertyKey, descriptor) { if (!descriptor?.value) { throw new Error("Decorator can only be applied to methods."); } const orignalMethod = descriptor.value; descriptor.value = async function (...args) { const key = JSON.stringify(args); if (cache.has(key)) { ; return cache.get(key); } else { const result = await orignalMethod.apply(this, args); cache.set(key, result); setTimeout(() => { cache.delete(key); }, ttl); return result; } }; return descriptor; }; } function OnError(fn) { return function (_targetFn, _propertyKey, descriptor) { if (!descriptor?.value) { throw new Error("Decorator can only be applied to methods."); } const orignalMethod = descriptor.value; descriptor.value = async function (...args) { try { return await orignalMethod.apply(this, args); } catch (err) { fn(err); } }; return descriptor; }; } function Pipe(fn) { return function (_targetFn, _propertyKey, descriptor) { if (!descriptor?.value) { throw new Error("Decorator can only be applied to methods."); } const orignalMethod = descriptor.value; descriptor.value = async function (...args) { const result = await orignalMethod.apply(this, args); fn(result); }; return descriptor; }; } function MeasureTime(fn) { return function (_targetFn, _propertyKey, descriptor) { if (!descriptor?.value) { throw new Error("Decorator can only be applied to methods."); } const orignalMethod = descriptor.value; descriptor.value = async function (...args) { const start = performance.now(); const result = await orignalMethod.apply(this, args); const elaspedTime = performance.now() - start; if (fn) { return fn(elaspedTime); } console.log(`${_propertyKey} took ${elaspedTime}ms.`); return result; }; return descriptor; }; } function Throttle(limit) { return function (_, __, descriptor) { if (!descriptor?.value) { throw new Error("Decorator can only be applied to methods."); } const orignalMethod = descriptor.value; let lastCall = 0; descriptor.value = async function (...args) { const now = Date.now(); if ((now - lastCall) >= limit) { lastCall = now; return await orignalMethod.apply(this, args); } }; return descriptor; }; } function Debounce(delay, fn) { return function (_, __, descriptor) { if (!descriptor?.value) { throw new Error("Decorator can only be applied to methods."); } const orignalMethod = descriptor.value; let timeout; descriptor.value = function (...args) { clearTimeout(timeout); console.log("cleard: ", timeout); timeout = setTimeout(async () => { const result = await orignalMethod.apply(this, args); if (fn) { fn(result); } }, delay); }; return descriptor; }; }