core icon indicating copy to clipboard operation
core copied to clipboard

Add mechanism to automatically gather contexts for libraries

Open zoe-codez opened this issue 1 year ago • 0 comments

Issue

It's not uncommon to want to gather some details about a parent service, normally this is handled by passing around a context variable. The logger, and scheduler have the benefit of being inside core, so they can get special code to provide the context build into the workflow, getting special treatment

Proposal

Some method of altering the base definition of the service needs to happen. This can be picked up on by the wiring process, which will have a dedicated workflow. If it observes the change, then every time a new service is constructed, something special will happen to this service providing it the child context via a callback. The result of which gets passed to the child service as the parent service definition.

Option 1

Add something to internal to serve as a wrapper.

This can be accounted for in the construction of TServiceParams, and can be dealt with in a number of ways by bootstrap.

function Example({ internal }: TServiceParams) {
  return internal.childContext((childContext: TContext) => {
    return {
      ...service definition
    }
  })
}

Option 2

Alter the definition of the module definition

CreateLibrary({
  services: { ... normal services },
  parentServices: { example: Example }
})

Create TServiceParent,

function Example({ logger }: TServiceParent) {
  logger.info("hello world!");
}

These parent services have a parentContext / childContext, instead of context available.

  • scheduler will refer to parent by default
  • logger refers to child by default

Option 3

Some form of wrapping the service definition with a callback

It looks ugly, and it's easy to see this setting some bad precedents. Not a huge fan, but including for completeness

export const Example = SpecialWrapperFunction(function({ internal }: TServiceParams) {
  return {
    ...service definition
  }
})

zoe-codez avatar Mar 18 '24 17:03 zoe-codez