@osx11/simple-event
v1.0.4
Published
Simple custom events
Downloads
366
Readme
TypeScript SimpleEvent
Example of usage
// SomeService.ts
import {SimpleEvent} from '@osx11/simple-event';
let i = 0;
const someEvent = new SimpleEvent<number>();
const callback = (v: number) => {
console.log(v);
}
someEvent.addEventListener(callback);
setInterval(() => someEvent.emit(++i), 1000);
If you need an event type with
addEventListener
andremoveEventListener
methods only (to prevent unintended emit), you can usesomeEvent.asSubscribeOnlyEvent()
To unsubscribe, use
someEvent.removeEventListener(callback)
or modify the code as following:
const subscription = someEvent.addEventListener(v => console.log(v));
// run where needed
subscription.unsubscribe()