A small (3.9KB
minified, 1.5KB
gzipped) and fast event system with no dependencies.
Written in es6 and built for performance. Great as a pubsub or to add event emitters
to your code.
NOTE: Version 2 has landed! Many optimizations have been made and namespace support has been removed. The new code is half the size of the original, is between 200 and 600% faster and has more tests!
npm install signal-js
Add a function to signal using on
and trigger the function using emit
import signal from 'signal-js';
signal.on('basic', arg => console.log(arg);
signal.emit('basic', 1);
// > 1
Add multiple functions to the same event name
import signal from 'signal-js';
signal.on('multiple', () => console.log(1));
signal.on('multiple', () => console.log(2));
signal.on('multiple', () => console.log(3));
signal.trigger('multiple');
// > 1
// > 2
// > 3
Pass as many parameters as you need
import signal from 'signal-js';
signal.on('params', (one, two, three) => console.log(one, two, three));
signal.emit('params', 1, 2, 3);
// > 1 2 3
Remove events using off
import signal from 'signal-js';
signal.on('test', () => console.log('hi'))
.off('test') // removes all `test` events
.emit('test'); // nothing happens
once
can also be used
import signal from 'signal-js';
signal.once('bam', function() {
console.log('Boom!');
});
signal.emit('bam')
// > "Boom!"
signal.emit('bam');
// nothing is logged
The exposed signal is a singleton, but other instances can also be created:
import signal from 'signal-js';
signal.on('foo', () => console.log('global'));
const local = signal();
local.on('foo', () => console.log('local'));
const local2 = local();
local2.on('foo', () => console.log('local2'));
signal.emit('foo');
// > "global"
local.emit('foo');
// > "local"
local2.emit('foo');
// > "local2"
eventName
string The name of the eventlistener
Function The event handler- Returns: signal
Alias: addListener
, subscribe
, bind
eventName
string The name of the eventlistener
Function (optional) The event handler- Returns: signal
If listener
is passed, the specific listener will be unbound,
otherwise all listeners under eventName
will be unbound.
Alias: removeListener
, unsubscribe
, unbind
eventName
string The name of the eventparameters
any (optional) The arguments passed to the listener- Returns: signal
Alias: dispatch
, trigger
eventName
string The name of the eventparameters
any The event handler- Returns: signal
Adds a one-time listener
that will remove itself after being invoked.
eventName
string The name of the event- Returns: Array
Retrieves registered listeners
under the eventName
. If no eventName
is passed, returns all listeners
.
- Returns: Array
Retrieves all eventNames
.
eventName
string The name of the event- Returns: Number
Returns the quantity of listeners
at the given eventName
. If no eventName
is passed, returns the quantity of all listeners
.
- Returns: signal
Disables the signal. All methods can still be accessed and called. Any calls
to emit
will be ignored.
- Returns: signal Enables the signal.
- Returns: signal
Removes all
listeners
andeventNames
from the signal.
The MIT License (MIT)
Copyright (c) 2018 Joseph Clay
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.