-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor changes - working on DI for event management.
- Loading branch information
Showing
4 changed files
with
1,076 additions
and
905 deletions.
There are no files selected for viewing
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
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
import {container} from 'tsyringe'; | ||
|
||
class CoreContainer { | ||
|
||
private static instance: CoreContainer; | ||
|
||
private constructor() { | ||
|
||
} | ||
|
||
/** | ||
* Get an instance of the container. | ||
*/ | ||
static getInstance() { | ||
if (!CoreContainer.instance) { | ||
CoreContainer.instance = new CoreContainer(); | ||
} | ||
return CoreContainer.instance; | ||
} | ||
|
||
/** | ||
* Fetch dependency | ||
* | ||
* Throws is the dependency does not exist. | ||
* | ||
* @param key | ||
* @throws Error | ||
*/ | ||
public fetch<T>(key: string): T { | ||
const service = container.resolve<T>(key); | ||
|
||
if (!service) { | ||
throw new Error(`Service not found: ${key}`); | ||
} | ||
|
||
return service; | ||
} | ||
|
||
/** | ||
* Register a dependency with the container. | ||
* | ||
* @param key | ||
* @param service | ||
*/ | ||
register<T>(key: string, service: T): void { | ||
container.register(key, { useValue: service }); | ||
} | ||
} |
Oops, something went wrong.