Skip to content
/ emitter Public

Offer a simple yet useful way of communication between components.

License

Notifications You must be signed in to change notification settings

wide/emitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Event Emitter

Offer a simple yet useful way of communication between components.

Install

npm install @wide/emitter --save

Listen events

import emitter from '@wide/emitter'

emitter.on('test', (a, b, c) => {})

or outside your script (without access to emitter)

document.onEvent('test', (1, 2, 3) => {})

##3 Listen event once only

import emitter from '@wide/emitter'

emitter.on('test', (a, b, c) => {}, { once: true })
// or
document.onEvent('test', (1, 2, 3) => {}, { once: true })

Emit events

import emitter from '@wide/emitter'

emitter.emit('test', 1, 2, 3)
// or
document.emitEvent('test', 1, 2, 3)

Unlisten event

import emitter from '@wide/emitter'

const ref = emitter.on('test', (a, b, c) => {})

emitter.off(ref)

Behind the scene

This emitter does not re-create an event dispatcher, but instead make use of document own event system, for exemple:

emitter.emit('test', 1, 2, 3)
// or
document.emitEvent('test', 1, 2, 3)

becomes:

document.dispatchEvent(new CustomEvent('@test', { detail: [1, 2, 3] }))

Authors

License

This project is licensed under the MIT License - see the licence file for details

About

Offer a simple yet useful way of communication between components.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published