Events
Import
TypeScriptimport { createEventManager, listen } from 'vanilla-jui';
createEventManager
createEventManager() creates an instance-level event registry. Each listener is identified by a non-empty string key.
Instance Methods
| Method | Return value | Behavior |
|---|---|---|
on(key, target, type, handler, options?) |
() => void |
Unbinds the old listener before rebinding the same key |
off(key) |
boolean |
Unbinds and deletes the record; returns false when missing |
clear() |
void |
Unbinds all recorded listeners |
size() |
number |
Current record count |
TypeScriptconst events = createEventManager();events.on('root-click', root, 'click', handleClick);events.on('window-resize', window, 'resize', handleResize, { passive: true });componentOwn(() => events.clear());
- Repeated keys mean replacement. This is useful when a component rebuilds and binds again. Empty keys throw
TypeError. - The cleanup function returned by
on()only unbinds the underlying event. It does not delete the record from the manager Map. Useoff(key)when the registry must be updated immediately, and useclear()at the end of the lifecycle.
listen
listen(target, type, handler, options?) is syntax sugar for addEventListener.
- Returns an idempotent unbind function.
- Unbinding uses the same
handlerandoptionsused during binding.
TypeScriptconst stop = listen(window, 'resize', () => measure());stop();stop(); // No side effects