-
Notifications
You must be signed in to change notification settings - Fork 261
feat: SyncAgent #557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: SyncAgent #557
Conversation
This document outlines a comprehensive design for implementing bi-directional sync between Durable Objects (Agents) and React clients using dedicated hooks: - useDurableQuery for subscribing to database queries - useDurableMutation for executing mutations Key features: - Real-time synchronization across all connected clients - Hibernation-aware with automatic re-subscription - Query/mutation registry pattern on the server - Type-safe client hooks - SQL-based subscription persistence The design leverages existing Agent infrastructure (SQL, WebSocket, broadcasting) while adding a structured query/mutation layer similar to React Query/tRPC.
- Cursor-based pagination (recommended) with useDurableInfiniteQuery hook - Offset-based pagination for traditional page navigation - Complete implementation examples for both patterns - Real-time update strategies for paginated data - Subscription persistence details for multi-page scenarios - Marked pagination question as resolved in discussion section
- Create comprehensive TanStack Query integration guide - Update main design document to reference TanStack Query foundation - Simplify hook implementations by leveraging TanStack Query - Add benefits: caching, deduplication, optimistic updates, DevTools - Include setup instructions and QueryClientProvider configuration - Document migration path and advanced patterns Key improvements: - Built on battle-tested React Query infrastructure - Automatic request deduplication and caching - Rich DevTools for debugging - Optimistic updates with rollback support - Framework-agnostic core for future extensions
- Support both local SQLite (10GB limit) and D1 (shared database) - Use Drizzle ORM for type-safe database operations - Automatic userId filtering for shared databases (D1) - No filtering needed for local SQLite (already isolated per DO) - userId filter uses this.name from Durable Object - Complete schema definition and configuration examples - Storage limits table and migration recommendations - Dependencies: drizzle-orm, drizzle-kit Key features: - Flexible storage backend selection via environment variable - Type-safe queries with Drizzle's inferSelect/inferInsert - Automatic multi-tenancy support for D1 - Same query API regardless of backend
- Implements a classic TodoMVC-style todo list application - Demonstrates real-time state synchronization using Cloudflare Agents sync layer - Features: - Add, edit, complete, and delete todos - Filter by all, active, or completed todos - Clear all completed todos - Toggle all todos at once - Real-time state sync across multiple browser windows - Uses useAgent hook for seamless client-server state management - All state changes via setState() automatically broadcast to connected clients - Includes comprehensive README with usage instructions
- Add 15 tests covering all TodoAgent functionality - Test state management, todo operations, filters, and persistence - Add getState() method to TodoAgent for test access - Configure Vitest with proper Wrangler test setup - All tests passing successfully
- Add client.test.tsx with 5 comprehensive React test cases - Test state initialization and rendering - Test state updates via onStateUpdate callback - Test agent.call() for add, toggle, delete, and filter operations - Test client-side filtering logic - Add vitest.react.config.ts for browser-based testing with Playwright - All tests passing successfully
…mple Add Todo List example demonstrating sync layer
- Add new MessageType enums for query/mutation operations - Create QuerySubscriptionManager for subscription tracking - Add registerQuery and registerMutation methods to Agent class - Implement query execution and broadcasting with versioning - Add message handlers for subscribe/unsubscribe/mutation - Create React hooks: useDurableQuery, useDurableMutation, useDurableInfiniteQuery - Update todo-list example to use new query/mutation sync system - Add SQL tables for subscriptions, query updates, and mutations
…tion-sync Research: Bi-directional Sync with useDurableQuery and useDurableMutation Hooks
|
commit: |
SyncAgent
is a replica ofAgent
(without the MCP client) that has bidirectional sync capabilities while using theuseAgent
hook on the frontend to receive data and execute mutations.Example in
examples/todo-list
demonstrates isolation for user data usingthis.sql
packages/sync
or something