1.4.2 • Published 4 years ago
explicit-di v1.4.2
explicit-di
Usage:
// composition root file
import { component, systemMap, using } from 'explicit-di'
import { loggerComponent } from '@logger'
import { messagingComponent } from '@messaging'
const componentMap = {
logger: loggerComponent,
messaging: using(
messagingComponent,
deps: ['logger']
)
}
// @logger
import { FancyLogger } from 'fancy-logger-tool'
import { component } from 'explicit-di'
const loggerClient = new FancyLogger()
export const loggerComponent = component(() => loggerClient)
// @messaging
import { subscribe } from 'fancy-messaging-tool'
import { ILogger } from '@interfaces'
import { component } from 'explicit-di'
interface Deps {
logger: ILogger
}
class MessagingClient {
unsubscribe?: () => {}
start: ({ logger }: Deps) => {
logger.logMessage('Start Listening')
this.unsubscribe = subscribe()
}
stop: () => {
if (this.unsubscribe) this.unsubscribe()
}
}
const messagingClient = new MessagingClient()
export const messagingComponent = component(
messagingClient.start,
messagingClient.stop
)