Skip to content

DavidNgugi/Simplevent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

THE SIMPLE EVENT LIBRARY

This is a small simple yet effective JavaScript Library that emulates the NodeJs EventEmitter. It's built for projects that require Event-driven programming techniques like simple HTML5 Canvas games etc.

Installation in Browser

For use on the browser, use simplevent.min.js found in the dist folder.

Installation in NodeJs

For use in a NodeJs project, instantiation is not required

npm install simplevent

Using Simplevent

Simplevent is simple just like any other event system and uses two main methods, on() and dispatch();

Adding it into your project

Simplevent can be added as a module just like any other mordern NodeJs package

When using it on the browser, instantiate Simplevent first.

const Simplevent = new Simplevent();

When using node js, just require the module.

const Simplevent = require('simplevent');

Registering an event

Simplevent's on() method takes 3 arguments, an EVENT<[String]> and a listening function which can be a callback LISTENER<[Function]> and an optional target argument which describes the context for execution TARGET?<[Object|Function]>

When using a callback function as a listener

Simplevent.on('GET_EVENT', function(){
    console.log('Registered the damn event!');
});

When using a normal function as the listener

function shout() {
    console.log('HEY YOU');
}

Simplevent.on('SHOUT', shout);

Dispatching/Firing an event

Simplevent.dispatch('GET_EVENT');

You can pass unlimited number of arguments to the listening function as shown below

Simplevent.on('EVENT_WITH_ARGS', function(name){
    console.log(name);
});

Simplevent.dispatch('EVENT_WITH_ARGS', 'Mr. Simplevent');

Turning off an event listener

Use the off() method.

Simplevent.off('SHOUT', shout);

Clearing events

You can clear all events

Simplevent.clear();

Method Chaining

You can chain the methods instead of writing them seperately

var result = 4;

var addOne = () => { result += 1; }

var multipyByThree = () => { result *= 3; }

var divideByFour = () => { result /= 4; }

Simplevent.on('ADD', addOne)
        .on('MULTIPLY', multipyByThree)
        .on('DIVIDE', divideByFour)
        .dispatch('MULTIPLY');

Licencing

Licensed using the MIT Licence and therefore free for commercial purposes;

About

A simple Event Emitter library for JavaScript

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published