Effect CommunityEC
Effect Community3y ago
69 replies
sandromaglione

Understanding a Real Use Case example of `Runtime`

Could someone give me a real usecase example of using
Runtime
?

I took a look at vite-remix-effect but it still looks too complex for me to get a sense of what is happening.
https://github.com/mikearnaldi/vite-remix-effect/blob/main/app/lib/effect.ts

I tried to come up with my own example based on the docs:

const layer = EmailService.EmailServiceLive;

const runtimeEffect = Effect.gen(function* (_) {
  const scope = yield* _(
    Scope.make(ExecutionStrategy.parallel).pipe(
      Effect.tap((scope) =>
        Scope.addFinalizer(scope, Console.log("finalizer 1"))
      ),
      Effect.tap((scope) =>
        Scope.addFinalizer(scope, Console.log("finalizer 2"))
      )
    )
  );

  const runtime = yield* _(Layer.toRuntime(layer), Scope.extend(scope));

  yield* _(Scope.close(scope, Exit.unit));
  return runtime;
});

export async function runPromise<E, A>(
  effect: Effect.Effect<Layer.Layer.Success<typeof layer>, E, A>
): Promise<A> {
  const runtime = await Effect.runPromise(runtimeEffect);
  return Runtime.runPromise(runtime)(effect);
}


Nonetheless, it's still unclear to me when something like this would be useful.

Anyone has a good practical example to understand
Runtime
?
Was this page helpful?