Effect CommunityEC
Effect Community2y ago
27 replies
whatplan

The Purpose of Layer.discard/EffectDiscard/ScopedDiscard in Creating Layers

im curious about the point of Layer.discard/Layer.effectDiscard/Layer.scopedDiscard

why even make a
Layer
if it doesnt produce anything? Why not just make it an Effect?

consider
@effect/platform
's Http.server.serve vs
@effect/cli
's Command.run

http does things in layers:
const ServerLive = Http.server.layer(() => createServer(), { port: 3000 });

const HttpLive = Http.router.empty.pipe(
  Http.router.get(
    "/",
    Effect.map(Http.request.ServerRequest, (req) => Http.response.text(req.url))
  ),
  Http.server.serve(Http.middleware.logger)
);

const MainLive = HttpLive.pipe(
  Layer.provide(ServerLive),
  Layer.provide(NodeContext.layer)
);

runMain(Layer.launch(MainLive));


but I dont see why this couldnt just be an Effect?
const ServerLive = Http.server.layer(() => createServer(), { port: 3000 });

// Effect<Http.server.Server, never, void>
const HttpLive: Effect.Effect<Http.server.Server, never, void> =
  Http.router.empty.pipe(
    Http.router.get(
      "/",
      Effect.map(Http.request.ServerRequest, (req) =>
        Http.response.text(req.url)
      )
    ),
    Http.server.serve(Http.middleware.logger)
  ) as any;

const main = HttpLive.pipe(
  Effect.provide(ServerLive),
  Effect.provide(NodeContext.layer)
);

runMain(main);


with cli:
const textArg = Args.text({ name: "text" });
const run = Command.make(
  "echo",
  {
    textArg,
  },
  ({ textArg }) => Console.log(textArg)
).pipe(
  Command.run({
    name: "echo",
    version: "1.0.0",
  })
);

const main = Effect.suspend(() => run(globalThis.process.argv));
main.pipe(Effect.provide(BunContext.layer), Runtime.runMain);


run could totally be a Layer<CommandLineArgs | CLI.Enviorment, ValidationError, void, but its just a function that returns an effect
Was this page helpful?