Check migration file to 1.0.0.
npm install psmitter --save
You can use this module of many ways:
- By minFile:
<!-- index.html -->
<script src="psmiter.min.js" type="text/javascript"></script>
// file.js
let myEmitter = new Psmitter()
- By require
// file.js
const Psmitter = require('psmitter').Psmitter
let myEmitter = new Psmitter()
- By webpack
Only add just with the others vendors files
// webpack.config.js
var vendors = [
...,
'psmitter'
]
// file.js
let myEmitter = new Psmitter()
Register a listener for one or more events
- event | [eventA, eventB] -
string|Array
: Event name or array of events names - fn -
function
: Callback function
Dispatch a event
- event -
string
: Event name for dispatch - data -
mixed
: Data for send when the event is emitted.
Register a listener that will be executed only once
- event | [eventA, eventB] -
string|Array
: Event name or array of events names - fn -
function
: Callback function
Get a list of event names in the current Psmitter instance
- array : The list of events names
Get a listeners list binded to determinate event
- event -
string
: Event name
- array : The list of listeners
Number of listeners registered to an event
- event -
string
: Event name
- number : Number of listeners registered
Remove an listener of a determinate event
- event -
string
: Event name - fn -
function
: Callback function to remove
Remove all listeners of a event
- event -
string
: Event name
Remove the last listener of an event
- event -
string
: Event name
Check if exists almost one listener registered to a event
- event -
string
: Event name
- boolean : Return
true
if contains some listener, otherwise returnfalse
If you need to emit and listen some data
// button[id="message"]
var button = document.getElementById('message')
button.addEventListener('click', function () {
myEmitter.emit('message', 'hello world from Psmitter')
})
myEmitter.on('message', function (data) {
console.log(data)
})
If you needs to remove and newly register an listener
<body>
<input type="button" value="Remove listener" id="removeListener">
<input type="button" value="Register Listener" id="registerListener">
<div id="time"></div>
</body>
var time = document.getElementById('time')
var commonListener = function (data) {
time.innerHTML = data
}
myEmitter.on('time', commonListener)
var removeButton = document.getElementById('removeListener')
removeButton.addEventListener('click', function () {
myEmitter.removeListener('time', commonListener)
})
var registerButton = document.getElementById('registerListener')
registerButton.addEventListener('click', function () {
myEmitter.on('time', commonListener)
})
setInterval(function () {
myEmitter.emit('time', new Date())
}, 1000)
You have a improvement? or found a bug? See how to contribute
If you are using this library in one of your projects, add it in this list.
proyect name with link
(Author) ---> description
[MIT][license] Β© [Fernando Palacios]