Conversation
|
@madeindjs You always share some good advice :) Happy to provide more context for these features if necessary. |
| setTimeout(() => { | ||
| // Debouncing | ||
| if (component.x !== newX) return; | ||
| if (component.y !== newY) return; | ||
| changeCoordinates(componentId, newX, newY); | ||
| }, 200); |
There was a problem hiding this comment.
If it's a debouncer you want, you might need to cancel the previous setTimeout using a method like
function useDebouncer<Args extends Array<unknown>, Return>(
callback: (...args: Args) => void,
ms: number,
): (...args: Args) => void {
let id: ReturnType<typeof setTimeout>;
return (...args: Args) => {
if (id) clearTimeout(id);
id = setTimeout(() => callback(...args), ms);
};
}
const changeCoordinatesDebounced = useDeboucer(changeCoordinates, 200);and then
| setTimeout(() => { | |
| // Debouncing | |
| if (component.x !== newX) return; | |
| if (component.y !== newY) return; | |
| changeCoordinates(componentId, newX, newY); | |
| }, 200); | |
| changeCoordinatesDebounced(componentId, newX, newY) |
There was a problem hiding this comment.
Yes that was the other alternative but I wanted to avoid having a single timer id for all components. So I'd have needed Record<Component["id"], ReturnType<typeof setTimeout>> and I wanted to avoid.
However, in the example you declare id inside the function. If it works that'd be great, but how would that behave? Wouldn't future calls get a completely new id since it's scoped to the function?
| postChildren.forEach((c) => { | ||
| if (!c.outs || c.outs.length == 0) return; | ||
| c.outs = c.outs.filter((out) => out.toNodeId !== removedId); | ||
| await nextTick(); |
There was a problem hiding this comment.
Why is this nextTick necessary ? It's a bit dangerous to use it inside a watch, no ?
There was a problem hiding this comment.
Because I need to have everything reflected on the DOM before I call refreshArrows, since this function calls getBoundingClientRect() to get precise coordinates.
Co-authored-by: Alexandre Rousseau <alexandre@rsseau.fr>
Co-authored-by: Alexandre Rousseau <alexandre@rsseau.fr>
Co-authored-by: Alexandre Rousseau <alexandre@rsseau.fr>
Co-authored-by: Alexandre Rousseau <alexandre@rsseau.fr>
No description provided.