Skip to content

Instantly share code, notes, and snippets.

@aaronpowell
Forked from philipstears/gist:3817035
Created October 3, 2012 07:25

Revisions

  1. aaronpowell revised this gist Oct 3, 2012. 2 changed files with 26 additions and 0 deletions.
    File renamed without changes.
    26 changes: 26 additions & 0 deletions PubSub.ts.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    var PubSub;
    (function (PubSub) {
    var registry = {
    };
    PubSub.Pub = function (name) {
    var args = [];
    for (var _i = 0; _i < (arguments.length - 1); _i++) {
    args[_i] = arguments[_i + 1];
    }
    if(!registry[name]) {
    return;
    }
    registry[name].forEach(function (x) {
    x.apply(null, args);
    });
    };
    PubSub.Sub = function (name, fn) {
    if(!registry[name]) {
    registry[name] = [
    fn
    ];
    } else {
    registry[name].push(fn);
    }
    };
    })(PubSub || (PubSub = {}));
  2. @philipstears philipstears created this gist Oct 2, 2012.
    27 changes: 27 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    module PubSub {
    interface ISubscription {
    (...args: any[]): void;
    }

    interface IDictionary {
    [name: string] : ISubscription[];
    }

    var registry : IDictionary = {
    }

    export var Pub = function(name: string, ...args: any) {
    if (!registry[name]) return;
    registry[name].forEach(x => {
    x.apply(null, args);
    });
    }

    export var Sub = function(name: string, fn: ISubscription) {
    if (!registry[name]){
    registry[name] = [fn];
    } else {
    registry[name].push(fn);
    }
    }
    }