Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Effect router

Most Crux apps handle every effect in the shell. The core serializes each request, the shell performs it with the platform's own APIs, and the shell sends the serialized output back. For most effects, that's exactly what you want, and it's how everything else in this book works.

Sometimes an effect is better handled in Rust, next to the core. You may want to use an existing Rust library that isn't written in a sans-I/O way, so it can't be driven from the app's update. The request or its result may be awkward or impossible to serialize, like a pointer-style handle to a rendering surface or a large buffer. Or the work may simply be better written once, in Rust, than separately in each shell.

The effect router handles these cases. An EffectRouter wraps the Core and a routing closure you write. The closure looks at each effect the app emits and sends it to a handler, or "lane". An effect can go to:

  • the shell, over the usual serialized interface,
  • a custom FFI of your own, or
  • Rust code running alongside the core.

The app itself doesn't change. It requests effects as usual, and doesn't know which lane handles them.

In the diagram, the routing closure sends effects to a Rust handler, to the shell and to a custom FFI. Each output goes back to the core, and the core gives the follow-up effects to the routing closure again.

The parts of an effect router. In the Rust shared crate, an EffectRouter holds the Core, which runs the app without changes, and the routing closure. The Core gives each effect and each follow-up effect to the routing closure. The closure sends each effect to one of the routes in your Routes type. A Rust handler runs alongside the core and resolves its requests itself. The Serialized lane sends bytes to the shell's EffectHandler, which is the usual serialized interface. The Parked lane sends the typed operation to a custom FFI of your own design. Each output goes back to the Core, and the Core sends the follow-up effects to the routing closure again.

We'll walk through the counter-routing example: an HTTP counter with a "random" button, which changes the count by a random amount. (The middleware chapter builds the same app with middleware instead.) Here the random number comes from a core-side handler that the router sends Random effects to.

The operation and the app

The random number is an ordinary operation, declared with #[derive(Operation)] as a request that produces a RandomNumber:

// Rust
/// Request a random number between the two values, both included. The answer
/// is a single [`RandomNumber`].
#[derive(Operation, Facet, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[operation(request, output = RandomNumber)]
pub struct RandomNumberRequest(pub isize, pub isize);

#[derive(Facet, Debug, PartialEq, Eq, Deserialize)]
pub struct RandomNumber(pub isize);

The app adds it to its Effect enum as a Random variant:

// Rust
#[effect(facet_typegen)]
#[derive(Debug)]
pub enum Effect {
    Render(RenderOperation),
    Http(HttpRequest),
    ServerSentEvents(SseRequest),
    Random(RandomNumberRequest),
}

And it asks for a random number with Command::request_from_shell, as it would for any effect:

// Rust
            Event::Random => Command::request_from_shell(RandomNumberRequest(-5, 5))
                .map(|out| out.0)
                .then_send(Event::UpdateBy),

Nothing in the app mentions routing. Where Random is handled is decided in the FFI module, which is the user-owned assembly point between the core and the shell.

Routing effects

Here is the whole flow in the counter. The core runs the app and hands every effect it emits to the routing closure. The closure sends Random to the RngHandler and everything else to the shell. Whichever lane answers, the answer goes back to the core, and any follow-up effects come round through the closure again.

The effect router in the counter. In the Rust shared crate, the EffectRouter holds the Core, which runs the app, and the routing closure. The Core gives its effects to the routing closure. The closure sends Random to the RngHandler, which runs on its own thread, and everything else to the Serialized lane. The RngHandler and the Serialized lane are the routes in EffectRoutes, our Routes implementation. The Serialized lane sends bytes through the CruxShell callback to the EffectHandler in the Swift or Kotlin shell. The RngHandler sends a RandomNumber back to the Core, and the shell sends the serialized output back to the Core.

The routes

The handlers an EffectRouter dispatches to are grouped in a type of your own, which implements the Routes trait. The counter has two: the Serialized lane, which talks to the shell, and the RngHandler, which produces random numbers in Rust.

// Rust
#[cfg(not(target_family = "wasm"))]
#[derive(Clone)]
struct EffectRoutes {
    serialized: Arc<Serialized<Counter, Self, BincodeFfiFormat>>,
    rng_handler: Arc<RngHandler>,
}

