Giter VIP home page Giter VIP logo

signals.js's Introduction

Signals.js

A lightweight, super fast, pure javascript event system that works in node and the browser.

API

ย  You can use it as a static mediator for signals....
  Set up a receiver channel....

    function callBack(d) {
        console.log(d);
    }

    Signals.receive("change", callBack);

Or...

    Signals.signaled("change", callBack);

Then transmit it to the receiver(s)...

    var payload = "I changed!";

    Signals.signal("change", payload);
    
        //  I changed!
    
    payload = "I have updates!";
    
    Signals.signal("update", payload);
    
       //  I have updates!

Now kill a receiver channel...

    Signals.dropReceivers("change");

Or, drop all channels at once....

    Signals.dropReceivers();

Or use it to set up observable objects by using prototypal inheritance...

set up the base class...

    function model() {
        var ID = null,
            slf = this;
        this.get = function() {
            return slf.ID;
        };
    };

    var mdl = model;

inherit from Signals....

    mdl.prototype = Signals;
    
    mdl.constructor = model.constructor;

add a new method that emits the signal...

    mdl.prototype.set = function(val) {
        var slf = this;
        if(val && val !== slf.ID) {
          slf.ID = val;
          slf.signal("change", val);
        }
        return slf;
    }

create your observable object....

    var Ident = new mdl();

set up the receiver channel....

    Ident.receive("change", function(d) {
        console.log("change signaled, new ID value: " + d);
    });

assign a value to the ID property....

    Ident.set("ABC");  //  "change signaled, new ID value: ABC"

voila! the message confirms everything works. Now, check the ID...

    var lg = Ident.get();
    
    console.log(lg);  // "ABC"

and there ya go the new value is indeed there... try again with another value...

    Ident.set(123);   //  "change signaled, new ID value: 123"
      
    lg = Ident.get();
    
    console.log(lg);  // 123

now kill the channels...

    Ident.dropReceivers();

Thats all Yo!

signals.js's People

Contributors

still-ill avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Forkers

still2ill

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.