├── src ├── main.ts └── event-bus │ └── event-bus.ts ├── package.json └── tsconfig.json /src/main.ts: -------------------------------------------------------------------------------- 1 | import { EventBus } from './event-bus/event-bus'; 2 | 3 | const registry = EventBus.getInstance().register('hello-world', (name: string) => { 4 | if(name) 5 | console.log('Hello ' + name); 6 | else 7 | console.log('Hello world'); 8 | }); 9 | 10 | 11 | EventBus.getInstance().dispatch('hello-world', 'Luis'); 12 | EventBus.getInstance().dispatch('hello-world'); 13 | 14 | registry.unregister(); 15 | EventBus.getInstance().dispatch('hello-world'); 16 | 17 | 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "event-bus-typescript", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "tsc && node dist/main.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/luixaviles/event-bus-typescript.git" 12 | }, 13 | "keywords": [], 14 | "author": "", 15 | "license": "MIT", 16 | "bugs": { 17 | "url": "https://github.com/luixaviles/event-bus-typescript/issues" 18 | }, 19 | "homepage": "https://github.com/luixaviles/event-bus-typescript#readme" 20 | } 21 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ 4 | "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ 5 | "outDir": "./dist", /* Redirect output structure to the directory. */ 6 | "strict": true, /* Enable all strict type-checking options. */ 7 | "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ 8 | "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/event-bus/event-bus.ts: -------------------------------------------------------------------------------- 1 | export interface Registry { 2 | unregister: () => void; 3 | } 4 | 5 | export interface Callable { 6 | [key: string]: Function; 7 | } 8 | 9 | export interface Subscriber { 10 | [key: string]: Callable; 11 | } 12 | 13 | export interface IEventBus { 14 | dispatch(event: string, arg?: T): void; 15 | register(event: string, callback: Function): Registry; 16 | } 17 | 18 | export class EventBus implements IEventBus { 19 | private subscribers: Subscriber; 20 | private static nextId = 0; 21 | private static instance?: EventBus = undefined; 22 | 23 | private constructor() { 24 | this.subscribers = {}; 25 | } 26 | 27 | public static getInstance(): EventBus { 28 | if (this.instance === undefined) { 29 | this.instance = new EventBus(); 30 | } 31 | 32 | return this.instance; 33 | } 34 | 35 | public dispatch(event: string, arg?: T): void { 36 | const subscriber = this.subscribers[event]; 37 | 38 | if (subscriber === undefined) { 39 | return; 40 | } 41 | 42 | Object.keys(subscriber).forEach((key) => subscriber[key](arg)); 43 | } 44 | 45 | public register(event: string, callback: Function): Registry { 46 | const id = this.getNextId(); 47 | if (!this.subscribers[event]) this.subscribers[event] = {}; 48 | 49 | this.subscribers[event][id] = callback; 50 | 51 | return { 52 | unregister: () => { 53 | delete this.subscribers[event][id]; 54 | if (Object.keys(this.subscribers[event]).length === 0) 55 | delete this.subscribers[event]; 56 | }, 57 | }; 58 | } 59 | 60 | private getNextId(): number { 61 | return EventBus.nextId++; 62 | } 63 | } 64 | --------------------------------------------------------------------------------