diff --git a/src/main.ts b/src/main.ts index 0732631..0f19238 100644 --- a/src/main.ts +++ b/src/main.ts @@ -94,26 +94,27 @@ export default class PluginsAnnotations extends Plugin { } }); - - - const asyncFunc = async (msg: string) => { console.log("Processing:", msg); return `Processed: ${msg}`; }; - console.log("HEY"); - const { debouncedFct, waitFnc } = debounceFactoryWithWaitMechanism(asyncFunc, 1000); + const { debouncedFct, waitFnc } = debounceFactoryWithWaitMechanism(async (msg: string) => { + console.log("Saving settings for:", msg); + await new Promise(resolve => setTimeout(resolve, 500)); // Simulate async work + console.log("Settings saved for:", msg); + }, 1000); - // Make multiple debounced calls, only the last one should execute - debouncedFct("Message 1"); - debouncedFct("Message 2"); - // Wait for the last debounced call to finish + // Trigger debounced function calls + debouncedFct("User 1"); + debouncedFct("User 2"); + + // Wait for the last debounced function call to complete waitFnc().then(() => { console.log("All debounced calls have been completed."); - }).catch(error => { - console.error("An unexpected error occurred:", error); }); + + } /* Load settings for different versions */ diff --git a/src/utils.ts b/src/utils.ts index 88f0b5f..86e71de 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -122,62 +122,43 @@ export function debounceFactory unknown>(func: }; } -export function debounceFactoryWithWaitMechanism ReturnType | Promise>>(func: F, wait: number) { +export function debounceFactoryWithWaitMechanism void | Promise>(func: F, wait: number) { let timeout: ReturnType | null = null; - let promise: Promise> | null = null; + let promise: Promise | null = null; let resolvePromise: (() => void) | null = null; - class CancelledExecution extends Error { - constructor(message = 'Debounced function call was cancelled') { - super(message); - this.name = 'CancelledExecution'; - } - } - return { // Function to wait for the completion of the current debounced call (if any) waitFnc: async (): Promise => { while (promise) { - try { - await promise; - } catch (error) { - if (!(error instanceof CancelledExecution)) { - throw error; // Re-throw if it's not a cancellation - } - // We do nothing if the execution was cancelled - } + await promise; // Await the current promise } }, // The debounced function itself - debouncedFct: (...args: Parameters): Promise> => { + debouncedFct: (...args: Parameters): void => { // Clear the previous timeout to cancel any pending execution if (timeout) { clearTimeout(timeout); } - // Store the previous resolvePromise to resolve it after the new promise is created + // Store the previous resolvePromise to reject it after the new promise is created const previousResolvePromise = resolvePromise; // Create a new promise for the current execution - - promise = new Promise>((resolve, reject) => { - // Now we set the new resolvePromise function + promise = new Promise((resolve, reject) => { + // Set the new resolvePromise function resolvePromise = () => { - reject(new CancelledExecution()); // Reject with a specific cancellation error + console.log("Resolved old promise") + resolve(); // Reject with a specific cancellation error }; - // After the new promise is created, resolve the previous one - if (previousResolvePromise) { - previousResolvePromise(); // Resolve the previous promise to prevent leaks - } - // Schedule the function to run after the debounce delay timeout = setTimeout(async () => { try { // Await the result of the function and cast it to the expected return type - const result = (await func(...args)) as ReturnType; - resolve(result); // Resolve with the result of the function (sync or async) + (await func(...args)) as ReturnType; + resolve(); // Resolve with the result of the function (sync or async) } catch (error) { reject(error); // Reject the promise if the function throws an error } @@ -187,12 +168,18 @@ export function debounceFactoryWithWaitMechanism { private files:TFile[] = [];