import { AbstractProvider } from '../model/abstract.provider'; const GLOBAL_MAP_NAME: string = 'spellProvider'; export class SandStormService { private static instance: SandStormService = new SandStormService(); private readonly cache: any; private constructor() { this.cache = global[GLOBAL_MAP_NAME] = global[GLOBAL_MAP_NAME] || {}; } public setProvider(provider: AbstractProvider, internalIdentifier?: string): void { if (internalIdentifier) { this.cache[internalIdentifier] = provider; } else { const providerKey: string = provider.constructor.name; if (providerKey) { this.cache[provider.constructor.name] = provider; } else { console.log('\x1b[31m', '\x1b[2m', `The Provider with the name ${providerKey} has already been provided.`, '\x1b[0m'); } } } public getProvider(providerClass: any): AbstractProvider { const providerKey: string = typeof providerClass === 'string' ? providerClass : providerClass.name; return this.cache[providerKey]; } public static getInstance(): SandStormService { return this.instance; } }