#[cfg(not(target_family = "wasm"))]
impl Routes<Counter> for EffectRoutes {
    fn new(router: Weak<EffectRouter<Counter, Self>>) -> Self {
        Self {
            serialized: Arc::new(Serialized::new(router.clone())),
            rng_handler: Arc::new(RngHandler::new(router)),
        }
    }
}

Routes::new receives a Weak reference to the router that will own the routes. Each route keeps it so that, when it resolves a request later, it can move the core's effect runtime forward. The reference is weak because the router owns the routes, and a strong reference back would be a cycle. For the same reason the router is always held in an Arc. The type must be Clone, because the routing closure gets a copy of it, so each route is wrapped in an Arc and cloning shares the same handlers.

The routing closure

EffectRouter::new takes the Core and a builder closure. The router first constructs the routes, then passes them to the builder, which returns the routing closure. That way the routing closure can capture the routes it dispatches to. Here is the whole of CoreFfi::new:

// Rust
    pub fn new(shell: Arc<dyn CruxShell>) -> Self {
        #[cfg(not(target_family = "wasm"))]
        {
            let router = EffectRouter::new(Core::new(), move |routes: EffectRoutes| {
                let shell = shell.clone();

                move |effect| match effect {
                    crate::Effect::Random(req) => {
                        routes.rng_handler.process(req);
                    }
                    effect => {
                        let serialized_effect = Effect::from(effect);

                        let bytes = routes
                            .serialized
                            .serialize(serialized_effect)
                            .expect("serialized effect request should encode");

                        shell.process_effects(bytes);
                    }
                }
            });

            Self { router }
        }

        #[cfg(target_family = "wasm")]
        {
            let inner = Core::<Counter>::new().bridge::<BincodeFfiFormat>(move |effect_bytes| {
                match effect_bytes {
                    Ok(effect) => shell.process_effects(effect),
                    Err(e) => panic!("{e}"),
                }
            });

            Self { inner }
        }
    }

On native targets the routing closure has two arms. A Random request goes to the RngHandler. Every other effect is converted to the FFI's own Effect type, serialized by the Serialized lane, and handed to the shell through the CruxShell callback. The fall-through arm is the usual place for the Serialized lane: the router is an opt-in for the few effects that need special handling, and everything else keeps working as it would with a plain bridge.

The routing closure isn't only called for the effects that update returns. When any lane resolves a request, the router collects the follow-up effects the app produces and passes each of them through the same closure. When the RngHandler answers a Random, the command that asked for the number turns the answer into an UpdateBy event for the app (that's what .then_send(Event::UpdateBy) does). The Render and HTTP requests that UpdateBy produces are then routed exactly as they would be if the shell had answered. So one policy applies to the whole chain of effects, however it was started.

On WebAssembly there are no threads for the RngHandler to use, so the wasm branch doesn't use the router at all. It bridges the Core directly, Random reaches the shell like any other effect, and the JavaScript shell answers it.

The lanes

crux_core::effects::routes has three lanes:

  • Serialized keeps the standard bridge behaviour. Each effect is registered under an EffectId and serialized to bytes with the FFI format (here BincodeFfiFormat) by Serialized::serialize. The shell later calls Serialized::resolve with the id and the serialized output. It also provides update and view over bytes, so it covers the whole serialized FFI surface.
  • Parked is for effects the shell handles over a custom FFI that you design, because the payload or the output shouldn't be serialized. The routing closure calls Parked::park with the request, which stores it and returns a ParkedEffectId together with the typed operation. Your FFI passes both to the shell however it likes. When the shell has the output, your FFI calls Parked::resolve with the id and the typed output, and the router routes the follow-up effects as usual. ParkedEffectId::into_raw and ParkedEffectId::from_raw turn the id into a u64 and back, for crossing the FFI. The counter doesn't need this lane. The crate's effect_router_prototype test uses it for a camera effect that returns an opaque image reference.
  • Buffer collects requests with Buffer::push, for the surrounding code to take with Buffer::drain and handle synchronously. It does no FFI or id bookkeeping, which makes it convenient in tests and for simple in-process handlers.

A core-side handler like the RngHandler isn't one of these types. It is any Rust code that holds the requests it is given and resolves them back through the router, as the next section shows.

The FFI effect type

The Serialized lane needs an effect type it can serialize, and only the effects that reach it need to be in it. So the FFI module declares its own Effect enum without the Random variant, and converts from the app's:

