#RunRepeatedly - decorator

2 messages · Page 1 of 1 (latest)

tawny swallow
#

hello i want to create a decorator that runs the function every n sec.
i dont really know why i get errors

this is my decorator

export function RunRepeatedly(interval: number): MethodDecorator {
    return function decorator(
        originalMethod: Function,
        context: ClassMethodDecoratorContext,
    ) {
        return function replacementMethod(this: any, ...args: any[]) {
            // Run the method immediately once
            originalMethod.apply(this, args);
            
            // Set up the interval to run the method repeatedly
            const intervalId = setInterval(() => {
                originalMethod.apply(this, args);
            }, interval * 1000); // Convert seconds to milliseconds

            // Store the intervalId on the instance to allow cleanup
            const cleanup = () => clearInterval(intervalId);
            if (this) {
                this.__intervalCleanup = this.__intervalCleanup || [];
                this.__intervalCleanup.push(cleanup);
            }

            return cleanup;
        };
    };
}

and this is the error

Unable to resolve signature of method decorator when called as an expression.
The runtime will invoke the decorator with 2 arguments, but the decorator expects 3.

if someone can help this will very helpful

slow sandal
#

Are you using legacy decoratiors or stage 3 decorators?