Effect CommunityEC
Effect Community4w ago
4 replies
Baptiste

Providing Dependencies with Dynamic Values in Effect Typescript

It's said that best practice is to Effect.provide once, at the top level of the program. Though I have a service that depends on a variable inside the program. Is it fine to provide that one specifically inside the program? Or is there a better way for that kind of pattern?

export const ProgressReporterRedisLayer = (exportId: string) =>
  Layer.unwrapEffect(
    Effect.gen(function* () {
      const redis = yield* RedisClient;

      return Layer.succeed(ProgressReporter, {
        report: (progress) =>
          redis
            .publish(
              `${EXPORT_PROGRESS_CHANNEL_PREFIX}${exportId}`,
              progress.toString(),
            )
            .pipe(
              Effect.mapError(
                (error) =>
                  new ProgressReporterError({
                    message: `[${error.cause}] ${error.message}`,
                  }),
              ),
            ),
      });
    }),
  );
Was this page helpful?