Making Layers Behave Like Modules for Global Dependencies

[SOLVED]

Hello ๐Ÿ‘‹
How to make Layers behave like NestJS modules, where you can add another module in the imports array and set it as a Global dependency?
I have PostLayer, AuthLayer, AppLayer, DbLayer. Each of these layers inject their own API routes (Effect.gen), or services, or child layers using Layer.mergeAll. The AppService which starts the Hono server, and provides it as App tag, only works if I add it to the main pipeline in the main Effect.runFork. So apparently Layers don't work like modules or groups , or maybe they do but they don't inject services globally...

This works:
const App = Server.pipe(
  Layer.provide(AppLayer),
  Layer.provide(DbLayer),
  Layer.provide(ApiLayer),
  Layer.provide(AppService), <~ Works
)

Effect.runFork(
  Layer.launch(App)
)


This worksn't:
//src/app/app.layer.ts
export const AppLayer = Layer.scopedDiscard(IndexRouteGen).pipe(
  Layer.provide(AppService)
)

//src/main.ts
const App = Server.pipe(
  Layer.provide(AppLayer),
  Layer.provide(DbLayer),
  Layer.provide(ApiLayer),
)

Effect.runFork(
  Layer.launch(App)
)


In other words, if I let AppLayer provide AppService, it doesn't work, but if I add AppService to the main pipeline after adding all the layers, it works.
Was this page helpful?