Running your program
To run your program, you need to use the provide
function and call its
return value.
import * as DI from 'diabolo'
DI.provide(mainFunction, {
MyService: myServiceImpl
})()
Your provided dependencies can also be a promise. It may be useful if you dynamically import one of your services.
import * as DI from 'diabolo'
async function getImpls() {
if (runtime.isNode) {
const { default: myServiceImpl } = await import('./myService.node.js')
return { MyService: myServiceImpl }
} else {
const { default: myServiceImpl } = await import('./myService.browser.js')
return { MyService: myServiceImpl }
}
}
DI.provide(mainFunction, getImpls())()
The provide
function is type-safe. If you forget to provide a service, you
will get a TypeScript error.
I decided to export a provide
function instead of a run
. This way, you
can provide the services you want and call the function later.
import * as DI from 'diabolo'
const providedMainFunction = DI.provide(mainFunction, {
MyService: myServiceImpl
})
providedMainFunction()