Widget Events
Handle widget mounting and destruction with ON_SCENA_MOUNT and ON_SCENA_DESTROY.
Widget events fire when the scena instance is mounted into the DOM and when it is destroyed. They carry no payload and require no arguments in the handler. These events are useful for running setup or teardown logic tied to the widget's DOM presence.
Events
| Event | String value | When |
|---|---|---|
ON_SCENA_MOUNT | scena:on-mount | Widget component is mounted and ready |
ON_SCENA_DESTROY | scena:on-destroy | Widget is about to be destroyed, before cleanup |
Usage
import { ScenaEvent } from '@retoo/scena';
instance.api.events.on(ScenaEvent.ON_SCENA_MOUNT, () => {
console.log('Widget is ready');
});
instance.api.events.on(ScenaEvent.ON_SCENA_DESTROY, () => {
console.log('Widget is being destroyed');
});
Unsubscribing
Pass the same handler reference to off():
const onMount = () => console.log('Mounted');
instance.api.events.on(ScenaEvent.ON_SCENA_MOUNT, onMount);
// Later
instance.api.events.off(ScenaEvent.ON_SCENA_MOUNT, onMount);
Notes
ON_SCENA_MOUNTfires after the Svelte component is mounted — the DOM is fully rendered at this point, so it is safe to query elements or initialize libraries that depend on the DOM.ON_SCENA_DESTROYfires before cleanup. After this event, the event emitter is cleared and all handlers are removed automatically, so there is no need to unsubscribe manually.