// Rust
#[cfg(not(target_family = "wasm"))]
#[effect(facet_typegen)]
pub enum Effect {
    Render(RenderOperation),
    Http(HttpRequest),
    ServerSentEvents(SseRequest),
}

#[cfg(not(target_family = "wasm"))]
#[allow(clippy::fallible_impl_from)] // panic on Random is intentional: it is never routed here
impl From<crate::Effect> for Effect {
    fn from(effect: crate::Effect) -> Self {
        match effect {
            crate::Effect::Render(request) => Self::Render(request),
            crate::Effect::Http(request) => Self::Http(request),
            crate::Effect::ServerSentEvents(request) => Self::ServerSentEvents(request),
            crate::Effect::Random(_) => panic!("Encountered a Random effect"),
        }
    }
}

The From implementation panics on Random, because the routing closure handles that variant before it converts anything. #[effect(facet_typegen)] generates the EffectFFI implementation that Serialized::serialize needs.

Type generation doesn't see this enum. The codegen binary registers the app, so the shells' generated EffectHandler follows the app's Effect and still has a random method. The Swift and Kotlin shells implement it by failing with an error, because on their targets the router answers Random and the method is never called.

A known rough edge

We plan to have type generation follow the effects that actually reach the shell, so a shell is never asked to implement a method for an effect the router handles. Until then, the native shells need the failing random method. See #623.

A core-side handler

The RngHandler generates random numbers on a background thread:

// Rust
use std::{
    sync::{
        Weak,
        mpsc::{Sender, channel},
    },
    thread::spawn,
};

use crux_core::{Request, effects::ResolveSink};
use rand::rngs::SysRng;
use rand::{RngExt, SeedableRng, TryRng as _, rngs::StdRng};

use crate::capabilities::{RandomNumber, RandomNumberRequest};

pub struct RngHandler {
    jobs_tx: Sender<Request<RandomNumberRequest>>,
}

impl RngHandler {
    pub fn new<R>(sink: Weak<R>) -> Self
    where
        R: ResolveSink<RandomNumberRequest> + Send + Sync + 'static,
    {
        let (jobs_tx, jobs_rx) = channel::<Request<RandomNumberRequest>>();

        // Persistent background worker
        spawn(move || {
            let mut sys_rng = SysRng;
            let mut rng =
                StdRng::seed_from_u64(sys_rng.try_next_u64().expect("could not seed RNG"));

            while let Ok(mut request) = jobs_rx.recv() {
                let RandomNumberRequest(from, to) = request.operation;

                // rand samples no `isize`, so draw an `i64` from the same range.
                #[allow(clippy::cast_possible_truncation)]
                let out = rng.random_range(from as i64..=to as i64) as isize;

                if let Some(sink) = sink.upgrade() {
                    sink.resolve_request(&mut request, RandomNumber(out))
                        .expect("resolving a random number should succeed");
                }
            }
        });

        Self { jobs_tx }
    }

    pub fn process(&self, request: Request<RandomNumberRequest>) {
        self.jobs_tx
            .send(request)
            .expect("RngHandler worker disconnected");
    }
}

The routing closure calls RngHandler::process with the Request, which sends it to a persistent worker thread over a channel. The thread owns the random number generator, so its seed lives in one place. When it has a number, it resolves the request through the ResolveSink trait, which the EffectRouter implements. ResolveSink::resolve_request resolves the request and then moves the core's effect runtime forward, routing every follow-up effect through the routing closure. That is how the Render and HTTP requests that follow a random number reach the shell, on the RngHandler's thread.

RngHandler::new takes a Weak reference to anything that implements ResolveSink<RandomNumberRequest>, rather than the router itself. The handler depends only on the one operation it serves, not on the app or the route set, and the Weak it is given in Routes::new is exactly that. If the router has been dropped, the upgrade fails and the handler drops the request.

The process call returns straight away, which matters: the routing closure runs inside update and resolve, so a handler that blocked there would block the shell's call into the core.

The FFI surface

On native targets, every effect the router sends to the shell goes through one callback, the CruxShell trait that the shell implements and passes to CoreFfi::new:

