Documents injecute 1.x · Migrating from 0.x
Skip to content

Function: startable() ​

startable<K, F, D, P>(key, factory, dependencies, hooks): StartableModule<NeededServices<F, D>, { [Q in string]: Produced<F> } & { [Q in `${string}:${string}`]: LifecycleHook }, { [Q in string]: Awaited<Produced<F>> } & { [Q in `${string}:${string}`]: LifecycleHook }>

Defined in: src/utils/lifecycle.ts:992

Registers a service and its lifecycle hook in one call: a module that extend() applies. The service is registered under key, and a hook under <key>:<stage> ('consumer:start') calls start with the service when the app starts and stop when it stops.

The services it needs are typed by the factory's parameters: annotate them on a function factory. It works with a DIContainer and an AsyncDIContainer; start receives the resolved service and the start signal.

Type Parameters ​

K ​

K extends string

F ​

F extends (new (...args) => unknown) | ((...args) => unknown)

D ​

D extends readonly unknown[]

P ​

P extends string = "start"

Parameters ​

key ​

K

factory ​

F

dependencies ​

D

hooks ​

StartableHooks<Awaited<Produced<F>>, P>

Returns ​

StartableModule<NeededServices<F, D>, { [Q in string]: Produced<F> } & { [Q in `${string}:${string}`]: LifecycleHook }, { [Q in string]: Awaited<Produced<F>> } & { [Q in `${string}:${string}`]: LifecycleHook }>

Example ​

ts
const addPayments = (c: ServiceRegistry<{ bus: Bus }>) =>
  c.extend(
    startable('consumer', PaymentConsumer, ['bus'], {
      start: (consumer) => consumer.start(),
      stop: (consumer) => consumer.stop(),
    }),
  );

Released under the MIT License.