Creating services
A service is a type that represents an object that can be injected. This object is a record of anything you want. It can contain functions, variables, or anything else.
To create a service in Diabolo, you need to use the Service
type and
the createService
function.
import * as DI from 'diabolo'
interface MyService extends DI.Service<
'MyService', // The name of the service
{
myFunction: () => void
} // The type of the service
> { }
const myService = DI.createService<MyService>('MyService')
If one of your functions needs one or more other services, you can use
the WithServices
type.
import * as DI from 'diabolo'
interface MyService extends DI.Service<
'MyService',
{
myFunction: (OtherService) => void
}
> { }
interface OtherService extends DI.Service<
'OtherService',
{
otherFunction: () => void
}
> { }
interface YetAnotherService extends DI.Service<
'YetAnotherService',
{
yetAnotherFunction: DI.WithServices<
'sync', // If the function is 'sync' or 'async'
() => void, // The type of the function
MyService | OtherService // The services needed by the function
>
}
> { }