Evmt
Self contained event emitter
Communicate between layers of code without the worry to build a complex solution, thinking of depth, or event injection.
Quick start
Install
npm install --save evmt
Usage
Basic concept
const onAnimalGoes = const animalGoes = { console onAnimalGoes} // Subscribes to the eventconst subscription1 = onAnimalGoesconst subscription2 = onAnimalGoes // Emits to both subscribed functionssubscription2 // Emits to the first subscribed functionsubscription1 // Emits but there is nothing to listen
Advanced usage, with components
const AnimalListComponent = { AnimalService } const AnimalService = onSelect: { console thisonSelect } const FarmComponent = { thisfarmAnimals = thishandleSelectedAnimal = thishandleSelectedAnimal thissubscriptions = AnimalServiceonSelect } { thissubscriptions } { thisfarmAnimals console } FarmComponentAnimalListComponentAnimalListComponentAnimalListComponentFarmComponentAnimalListComponentAnimalListComponent
Documentation
Evmt
It returns it's own instance which is used to subscribe to and emit events.
emit([arg1, ..., argN])
Calls/emits each subscribed callback passing the parameters it received.
Param | Type | Description |
---|---|---|
arg1 ...argN |
any | A list of values to be emitted |
Example
const onSelect = onSelect
subscribe(callback)
Subscribes an callback into an event to wait for the emit. It returns an "subscription" that can removes itself from the subscriptions.
Param | Type | Description |
---|---|---|
callback |
Function | A function to handle the emitted event |
Example
const onSelect = const subscription = onSelect subscription
remove(index)
Removes the subscribed function from the subscriptions by it's index
Param | Type | Description |
---|---|---|
index |
Number | The subscription index to be removed |
Example
const onSelect = const subscription = onSelect // It's the same as subscription.remove()onSelect