Created
February 10, 2019 13:13
-
-
Save Stronhold/ef140d8a03e28b50c38be21e9216fdee to your computer and use it in GitHub Desktop.
This file contains 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
import { Injectable, Injector } from '@angular/core'; | |
import { Executioner } from '../model/executioner'; | |
import { ServiceLocator } from '../services/service-locator'; | |
import { MessageTypeEnum } from '../model/message-type.enum'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class RegistryService { | |
private static INSTANCE: RegistryService | undefined = null; | |
private registry: Map<MessageTypeEnum, Executioner> = new Map(); | |
private constructor(private _injector: Injector) { | |
} | |
public static getRegistry(): RegistryService { | |
if (!this.INSTANCE) { | |
this.INSTANCE = new RegistryService(ServiceLocator.injector); | |
} | |
return this.INSTANCE; | |
} | |
register(key: MessageTypeEnum, value: Executioner): void { | |
this.registry.set(key, value); | |
} | |
getRegister(key: MessageTypeEnum): Executioner { | |
if (this.registry.has(key)) | |
return this.registry.get(key); | |
throw Error(`No service registered for key ${key}`) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment