Skip to content

Simple Signals implemented in Typescript - Statically Typed For U!

License

Notifications You must be signed in to change notification settings

staticfunction/kola-signals

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kola Signals

Statically-Typed For U implementation of signals. Use with Typescript but you can also use it for Javascript/Coffeescript.

Install via:

    npm install kola-signals --save

Quick guide

import signals = require('kola-signals');

var messenger:signals.Dispatcher<string> = new signals.Dispatcher<string>();

var receiver = (msg: string) => {
    console.log("message received!", msg);
}

messenger.listen(receiver);
messenger.dispatch("Hello Awesomeness!");

Use it in classes

import signals = require('kola-signals');

var smsService = new signals.Dispatcher<string>();

class Phone {

    msgs: string[];

    constructor() {
        this.msgs = [];
    }

    onReceiveMsg(msg:string):void {
        console.log("Message received!", msg);
        this.msgs.push(msg);
    }
}

var phone: Phone = new Phone();

smsService.listen(phone.onReceiveMsg, phone);

smsService.dispatch("Hi there!");

NOTE: In this example, we pass in the 'phone' as second argument for listen(). It allows the onReceiveMsg() to be called with 'this' as the 'phone' instance.

Call once

import signals = require('kola-signals');

smsDispatcher.listen(onceAFunction, null, true);

Unlisten

The listen() method of signals.Dispatcher returns an instance of signals.Listener. You can use this instance to call unlisten() like so:

var listener = smsService.listen(onReceiveMsg);

...

listener.unlisten();

About

Simple Signals implemented in Typescript - Statically Typed For U!

Resources

License

Stars

Watchers

Forks

Packages

No packages published