@jtjs/event
v2.0.1
Published
A simple and powerful object-oriented event implementation for Node and browser.
Downloads
32
Maintainers
Readme
A simple, object-oriented event implementation for Node and browser.
Fully typed for beautiful TS development.
How to Use
Create your event:
import { Event } from '@jtjs/event'; type ThemeChangeHandler = (themeName: string) => void; const onThemeChange = new Event<ThemeChangeHandler>();
Subscribe to your event...
Until you unsubscribe:
onThemeChange.subscribe((themeName) => { console.log(`Theme changed to ${themeName}!`); });
For just one trigger:
onThemeChange.once((themeName) => { console.log('Just once!'); });
Trigger your event:
onThemeChange.trigger('light');
Unsubscribe from your event...
With the return from
subscribe
:const unsub = onThemeChange.subscribe((themeName) => { console.log('beep boop'); }); unsub();
With the reference to your handler:
const handler = (themeName: string) => { console.log('beep boop'); }; onThemeChange.subscribe(handler); onThemeChange.unsubscribe(handler);