inversify#Container TypeScript Examples
The following examples show how to use
inversify#Container.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: createContainer.ts From reactant with MIT License | 6 votes |
export function createContainer({
ServiceIdentifiers,
modules = [],
options,
}: ContainerConfig) {
setModulesDeps(modules);
const container = new Container(options);
container.applyCustomMetadataReader(new CustomMetadataReader());
bindModules(container, modules);
container.applyMiddleware(createCollector(ServiceIdentifiers));
if (container.isBound(ModuleRef)) {
container.unbind(ModuleRef);
}
container.bind(ModuleRef).toConstantValue(container);
container.bind(defaultUndefinedValue).toConstantValue(undefined);
return container;
}
Example #2
Source File: index.di.ts From NetStatus with MIT License | 6 votes |
exports: IInjectable<IConfig> = {
addBinding: (container) => {
const env = container.get<IEnv>(Symbol.for('IEnv'));
container
.bind<IConfig>(type)
.toConstantValue(config(env));
},
resolve: (container: Container) => {
return container.get<IConfig>(type);
},
type,
}
Example #3
Source File: ioc.ts From NetStatus with MIT License | 6 votes |
newContainer = (): IIOC => {
const container = new Container();
EnvDI.addBinding(container);
ConfigDI.addBinding(container);
LogDi.addBinding(container);
const mapped: IIOC = {
env: () => EnvDI.resolve(container),
config: () => ConfigDI.resolve(container),
logger: () => LogDi.resolve(container),
};
(global as any)[symbol] = mapped;
return mapped;
}
Example #4
Source File: env.di.ts From NetStatus with MIT License | 6 votes |
exports: IInjectable<IEnv> = {
addBinding: (container) => {
container
.bind<IEnv>(type)
.toConstantValue(env);
},
resolve: (container: Container) => {
return container.get<IEnv>(type);
},
type,
}
Example #5
Source File: log.di.ts From NetStatus with MIT License | 6 votes |
exports: IInjectable<ILogger> = {
addBinding: (container) => {
const config = container.get<IConfig>(Symbol.for('IConfig'));
let levels = [];
if ( config.logging.levels.verbose ) {
levels.push(LOGSEVERITY.VERBOSE);
}
if ( config.logging.levels.info ) {
levels.push(LOGSEVERITY.INFO);
}
if ( config.logging.levels.warn ) {
levels.push(LOGSEVERITY.WARN);
}
if ( config.logging.levels.error ) {
levels.push(LOGSEVERITY.ERROR);
}
if ( config.logging.levels.fatal ) {
levels.push(LOGSEVERITY.FATAL);
}
const settings = {
levels,
}
const logger = createLogger(settings);
container
.bind<ILogger>(type)
.toConstantValue(logger);
},
resolve: (container: Container) => {
return container.get<ILogger>(type);
},
type,
}
Example #6
Source File: CommonManagers.ts From rewind with MIT License | 6 votes |
export function createRewindTheater({ apiUrl }: Settings) {
// Regarding `skipBaseClassChecks`: https://github.com/inversify/InversifyJS/issues/522#issuecomment-682246076
const container = new Container({ defaultScope: "Singleton", skipBaseClassChecks: true });
container.bind(TYPES.API_URL).toConstantValue(apiUrl);
container.bind(TYPES.WS_URL).toConstantValue(apiUrl); // Might change in the future
container.bind(STAGE_TYPES.AUDIO_CONTEXT).toConstantValue(new AudioContext());
container.bind(BlueprintService).toSelf();
container.bind(ReplayService).toSelf();
container.bind(SkinLoader).toSelf();
container.bind(SkinHolder).toSelf();
container.bind(AudioService).toSelf();
container.bind(SkinManager).toSelf();
// General settings stores
container.bind(AudioSettingsStore).toSelf();
container.bind(AnalysisCursorSettingsStore).toSelf();
container.bind(BeatmapBackgroundSettingsStore).toSelf();
container.bind(BeatmapRenderSettingsStore).toSelf();
container.bind(HitErrorBarSettingsStore).toSelf();
container.bind(PlayfieldBorderSettingsStore).toSelf();
container.bind(ReplayCursorSettingsStore).toSelf();
container.bind(SkinSettingsStore).toSelf();
container.bind(PlaybarSettingsStore).toSelf();
container.bind(RewindLocalStorage).toSelf();
// Theater facade
container.bind(CommonManagers).toSelf();
return {
common: container.get(CommonManagers),
analyzer: createRewindAnalysisApp(container),
};
}
Example #7
Source File: EsUtils.ts From elasticsearch-index-migrate with MIT License | 6 votes |
export function esClientBind(esConfig: ESConfig): Container {
const container = new Container();
container.bind<ESConnectConfig>(Bindings.ESConfig).toConstantValue(esConfig.connect);
if (esConfig.version === OPENSEARCH) {
container.bind<ElasticsearchClient>(Bindings.ElasticsearchClient).to(OpenSearchClient);
return container;
}
const version = usedEsVersion(esConfig.version)?.major;
if (version) {
switch (version) {
case 6:
container
.bind<ElasticsearchClient>(Bindings.ElasticsearchClient)
.to(Elasticsearch6Client);
break;
case 7:
container
.bind<ElasticsearchClient>(Bindings.ElasticsearchClient)
.to(Elasticsearch7Client);
break;
default:
throw new Error(
`${esConfig.version} is unsupported. support version is 6.x or 7.x.`
);
}
return container;
} else {
throw new Error(`Unknown version:${esConfig.version}. support version is 6.x or 7.x.`);
}
}
Example #8
Source File: App.tsx From mo360-ftk with MIT License | 6 votes |
private setup = once((containerFromContext: IDiContainer | null) => {
const isHotLoaded = containerFromContext !== null;
const defaultContainer = new Container();
const appContainer = defaultContainer.createChild();
if (!isHotLoaded) {
registerDefaultDependencies(defaultContainer, this.props.name);
} else {
defaultContainer.parent = containerFromContext;
registerSwidgetDependencies(defaultContainer, this.props.name);
}
this.useChildrenAsDefaultRouteIfNotSet(defaultContainer);
if (this.props.init) {
const custom = this.props.init(appContainer);
// LEGACY: we need this as the container passed to kernel() in the init func
// returns a custom container - this should not be done in new apps and is deprecated
if (custom && custom !== appContainer) {
return custom;
}
}
return appContainer;
});
Example #9
Source File: ioc-test.ts From elasticsearch-index-migrate with MIT License | 6 votes |
export function openSearchClientContainer() {
const container = new Container();
container.bind<ESConnectConfig>(Bindings.ESConfig).toConstantValue({
host: 'https://admin:admin@localhost:9203',
insecure: true
});
container.bind<ElasticsearchClient>(Bindings.ElasticsearchClient).to(OpenSearchClient);
return container;
}
Example #10
Source File: inversify.config.ts From ng-chrome-extension with MIT License | 6 votes |
createContainer = (): Container => {
const container = new Container({ defaultScope: 'Singleton' });
container.bind<CLI>('CLI').to(CLI);
container.bind<LogService>('LogService').to(LogService);
container.bind<ProjectService>('ProjectService').to(ProjectService);
container.bind<SpinnerService>('SpinnerService').to(SpinnerService);
container.bind<Package>('Package').toConstantValue(require('../package'));
return container;
}
Example #11
Source File: application.ts From regax with MIT License | 5 votes |
protected container = new Container()
Example #12
Source File: inversify.config.ts From che-dashboard-next with Eclipse Public License 2.0 | 5 votes |
testContainer: Container = new Container({ defaultScope: 'Transient' })
Example #13
Source File: inversify.config.ts From che-dashboard-next with Eclipse Public License 2.0 | 5 votes |
container = new Container()
Example #14
Source File: inversify.config.ts From vscode-dbt-power-user with MIT License | 5 votes |
container = new Container()
Example #15
Source File: ioc-test.ts From elasticsearch-index-migrate with MIT License | 5 votes |
export function es6ClientContainer() {
const container = new Container();
container.bind<ESConnectConfig>(Bindings.ESConfig).toConstantValue({
host: 'http://localhost:9201'
});
container.bind<ElasticsearchClient>(Bindings.ElasticsearchClient).to(Elasticsearch6Client);
return container;
}
Example #16
Source File: ioc-test.ts From elasticsearch-index-migrate with MIT License | 5 votes |
export function es7ClientContainer() {
const container = new Container();
container.bind<ESConnectConfig>(Bindings.ESConfig).toConstantValue({
host: 'http://localhost:9202'
});
container.bind<ElasticsearchClient>(Bindings.ElasticsearchClient).to(Elasticsearch7Client);
return container;
}
Example #17
Source File: container.core.ts From rest-api.ts with MIT License | 5 votes |
container = new Container()
Example #18
Source File: ChildContainer.tsx From mo360-ftk with MIT License | 5 votes |
private setupContainer = once((container: IDiContainer) => {
const childContainer = new Container();
childContainer.parent = container;
return childContainer;
});
Example #19
Source File: container-provider.ts From malagu with MIT License | 5 votes |
_container: Container
Example #20
Source File: container.ts From TidGi-Desktop with Mozilla Public License 2.0 | 5 votes |
container = new Container()
Example #21
Source File: config.ts From nodejs-angular-typescript-boilerplate with Apache License 2.0 | 5 votes |
container = new Container()
Example #22
Source File: createContainer.ts From reactant with MIT License | 5 votes |
export function bindModules(container: Container, modules: ModuleOptions[]) {
const provideMeta = getMetadata(METADATA_KEY.provide);
for (const module of modules) {
if (typeof module === 'function') {
// auto decorate `@injectable` for module.
if (!provideMeta.has(module)) decorate(injectable(), module);
autoDecorateParams(module);
container.bind(module).toSelf();
} else if (typeof module === 'object') {
if (isClassProvider(module)) {
// auto decorate `@injectable` for module.useClass
if (!provideMeta.has(module.useClass))
decorate(injectable(), module.useClass);
autoDecorateParams(module.useClass);
container.bind(module.provide).to(module.useClass);
} else if (Object.hasOwnProperty.call(module, 'useValue')) {
container
.bind(module.provide)
.toConstantValue((module as ValueProvider).useValue);
} else if (isFactoryProvider(module)) {
container
.bind(module.provide)
.toFactory((context: interfaces.Context) => {
const deps = module.deps || [];
const depInstances = deps.map((identifier) => {
// TODO: refactor with `is` assertion
const provide =
(identifier as DependencyProviderOption).provide ||
(identifier as ServiceIdentifier<any>);
if (
(identifier as DependencyProviderOption).optional &&
!context.container.isBound(
(identifier as DependencyProviderOption).provide
)
) {
return undefined;
}
return context.container.get(provide);
});
return module.useFactory(...depInstances);
});
} else if (typeof module.provide === 'function') {
// auto decorate `@injectable` for module.provide
if (!provideMeta.has(module.provide))
decorate(injectable(), module.provide);
autoDecorateParams(module.provide);
container.bind(module.provide).toSelf();
} else {
throw new Error(`${module} option error`);
}
} else {
throw new Error(`${module} option error`);
}
}
// load modules with `@injectable` decoration, but without `@optional` decoration.
container.load(autoBindModules());
}
Example #23
Source File: moduleRef.ts From reactant with MIT License | 5 votes |
/**
* > Make sure that `Container` type for getting module.
*/
class ModuleRef extends Container {
//
}
Example #24
Source File: World.ts From GWebGPUEngine with MIT License | 5 votes |
public setContainer(container: Container) {
this.container = container;
}
Example #25
Source File: inversify.config.ts From node-experience with MIT License | 5 votes |
container = new Container()
Example #26
Source File: ioc.config.ts From The-TypeScript-Workshop with MIT License | 5 votes |
container = new Container()
Example #27
Source File: ioc.config.ts From The-TypeScript-Workshop with MIT License | 5 votes |
container = new Container()
Example #28
Source File: ioc.config.ts From The-TypeScript-Workshop with MIT License | 5 votes |
container = new Container()
Example #29
Source File: createRewindAnalysisApp.ts From rewind with MIT License | 5 votes |
/**
* This is a Rewind specific constructor of the "Analysis" tool (not to be used outside of Rewind).
*
* Reason is that many "Rewind" tools share the same services in order to provide smoother experiences.
*
* Example: If I use the "Cutter" tool then I want to use the same preferred skin that is used across Rewind.
*
* The analysis tool can be used as a standalone app though.
*/
export function createRewindAnalysisApp(commonContainer: Container) {
const container = new Container({ defaultScope: "Singleton" });
container.parent = commonContainer;
container.bind(STAGE_TYPES.EVENT_EMITTER).toConstantValue(new EventEmitter2());
container.bind(ReplayManager).toSelf();
container.bind(BeatmapManager).toSelf();
container.bind(GameplayClock).toSelf();
container.bind(ScenarioManager).toSelf();
container.bind(ModSettingsManager).toSelf();
container.bind(GameSimulator).toSelf();
container.bind(PixiRendererManager).toSelf();
// Plugins ?
container.bind(ScreenshotTaker).toSelf();
container.bind(ClipRecorder).toSelf();
container.bind(ReplayWatcher).toSelf(); // Listens to WebSocket
// Assets
container.bind(TextureManager).toSelf();
container.bind(AudioEngine).toSelf();
// Scenes
container.bind(AnalysisSceneManager).toSelf();
container.bind(SceneManager).toSelf();
// Skin is given by above
// container.bind(SkinManager).toSelf();
// AnalysisScenes
container.bind(AnalysisScene).toSelf();
container.bind(IdleScene).toSelf();
// Sliders
container.bind(SliderTextureManager).toSelf();
container.bind(AnalysisStage).toSelf();
{
container.bind(BeatmapBackgroundFactory).toSelf();
container.bind(ForegroundHUDPreparer).toSelf();
container.bind(KeyPressWithNoteSheetPreparer).toSelf();
container.bind(PlayfieldFactory).toSelf();
{
container.bind(PlayfieldBorderFactory).toSelf();
container.bind(HitObjectsContainerFactory).toSelf();
container.bind(HitCircleFactory).toSelf();
container.bind(SliderFactory).toSelf();
container.bind(SpinnerFactory).toSelf();
container.bind(CursorPreparer).toSelf();
container.bind(JudgementPreparer).toSelf();
}
}
container.bind(GameLoop).toSelf();
container.bind(AnalysisApp).toSelf();
return container.get(AnalysisApp);
}