Skip to content

Instantly share code, notes, and snippets.

@philipstears
Created October 2, 2012 07:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save philipstears/3817035 to your computer and use it in GitHub Desktop.
Save philipstears/3817035 to your computer and use it in GitHub Desktop.
PubSub
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment