- Docs
- destroyToaster
destroyToaster
Unmount the singleton toaster, publish its dismissals, and clear the registry.
Unmount the singleton toaster and drop the toasts it was rendering. It is a no-op when no toaster is mounted.
Signature
function destroyToaster(): void
Returns
void. The call is idempotent.
Behaviour
- Calls
toast.dismiss()while the renderer is still subscribed, so every active toast publishes its dismissal and the dismissal callbacks run before the listeners go away. - Calls
controller.destroy()on the singleton. That clears every active timer, removes every event listener, and removes the container from the DOM. - Clears the singleton slot, so
getToaster()returnsnullafterwards.
The state history is not cleared by this call. toast.getToasts() returns an empty array, while toast.getHistory() still lists the dismissed records (up to the newest 50) until resetToasts() runs.
Examples
Router cleanup
import { createToaster, destroyToaster } from '@samline/notify'
router.on('/profile', () => {
createToaster({ position: 'bottom-right' })
})
router.off('/profile', () => {
destroyToaster()
})
Test setup
import { destroyToaster } from '@samline/notify'
beforeEach(() => {
destroyToaster()
})
destroyToaster is a no-op when no toaster is mounted, so it is safe to call at the top of every test.
Manual unmount
import { createToaster, destroyToaster } from '@samline/notify'
createToaster({ position: 'top-right' })
// ...later, when the page unmounts...
destroyToaster()
Edge cases
- No-op when nothing is mounted. It does not throw, it returns immediately.
- Nothing remounts afterwards. Call
createToaster()again before you show more visible toasts. - Direct-mount containers are not destroyed. Global-state mounts still receive the dismissals published by this function. Keep each direct controller and call its destroy() when its container should also be removed. Injected state can isolate a mount from global dismissals.
Related
createToaster— mount the singleton.getToaster— read the singleton.resetToasts— drop toasts without unmounting the container.