Effect CommunityEC
Effect Community2mo ago
9 replies
rnau

Passing Scope as a Parameter: Is It an Antipattern?

Is it antipattern to pass the Scope.Scope as a parameter?

I have service that implements web-socket protocol, and it cleans up connection with a clean up function

export class StreamingClient extends Effect.Service<StreamingClient>()("StreamingClient", {
  effect: Effect.scoped(
    Effect.gen(function* () {
        ...
        yield* Effect.addFinalizer(() => protocol.disconnect());

        return channelId;
      });
})


So I put this together, and tested locally and it works nicely. But when I deployed this in staging, service errored with Service not found: effect/Scope

I thought if I declare scope at the entry point for the app (this is Lambda function), it's just going to use it automaticlaly and be happy but that didn't work - same error

export async function handler(event, context): Promise<void> {
  const baseProgram = Effect.gen(function* () {
    ... logic
  })

  await Effect.runPromise(
    Effect.scoped(baseProgram.pipe(Effect.tapError((error) => Effect.logError("Handler failed", error))))
  );
}


I managed to make it work with passing scope around like this to services that use scoped protocol service

return Effect.runPromise(Effect.provideService(effect, Scope.Scope, dependencies.scope));


The question basically is if I made some internal service scoped, how to setup the scope within the main app (entrypoint)? Ty!
Was this page helpful?