Djent
Djent

Reputation: 3467

Checking if somebody is handling the svelte component's event

I have a component, that dispatched some event. Let's say it's cleared. This event is optional, it can be called like below:

<MyComponent />
<MyComponent on:cleared={smth} />

Is it possible to check within my component if the caller is expecting the event to happen? I know I can change the component call to something like that: <MyComponent on:cleared={smth} expectingClear={true} />, but that is a bit error prone.

Upvotes: 3

Views: 522

Answers (1)

brunnerh
brunnerh

Reputation: 184376

This is not possible to my knowledge.

There also would be the issue of when to check. The event listener can be added at any point (though via creation as part of a template it happens right after creation), e.g.

const component = new MyComponent(...);
setTimeout(() => component.$on('cleared', ...), 3000);

Upvotes: 2

Related Questions