// https://gist.github.com/mohsen1/5123533 import { common } from '@material-ui/core/colors' function Reactor() { // @ts-ignore this.events = {} } Reactor.prototype.registerEvent = function(eventName: string) { this.events[eventName] = new (Event as any)(eventName) } Reactor.prototype.dispatchEvent = function(eventName: string, eventArgs: any) { this.events[eventName].callbacks.forEach((callback: (e: any) => void) => { callback(eventArgs) }) } Reactor.prototype.addEventListener = function(eventName: string, callback: (e: any) => void) { if (!this.events[eventName]) { this.registerEvent(eventName) } this.events[eventName].registerCallback(callback) } Reactor.prototype.removeEventListener = function(eventName: string, callback: (e: any) => void) { if (!this.events[eventName]) return this.events[eventName].unregisterCallback(callback) } export function Event(name: string) { // @ts-ignore this.name = name // @ts-ignore this.callbacks = [] ;(window as any)[name] = (e: any) => { // @ts-ignore this.callbacks.forEach(callback => { callback(e) }) } } Event.prototype.registerCallback = function(callback: (e: any) => void) { this.callbacks.push(callback) } Event.prototype.unregisterCallback = function(callback: (e: any) => void) { this.callbacks.pop(callback) } export default Reactor