Effect CommunityEC
Effect Community3y ago
135 replies
addamsson

Combining ReaderTaskEithers with different contexts using pipe and flatMap

In fp-ts i can combine ReaderTaskEithers using a pipe with other ReaderTaskEithers that need different context (the R type parameter). When using flatMap (which is an equivalent of chainW) I "widen" the scope of the resulting ReaderTaskEither so each of my functions returning an RTE will have their independent list of dependencies and I don't have to mention them in the others:
const foo = pipe(
    users.findById(ownerId), // 👈 `users` require a Transaction
    flatMap((user) => {
        if (isOrgRoot(user)) {
            return right(user); // 👈 no deps
        } else {
            return users.update({
                id: user.id,
                roles: addRole(user.roles, Roles.OrgRoot),
            });
        }
    }),
    flatMap(() => organizations.create({ name, ownerId })) // 👈 `organizations` require a transaction and a logger
)

In the end I just have to provide the right context object : foo()(context).

Do I understand correctly that in Effect there is no "widening", and this problem is solved with Layers (as opposed to just using plain service dependencies).
Was this page helpful?