// Rust
/// For the Shell to provide.
///
/// `boltffi` generates the bindings for every target, wasm included, from a
/// build for the host. Thus the FFI surface (this trait and the `CoreFfi`
/// methods below) must present a single, cfg-independent signature. The
/// native and wasm paths therefore differ only inside method bodies, not in
/// their signatures.
#[boltffi::export]
pub trait CruxShell: Send + Sync {
    /// Called when any effects resulting from an asynchronous process
    /// need processing by the shell.
    ///
    /// The bytes are a serialized vector of requests.
    fn process_effects(&self, bytes: Vec<u8>);
}

The bytes are a serialized vector of requests, the same shape that a plain bridge's update and resolve return, so the shell handles them the same way. Each call carries the request for one effect.

This includes the effects produced synchronously inside update and resolve. The router calls the routing closure for each of them before the call returns, and the closure calls process_effects straight away. So on native targets update and resolve always return an empty vector:

// Rust
    #[must_use]
    pub fn update(&self, data: &[u8]) -> Vec<u8> {
        #[cfg(not(target_family = "wasm"))]
        {
            self.router
                .routes
                .serialized
                .update(data)
                .expect("event should deserialize");

            Vec::new()
        }

        #[cfg(target_family = "wasm")]
        {
            let mut effects = Vec::new();
            match self.inner.update(data, &mut effects) {
                Ok(()) => effects,
                Err(e) => panic!("{e}"),
            }
        }
    }

The callback, then, arrives in two ways. Most calls come on the thread that called update or resolve, before that call has returned. The rest come from the RngHandler's thread, after it has answered a Random. The shells have to handle both, which is what the next section is about.

A Parked lane adds to this surface rather than replacing it. You would add a callback to the shell trait for the parked effect, carrying its id and operation, and a method on CoreFfi that the shell calls to resolve it. Both are yours to design, with whatever types your binding generator can carry.

In the shell

The counter-routing shells use the generated Core (Who drives the loop), with an EffectHandler that delegates http to the shell's own HTTP client and implements serverSentEvents by hand. CoreFfi::new takes the shell's callback, but the generated FfiBridge constructs CoreFfi with no arguments, so the codegen binary doesn't configure .boltffi(...). Instead each shell writes the three-method CoreBridge over its own CoreFfi, and constructs the generated Core with it. See the generated Core for CoreBridge and Core(bridge:handler:) in each language.

Swift

The Swift bridge takes the callback and hands it to CoreFfi. Its methods are bytes in, bytes out:

// Swift: apple/CounterApp/RoutingBridge.swift
/// The generated `CoreBridge`, written by hand over this app's `CoreFfi`.
///
/// The codegen binary doesn't ask for the generated `FfiBridge`, because that
/// constructs `CoreFfi` with no arguments, and this app's `CoreFfi` takes the
/// `CruxShell` callback the `EffectRouter` delivers effects through. So this
/// bridge takes that callback and hands it to `CoreFfi`; the three methods are
/// bytes in, bytes out.
///
/// On native targets `update` and `resolve` always return no requests: the
/// router sends every effect it doesn't handle itself to the callback
/// instead.
///
/// `@unchecked Sendable` because `CoreFfi` is a class Swift cannot prove
/// `Sendable`; its state is a handle into a mutex-guarded bridge on the Rust
/// side, so calling it from any task is safe.
struct RoutingBridge: CoreBridge, @unchecked Sendable {
    private let ffi: CoreFfi

    init(shell: CruxShell) {
        ffi = CoreFfi(shell: shell)
    }

    func update(_ event: [UInt8]) -> [UInt8] {
        [UInt8](ffi.update(data: Data(event)))
    }

    func resolve(_ id: UInt32, _ output: [UInt8]) -> [UInt8] {
        [UInt8](ffi.resolve(effectId: id, data: Data(output)))
    }

    func view() -> [UInt8] {
        [UInt8](ffi.view())
    }
}

The callback has to hand its bytes to the Core's process(bytes:), but construction runs the other way: the Core is built from the bridge, which is built from the callback. The callback breaks the cycle by not holding the Core at all. It puts each batch on an AsyncStream, in the order the batches arrive:

