-
-
Save aaronpowell/3825576 to your computer and use it in GitHub Desktop.
PubSub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment