Git Product home page Git Product logo

sioevents's Introduction

SioEvents

Events Controller for nodejs module and navigators.

The EventEmitter is preferable used in extended classes.

Install

  npm install sioevents

Include

  import SioEvents from "sioevents";

Full Example

class EventSender extends SioEvents {
    constructor() {
        super();
        /**This listener is going to be fired with all the events */
        this.on("*",(ev)=>{
            console.log("[EventSender] *: ",ev.event,ev.data);
        })
    }
    send(event, data) {
        this.emit(event, data);
    }
    testEmit(t) {
      this.emit("test", {name:t});
    }
}

let sender=new EventSender();

/**The first listener is going to run just the next two times the event its fired and its going to stop further propagation */
sender.on("test",(ev)=>{
  ev.stopPropagation();
  console.log("[First Listener]: ",ev.data);
},2);

/**The second listener is going to run always the event its fired */
sender.on("test",(ev)=>{
  console.log("[Second Listener]: ",ev.data);
});

/**This is going to fire the * listeners */
sender.emit("noTest",{name:"noTest"});

/**This fire 3 test events so we can see all the functionallity of the class */
sender.testEmit("t1");
sender.testEmit("t2");
sender.testEmit("t3");

/**Console
  [EventSender] *:  noTest { name: 'noTest' }
  [First Listener]:  { name: 't1' }    
  [First Listener]:  { name: 't2' }    
  [Second Listener]:  { name: 't3' }   
  [EventSender] *:  test { name: 't3' }
 * 
*/

Add Listener -> .on(eventName, callback,times=Number.POSITIVE_INFINITY);

Name Type Description Special Values
eventName String Name of the event to listen for. Use "*" to listen to all the events fired.
callback Function Function to be called when the event fires. It recives an EventElement Object.
times Number Defines the number of times this callback can run. Default: Infinite times

Add a new listener to listen for a name event

  ev.on("*",(eventElement)=>{
    console.log(eventElement.data);
  })

Emit -> .emit(eventName, data) || .emit(EventElement);

Name Type Description Special Values
eventName String Name of the event to be fired
data Any On the listeners callback EventElement data value.
EventElement EventElement Another EventElement recived in a listener.
  ev.emit("eventName",
    {
      value:"Some special data"
    }
  )

EventElement

Created by SioEvents on every event fire.

Properties

Name Type Default Description
event String It's always setted Event Name
data Any Any Data the event was fired. If you modify this value subsequent listeners will recive the modified value.
stopped Boolean false It its true if one listener calls EventElement.stopPropagation()
error Any false It is setted if one listener calls EventElement.preventDefault(info). If not info is provided this value is true else the value of this is the info param.

Methods

stopPropagation()

The SioEvent is going to stop calling any other listener on the list

preventDefault(info=null)

It's goint to set the error with the info value

NICE DAY AND WONDERFUL CODING

Author: Adrián Mercado Martínez.

Last modification: 2021-10-29

sioevents's People

Contributors

amercadogoblin avatar empirreamm avatar

Watchers

 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.