/** * Created by mm28969 on 10/25/16. */ import {Channel} from "./channel"; export class Dispatcher { public static getInstance(): Dispatcher { return Dispatcher._instance; } private static _instance: Dispatcher = new Dispatcher(); channelMap: any; constructor() { this.channelMap = {}; } createTopic(topic: string): Dispatcher { this.channelMap[topic] = new Channel(); return this; } subscribe(topic: string, observerId, observer: Function): Dispatcher { let channel: Channel = this.channelMap[topic]; if (!channel) { channel = new Channel(); this.channelMap[topic] = channel; } channel.subscribe(observerId, observer); return this; } unsubscribe(topic: string, observerId, observer: Function): Dispatcher { let channel: Channel = this.channelMap[topic]; if (channel) { channel.unsubscribe(observerId, observer); } return this; } notify(topic: string, payload: any = {}): Dispatcher { let channel: Channel = this.channelMap[topic]; if (channel) { channel.notify(payload); } return this; } get details() { let topic, channel, detailsMap = {}; for(topic in this.channelMap){ if (this.channelMap.hasOwnProperty(topic)) { channel = this.channelMap[topic]; detailsMap[topic] = channel.details; } } return detailsMap; } }