Skip to content

Latest commit

 

History

History
118 lines (62 loc) · 2.66 KB

README.md

File metadata and controls

118 lines (62 loc) · 2.66 KB

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!