zowe / zowe-cli

Zowe CLI
Eclipse Public License 2.0
114 stars 86 forks source link

Add debounce utility method to Core SDK #2364

Open t1m0thyj opened 2 hours ago

t1m0thyj commented 2 hours ago

Is your feature or enhancement request related to a problem or limitation? Please describe

I wish the Core SDK provided a method that could be used to debounce Node.js fs.watch events.

Describe your enhancement idea

Add a debounce utility method - this could be implemented as a decorator like the following:

function Debounce(delay: number) {
    return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
        let timeoutId: ReturnType<typeof setTimeout>;
        const originalMethod = descriptor.value;

        descriptor.value = function (...args: any[]) {
            if (timeoutId) {
                clearTimeout(timeoutId);
            }
            timeoutId = setTimeout(() => originalMethod.apply(this, args), delay);
        };

        return descriptor;
    };
}

Describe alternatives you've considered

Currently the Imperative Event Emitter and Zowe Explorer have each implemented a solution for debouncing events, but it would be nice if they could share a common one.

Provide any additional context

Example of how to use the decorator above:

class MyClass {
    @Debounce(100)
    public static myMethod() {
        console.log("Method 1 called");
    }

    @Debounce(100)
    public static myOtherMethod() {
        console.log("Method 2 called");
    }
}

MyClass.myMethod();
MyClass.myOtherMethod();
MyClass.myMethod();
github-actions[bot] commented 2 hours ago

Thank you for raising this enhancement request. The community has 90 days to vote on it. If the enhancement receives at least 5 upvotes, it is added to our development backlog. If it receives fewer votes, the issue is closed.