https://github.com/aadilhasan/listener.js
A small, light weight event listener, javascript library for browser and node js.
https://github.com/aadilhasan/listener.js
Last synced: 10 months ago
JSON representation
A small, light weight event listener, javascript library for browser and node js.
- Host: GitHub
- URL: https://github.com/aadilhasan/listener.js
- Owner: aadilhasan
- Created: 2018-02-07T05:05:51.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-07T06:56:59.000Z (almost 8 years ago)
- Last Synced: 2025-01-16T10:55:46.648Z (12 months ago)
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Listener.js
It is small and light weight javascript event listener library for for client side js and node js.
## Uses
### Listener.on
on function is used to takes 3 arguments.
* event_name`` **required**,
* callback `` **required**,
* options `` **optional**
#### callback
callback function receives a value passed into emit function.
#### options
###### callOnce (default value: false) `` :
if true listener will be removed after one call.
###### callLimit (default value: 0) ``:
if callLimit value is > 0 listener will be removed after it called callLimit value times.
```
Listener.on('userLoggedIn', function(data){
...do some stuff
}, {callOnce: true});
```
### Listener.emit
on function is used to emit an event, it takes 3 arguments.
* event_name`` **required**,
* data `` **optional**,
* context `` **optional**,
* options `` **optional**
#### context
user can pass a context in which they want the callback function to be executed.
#### data
data can be anything user wants to pass to the listener callback.
#### options
###### removeAfter (default value: false) `` :
if true, the emitted listener will be removed after this.
```
Listener.emmit('userLoggedIn', {}, this, {removeAfter: true});
```
### Listener.broadcast
broadcast function is used to emit more then one event, it takes 3 arguments.
* events `` **required**,
* default_data `` **optional**,
* default_context `` **optional**,
* default_options `` **optional**
#### events
events is an array of strings/objects. value of each item can be a string or an object.
###### string value:
if array item is string the event will be emitted with the default_data, default_context and default_options (if any).
###### object value:
if array item is an object the event will be emitted with the data, context and options given to the object if not default passed value will be used (if any).
this object has 3 keys:
* eventName
* data
* context
* options
#### default_data
default data can be anything user wants to pass to every listener given in first argument array.
#### default_context
default context for all the listeners given in first argument.
#### default_options
###### removeAfter (default value: false) `` :
if true, the emitted listener will be removed after this.
```
Listener.broadcast(['userLoggedIn', 'userCreated', {eventName: 'showNotificatons', data: {}, options: {callLimit: 2}}, 'getUserData'], {}, this);
```