I recently tried to write a custom directive that did some logic and dispatched an event back to the element it was used on.
//svelte file
<div use:customDiective on:success={handleSuccess}>...</div>
// Custom directive
export const customDirective = node => {
//some synchronous logic here
node.dispatchEvent(new CustomEvent('success', node))
}
What I found out is that since the logic in my directive is synchronous it will dispatch the new custom event before the node is ready to catch it. I was able to easily solve it by using setTimeout(), but that does not seem like a proper solution. Is there any way for me to use a lifecycle method or something in the directive to make sure the component is ready for the dispatched event?