Created
November 21, 2015 09:22
-
-
Save ichpuchtli/57dfd220faebbb92c5dd to your computer and use it in GitHub Desktop.
A super dumb pub sub implementation
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
export interface PubSubCallback | |
{ | |
(args?: any, topic?: any) : void; | |
} | |
interface PubSubTopicMap | |
{ | |
[topic : string]: PubSubCallback[]; | |
} | |
class DumbPubSubStub | |
{ | |
public static debugging: boolean = true; | |
private static map : PubSubTopicMap = {}; | |
public static Publish(topic: any, args?: any) | |
{ | |
if (DumbPubSubStub.debugging) | |
{ | |
console.info(`publish: ${topic} -> `, args); | |
} | |
if (DumbPubSubStub.map[topic]) | |
{ | |
DumbPubSubStub.map[topic].forEach(callback => callback(args || {}, topic)); | |
} | |
} | |
public static Unsubscribe(topic: any, func: PubSubCallback): void | |
{ | |
if (DumbPubSubStub.debugging) | |
{ | |
console.info(`unsubscribe: ${topic}`); | |
console.trace(); | |
} | |
DumbPubSubStub.map[topic].splice(DumbPubSubStub.map[topic].indexOf(func), 1); | |
} | |
public static Subscribe(topic: any, callback: PubSubCallback) | |
{ | |
if (DumbPubSubStub.debugging) | |
{ | |
console.info(`subscribe: ${topic}`); | |
console.trace(); | |
} | |
DumbPubSubStub.map[topic] = DumbPubSubStub.map[topic] || []; | |
DumbPubSubStub.map[topic].push(callback); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment