@lexriver/type-event
v2.0.0
Published
Typed event class for typescript
Downloads
13
Maintainers
Readme
TypeEvent
Typed event class for typescript
Install
npm install @lexriver/type-event
Import
import {TypeEvent} from '@lexriver/type-event'
Usage
// creating new event
const e = new TypeEvent<(a:number, b:number)=>void>()
let sum = 0
// subscribing to event
e.subscribe((a,b) => {
console.log('event is triggered!', 'a=', a, 'b=', b)
sum = a + b
})
// triggering event
await e.triggerAsync(100,200)
console.log(sum) // 300
Unsubscibe from event
There are two ways to unsubscribe from event.
First way is to return special object {unsubscribe:true}
const e = new TypeEvent()
e.subscribe(() => {
if(someCondition()){
return {unsubscribe: true}
}
})
Second way is to use .unsubscribe(handler)
method on event
const e = new TypeEvent<()=>void>()
let counter = 0
const eventHandler = () => {
console.log('event is triggered')
counter++
}
e.subscribe(eventHandler)
e.triggerAsync()
e.unsubscribe(eventHandler)
e.triggerAsync()
console.log(counter) // 1
Subscribe once
const e = new TypeEvent()
let counter = 0
e.once(() => {
counter++
})
e.triggerAsync()
e.triggerAsync()
console.log(counter) // 1
Count of subscribers
Use .countOfSubscribers
and .countOfOnceSubscribers
properties to get some stats for the event.
const e = new TypeEvent()
e.once(() => {
})
e.once(() => {
})
e.subscribe(() => {
})
console.log(e.countOfSubscribers) // 1
console.log(e.countOfOnceSubscribers) // 2
Unsubscribe all
Unsubscribe all handlers from event
const e = new TypeEvent()
e.subscribe(() => 0)
e.subscribe(() => 1)
e.once(() => 2)
e.unsubscribeAll()
e.countOfSubscribers // 0
e.countofOnceSubscribers // 0