// Swift: apple/CounterApp/RoutingBridge.swift
/// The `CruxShell` callback `CoreFfi` is constructed with.
///
/// The `EffectRouter` calls `processEffects` with the serialized requests for
/// every effect it routes to the shell. Most arrive synchronously, on the
/// thread that called `update` or `resolve` and before that call returns; the
/// rest come from the `RngHandler`'s thread, after it has answered a `Random`.
/// The callback doesn't hold the `Core` (it can't, because the `Core` is
/// built from the bridge, which is built from this), so it puts the bytes on
/// a stream, in the order they arrive, for `makeCore` to forward.
final class RoutedEffects: CruxShell, Sendable {
    let stream: AsyncStream<[UInt8]>
    private let continuation: AsyncStream<[UInt8]>.Continuation

    init() {
        (stream, continuation) = AsyncStream.makeStream(of: [UInt8].self)
    }

    func processEffects(bytes: Data) {
        continuation.yield([UInt8](bytes))
    }
}

Once the Core exists, one task on the main actor reads the stream and passes each batch on. The Swift Core is @MainActor, so the batches have to be processed there, whichever thread they arrived on. Because one task reads the whole stream, the batches reach the Core in the order the router sent them. And because the task runs later, a batch sent from inside update is never processed while update is still running:

// Swift: apple/CounterApp/RoutingBridge.swift
/// Build the generated `Core` over the routing bridge.
///
/// Construction runs one way (callback, then bridge, then `Core`), and the
/// stream carries the callback's bytes back the other way. One task on the
/// main actor reads it and passes each batch to `Core.process(bytes:)`, which
/// has to run there because `Core` is `@MainActor`. A single reader keeps the
/// batches in the order the router sent them, and because it runs later, a
/// batch sent from inside `update` is never processed while `update` is
/// still running.
@MainActor
func makeCore() -> Core {
    let effects = RoutedEffects()
    let core = Core(bridge: RoutingBridge(shell: effects), handler: CounterHandler())

    let stream = effects.stream
    Task { [weak core] in
        for await bytes in stream {
            core?.process(bytes: bytes)
        }
    }

    return core
}

The app builds its Core with makeCore() in place of Core(handler:), and the rest of the shell is the same as any other.

Kotlin

The Kotlin shell has the same RoutingBridge over CoreFfi, and a RoutedEffects callback whose core property is set once the Core has been constructed. Nothing calls the callback before then, because the router only has effects once an event has been sent.

The callback posts each batch to the main thread with Dispatchers.Main, not Dispatchers.Main.immediate. The Kotlin Core isn't thread-safe, so the batch has to run on the main thread. But most batches arrive on the main thread, inside the CoreFfi call that produced them, and Main.immediate would run them inline, calling back into CoreFfi before that call has returned. Dispatchers.Main always posts, and the main looper runs posts in order, so the batches reach the Core in the order the router sent them.

Web

In WebAssembly CoreFfi bridges the Core directly, without the router, so Random reaches the shell. The TypeScript CounterHandler implements random with Math.random, and its RoutingBridge passes a callback that forwards bytes to the Core's processBytes, through a variable that is assigned once the Core has been built. In WebAssembly that callback is never called, because update and resolve return every effect themselves. The Leptos shell is written in Rust, uses the Core directly without the FFI, and answers Random itself.

Testing

The router lives in the FFI module, so it isn't involved in testing the app. You test update directly, treating Random as a normal effect and resolving it yourself:

// Rust
    #[test]
    fn random_change() {
        let app = Counter;
        let mut model = Model::default();

        let mut cmd = app.update(Event::Random, &mut model);

        // the app should request a random number from the web API
        let mut request = cmd.effects().next().unwrap().expect_random();

        assert_eq!(request.operation, RandomNumberRequest(-5, 5));
        request.resolve(RandomNumber(-2)).unwrap();

        // And start an UpdateBy the number

        let event = cmd.events().next().unwrap();
        assert_eq!(event, Event::UpdateBy(-2));

The app's logic stays pure and testable, and how its effects are handled is a separate concern, composed at the FFI boundary.

Summary

To route some effects to handlers of your own:

  1. Define the operation with #[derive(Operation)], and request it from the app as usual.
  2. Group the routes in a type that implements Routes, with a Serialized lane for the shell and a lane or handler for each effect that needs special handling.
  3. Write the routing closure in EffectRouter::new, sending each effect to its lane, with Serialized as the fall-through arm.
  4. Deliver the shell's effects through the callback, and have each shell write a CoreBridge that constructs CoreFfi with it.

For the full API reference, see the effects module docs. The Effect Router RFC explains the motivation and the design.