Composition
Build a graph with remote values, select its result, then run it once.
Compose finite calls#
Every finite client method returns a Pipeline.Remote<A, E, R>. A Remote is both:
a symbolic value that can be referenced by later RPCs; and
an
Effect<A, E, R>that sends its graph when run.
Select several results with Pipeline.all#
Use Pipeline.all to return an object, array, or nested structure containing multiple remote values:
const dashboard = yield* Pipeline.all({
user: rpc.findUser({ email }),
recentPosts: rpc.listRecentPosts(),
unreadCount: rpc.countUnreadNotifications()
})Independent nodes run concurrently on the server and share one transport request.
Pass remote results to later calls#
Read a field from a remote value to pass it to a later call. This builds a dependency; it does not fetch the field on the client:
const user = rpc.findUser({ email })
const result = yield* Pipeline.all({
user,
profile: rpc.getProfile({ userId: user.id }),
friendIds: rpc.listFriendIds({ userId: user.id })
})The server waits for findUser, then runs the profile and friend-ID lookups concurrently. No explicit ordering call is needed.
Map remote arrays and Option values#
Pipeline.map executes its template for every element of a remote array, or for the Some value of a remote Effect Option:
const friends = yield* Pipeline.map(
rpc.listFriendIds({ userId }),
(friendId) => ({
id: friendId,
profile: rpc.getProfile({ userId: friendId })
})
)// findUserByHandle returns Option<User>
const greeting = yield* Pipeline.map(
rpc.findUserByHandle({ handle }),
(user) => rpc.makeGreeting({ name: user.name })
)For arrays, the server evaluates items concurrently. For an Option, it runs the mapped calls for Some and skips them for None.
Calls built inside the callback run per item. Build a call outside it when its result should be shared. Nested maps can refer to outer items.
Query after a mutation#
Pipeline.andThen creates a new execution phase. It is useful when a client wants to choose the data returned after a mutation:
const state = yield* rpc.addTodo({ title }).pipe(
Pipeline.andThen((created) => ({
created,
todos: rpc.listTodos({ filter: "all" }),
remaining: rpc.countTodos({ filter: "active" })
}))
)All calls introduced by the callback begin after the mutation. Independent calls within that new phase can run concurrently.
The callback runs on the client while the graph is being built. Its created argument is symbolic. You can project its fields or pass them to another RPC, but JavaScript branching cannot inspect them.
Put a decision that needs server data in an RPC handler.
For simple sequencing where only the second result matters, use Pipeline.zipRight(first, second).
Errors and requirements#
Pipeline composition accumulates the same three type parameters used by Effect:
const account = rpc.findAccount({ email })
const invoices = rpc.listInvoices({ accountId: account.id })
// Effect<ReadonlyArray<Invoice>,
// AccountNotFound | InvoiceError | Unauthorized |
// RpcClientError | PipelineProtocolError | PipelineExecutionError,
// ClientSchemaServices>Domain and middleware errors remain schema-backed tagged errors, so
Effect.catchTagandEffect.catchTagswork normally.Transport failures use Effect's
RpcClientError.Invalid or incompatible request graphs fail with
PipelineProtocolError.Server infrastructure failures use the stable
PipelineExecutionErrorinstead of exposing driver-specific details.Client schema services and required client middleware remain visible in the Effect requirement type.
Services needed only by handlers or server middleware stay on the server.
Errors and requirements accumulate through dependent payloads, Pipeline.all, Pipeline.map, Pipeline.andThen, and capability calls.