πΈοΈ Flow Mesh β
Flows across multiple browser tabs form a mesh network. They share cache, coordinate execution via leader election, and prevent duplicate API calls.
π
Fetching API...
BroadcastChannel
Waiting cache...
Waiting cache...
The Concept β
When a user has 20 tabs open, each tab making identical API calls is wasteful. Flow Mesh coordinates across tabs so that one tab fetches and all others receive the result instantly.
Quick Start β
ts
const { data, execute } = useFlow(fetchDashboard, {
mesh: {
channel: "dashboard-mesh",
strategy: "leader-follower",
shareCache: true,
shareErrors: true,
heartbeat: 5000,
},
});
// Tab 1: flow.execute() β fetches from server
// Tab 2: flow.execute() β gets Tab 1's cached result instantly
// Tab 1 closes β Tab 2 auto-promotes to leaderLeader Election β
The mesh uses a Bully Algorithm via BroadcastChannel:
mermaid
sequenceDiagram
participant Tab1
participant Tab2
participant Tab3
Tab1->>Tab2: election (ID: abc)
Tab1->>Tab3: election (ID: abc)
Tab2->>Tab1: leader (ID: xyz - higher)
Note over Tab2: Becomes Leader
Tab2->>Tab1: heartbeat
Tab2->>Tab3: heartbeat
Note over Tab1,Tab3: Followers receive cache updatesShared Cache β
When the leader tab fetches data successfully, it broadcasts the result to all followers. Followers can use this cached data without making their own API call.
Error Propagation β
Errors are shared across the mesh to prevent "retry storms" β if the server is down, all tabs learn simultaneously instead of each one retrying independently.
API Reference β
FlowMesh β
ts
import { FlowMesh } from "@asyncflowstate/core";
const mesh = new FlowMesh({
channel: "my-mesh",
strategy: "leader-follower",
});
// Check if this tab is the leader
mesh.leader; // true | false
// Share a result across tabs
mesh.shareResult("user-123", { name: "John" });
// Check for cached results
const cached = mesh.getCached("user-123");
// Listen for updates
mesh.onCacheUpdate("user-123", (data) => {
console.log("Received from another tab:", data);
});
// Cleanup
mesh.dispose();Configuration β
| Option | Type | Default | Description |
|---|---|---|---|
channel | string | β | BroadcastChannel name |
strategy | string | 'leader-follower' | Coordination mode |
shareCache | boolean | true | Share results across tabs |
shareErrors | boolean | true | Propagate errors |
heartbeat | number | 5000 | Leader heartbeat interval (ms) |
