|
| 1 | +import * as contextsUtils from '../utils'; |
| 2 | +import * as contextsErrors from '../errors'; |
| 3 | +import Timer from '../../timer/Timer'; |
| 4 | +import * as timerErrors from '../../timer/errors'; |
| 5 | +import { |
| 6 | + AsyncFunction, |
| 7 | + GeneratorFunction, |
| 8 | + AsyncGeneratorFunction |
| 9 | +} from '../../utils'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Timed method decorator |
| 13 | + */ |
| 14 | +function timed(delay: number = Infinity) { |
| 15 | + return ( |
| 16 | + target: any, |
| 17 | + key: string | symbol, |
| 18 | + descriptor: TypedPropertyDescriptor<(...params: any[]) => any> |
| 19 | + ): TypedPropertyDescriptor<(...params: any[]) => any> => { |
| 20 | + const targetName = (target['name'] ?? target.constructor.name); |
| 21 | + const f = descriptor['value']; |
| 22 | + if (typeof f !== 'function') { |
| 23 | + throw new TypeError(`\`${targetName}.${key.toString()}\` is not a function`); |
| 24 | + } |
| 25 | + const contextIndex = contextsUtils.contexts.get(target[key]); |
| 26 | + if (contextIndex == null) { |
| 27 | + throw new TypeError(`\`${targetName}.${key.toString()}\` does not have a \`@context\` parameter decorator`); |
| 28 | + } |
| 29 | + const wrap = (that: any, params: Array<any>) => { |
| 30 | + const context = params[contextIndex]; |
| 31 | + if (context !== undefined && (typeof context !== 'object' || context === null)) { |
| 32 | + throw new TypeError( |
| 33 | + `\`${targetName}.${key.toString()}\` decorated \`@context\` parameter is not a context object` |
| 34 | + ); |
| 35 | + } |
| 36 | + if (context?.timer !== undefined && !(context.timer instanceof Timer)) { |
| 37 | + throw new TypeError( |
| 38 | + `\`${targetName}.${key.toString()}\` decorated \`@context\` parameter's \`timer\` property is not an instance of \`Timer\`` |
| 39 | + ); |
| 40 | + } |
| 41 | + if (context?.signal !== undefined && !(context.signal instanceof AbortSignal)) { |
| 42 | + throw new TypeError( |
| 43 | + `\`${targetName}.${key.toString()}\` decorated \`@context\` parameter's \`signal\` property is not an instance of \`AbortSignal\`` |
| 44 | + ); |
| 45 | + } |
| 46 | + // Now `context: { timer: Timer | undefined; signal: AbortSignal | undefined } | undefined` |
| 47 | + if ( |
| 48 | + context === undefined || |
| 49 | + context.timer === undefined && context.signal === undefined |
| 50 | + ) { |
| 51 | + const abortController = new AbortController(); |
| 52 | + const timer = new Timer({ |
| 53 | + delay, |
| 54 | + handler: () => void abortController.abort(new contextsErrors.ErrorContextsTimerExpired) |
| 55 | + }); |
| 56 | + params[contextIndex] = (context !== undefined) ? context : {}; |
| 57 | + params[contextIndex].signal = abortController.signal; |
| 58 | + params[contextIndex].timer = timer; |
| 59 | + const result = f.apply(that, params); |
| 60 | + timer.catch((e) => { |
| 61 | + // Ignore cancellation |
| 62 | + if (!(e instanceof timerErrors.ErrorTimerCancelled)) { |
| 63 | + throw e; |
| 64 | + } |
| 65 | + }); |
| 66 | + timer.cancel(); |
| 67 | + return result; |
| 68 | + } else if ( |
| 69 | + context.timer === undefined && |
| 70 | + context.signal instanceof AbortSignal |
| 71 | + ) { |
| 72 | + const abortController = new AbortController(); |
| 73 | + const timer = new Timer({ |
| 74 | + delay, |
| 75 | + handler: () => void abortController.abort(new contextsErrors.ErrorContextsTimerExpired) |
| 76 | + }); |
| 77 | + context.signal.onabort = () => void abortController.abort(context.signal.reason); |
| 78 | + params[contextIndex].signal = abortController.signal; |
| 79 | + params[contextIndex].timer = timer; |
| 80 | + const result = f.apply(that, params); |
| 81 | + timer.catch((e) => { |
| 82 | + // Ignore cancellation |
| 83 | + if (!(e instanceof timerErrors.ErrorTimerCancelled)) { |
| 84 | + throw e; |
| 85 | + } |
| 86 | + }); |
| 87 | + timer.cancel(); |
| 88 | + return result; |
| 89 | + } else if ( |
| 90 | + context.timer instanceof Timer && |
| 91 | + context.signal === undefined |
| 92 | + ) { |
| 93 | + const abortController = new AbortController(); |
| 94 | + context.timer.then(() => void abortController.abort(new contextsErrors.ErrorContextsTimerExpired)); |
| 95 | + params[contextIndex].signal = abortController.signal; |
| 96 | + return f.apply(that, params); |
| 97 | + } else if ( |
| 98 | + context.timer instanceof Timer && context.signal instanceof AbortSignal |
| 99 | + ) { |
| 100 | + return f.apply(that, params); |
| 101 | + } |
| 102 | + }; |
| 103 | + if (f instanceof AsyncFunction) { |
| 104 | + descriptor['value'] = async function (...params) { |
| 105 | + return wrap(this, params); |
| 106 | + }; |
| 107 | + } else if (f instanceof GeneratorFunction) { |
| 108 | + descriptor['value'] = function* (...params) { |
| 109 | + return yield* wrap(this, params); |
| 110 | + }; |
| 111 | + } else if (f instanceof AsyncGeneratorFunction) { |
| 112 | + descriptor['value'] = async function* (...params) { |
| 113 | + return yield* wrap(this, params); |
| 114 | + }; |
| 115 | + } else { |
| 116 | + descriptor['value'] = function (...params) { |
| 117 | + return wrap(this, params); |
| 118 | + }; |
| 119 | + } |
| 120 | + // Preserve the name |
| 121 | + Object.defineProperty(descriptor['value'], 'name', { |
| 122 | + value: (typeof key === 'symbol') ? `[${key.description}]` : key |
| 123 | + }); |
| 124 | + return descriptor; |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +export default timed; |
0 commit comments