Effect CommunityEC
Effect Community3y ago
9 replies
bigpopakap

Controlling Service Construction with Layer Arguments

Is it reasonable for layers to take arguments to control how they construct a service? For example:
// Represents the version control system we are in
interface VCS {
   isRepo: Effect<never, never, boolean>;
}
const VCS = Context.Tag<VCS>();

// Something that can detect an existing VCS, or initialize one
// Example impl: GitDetector, which can detect if we're in a git repo and/or initialize one
interface VCSDetector {
   detect: Effect<never, VCSDetectorError, Option<VCS>>; // Can return successfully that we're not inside a VCS
   init: Effect<never, VCSDetectorError, VCS>; // Initializes the VCS
}
const VCSDetector = Context.Tag<VCSDetector>();

const vcsLayer = (init: boolean): Layer<VCSDetector, VCSDetectorError, Option<VCS>> =>
   // Calls detect()
   // If we are in a VCS already, returns that
   // Else if init=false, returns None
   // Else calls init()
   // returns the created VCS as a Some<>

Or is this a bad idea for some reason (like layer caching)?
Was this page helpful?