Effect CommunityEC
Effect Community3y ago
223 replies
addamsson

Composing Context Elements for Memoization in Business Operations

I bumped into something today: I have an Effect that represents a business operation. Whenever I want to call it, I'm passing the context (type parameter R) to it, but what if I have a context element that I want to create only once and then memoize while my application runs? In other words: a service that's not just associated to my operation, but composed into it (eg: the lifecycle of this dependency is bound to the lifecycle of the operation). A concrete example:
const finalDeps = {
    ...deps,
    logger: this.logger.getSubLogger({
        name: RegisterOrUpdateUser.name,
    }),
}

Here deps contains all the services (repositories, clients, etc), but the logger is a service that's specific to only one operation (RegisterOrUpdateUser in my case). How can I implement this with Effect?
Was this page helpful?