Anonymous Anonymous - 19 days ago
196 0

stuff

TypeScript

noise

module Routier {
    
    export enum EventType {
        ContactCreated,
        RouteCalculated,
        RouteSaved
    }

    export interface IObserver {
        notify(eventName: EventType, data: Object);
        subscribe(eventName: EventType, callback: (object) => void);
    }

    export class EventBus implements IObserver {
        private subscribers;
        private static _instance: EventBus = null;

        constructor() {            
            this.subscribers = [];            
        }

        public static getInstance():EventBus{
            if (EventBus._instance === null) {
                EventBus._instance = new EventBus();
            }
            return EventBus._instance;
        }

        public subscribe(eventName, callback) {
            if (this.subscribers[eventName] === undefined) {
                this.subscribers[eventName] = [];
            }

            this.subscribers[eventName].push(callback);
        }

        public notify(eventName, parameters) {
            for (var i = 0; i < this.subscribers[eventName].length; i++) {
                this.subscribers[eventName][i](parameters);
            }
        }
    }    
}