UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

20 lines (19 loc) 803 B
import { _assert } from '../error/assert'; import { pTimeout } from '../promise/pTimeout'; import { _getMethodSignature } from './decorator.util'; // eslint-disable-next-line @typescript-eslint/naming-convention export function _Timeout(opt) { return (target, key, descriptor) => { _assert(typeof descriptor.value === 'function', '@_Timeout can be applied only to methods'); if (!opt.timeout) return descriptor; const originalFn = descriptor.value; const keyStr = String(key); descriptor.value = async function (...args) { const ctx = this; opt.name || (opt.name = _getMethodSignature(ctx, keyStr)); return await pTimeout(() => originalFn.apply(this, args), opt); }; return descriptor; }; }