Giter Club home page Giter Club logo

simplevent's Introduction

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;

simplevent's People

Contributors

davidngugi avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.