Skip to content

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

Notifications You must be signed in to change notification settings

Still2Ill/Signals.js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

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!

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%