mirror of
https://github.com/alberti42/obsidian-plugins-annotations.git
synced 2026-07-22 10:10:24 +00:00
Simplified
This commit is contained in:
parent
59da6411e4
commit
cec5b3bfb8
2 changed files with 31 additions and 43 deletions
23
src/main.ts
23
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 */
|
||||
|
|
|
|||
51
src/utils.ts
51
src/utils.ts
|
|
@ -122,62 +122,43 @@ export function debounceFactory<F extends (...args: unknown[]) => unknown>(func:
|
|||
};
|
||||
}
|
||||
|
||||
export function debounceFactoryWithWaitMechanism<F extends (...args: unknown[]) => ReturnType<F> | Promise<ReturnType<F>>>(func: F, wait: number) {
|
||||
export function debounceFactoryWithWaitMechanism<F extends (...args: unknown[]) => void | Promise<void>>(func: F, wait: number) {
|
||||
let timeout: ReturnType<typeof setTimeout> | null = null;
|
||||
let promise: Promise<ReturnType<F>> | null = null;
|
||||
let promise: Promise<void> | 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<void> => {
|
||||
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<F>): Promise<ReturnType<F>> => {
|
||||
debouncedFct: (...args: Parameters<F>): 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<ReturnType<F>>((resolve, reject) => {
|
||||
// Now we set the new resolvePromise function
|
||||
promise = new Promise<void>((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<F>;
|
||||
resolve(result); // Resolve with the result of the function (sync or async)
|
||||
(await func(...args)) as ReturnType<F>;
|
||||
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<F extends (...args: unknown[])
|
|||
resolvePromise = null;
|
||||
}, wait);
|
||||
});
|
||||
// Return the promise, allowing the caller to await the debounced execution
|
||||
return promise;
|
||||
|
||||
console.log("Created new promise");
|
||||
|
||||
// After the new promise is created, reject the previous one
|
||||
if (previousResolvePromise) {
|
||||
previousResolvePromise(); // Reject the previous promise with the custom error
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/* File suggestions */
|
||||
export class FileSuggestion extends AbstractInputSuggest<TFile> {
|
||||
private files:TFile[] = [];
|
||||
|
|
|
|||
Loading…
Reference in a new issue