@abloatai/ablo 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +208 -0
- package/LICENSE +201 -0
- package/NOTICE +12 -0
- package/README.md +230 -0
- package/dist/BaseSyncedStore.d.ts +709 -0
- package/dist/BaseSyncedStore.js +1843 -0
- package/dist/Database.d.ts +344 -0
- package/dist/Database.js +1259 -0
- package/dist/LazyReferenceCollection.d.ts +181 -0
- package/dist/LazyReferenceCollection.js +460 -0
- package/dist/Model.d.ts +339 -0
- package/dist/Model.js +715 -0
- package/dist/ModelRegistry.d.ts +200 -0
- package/dist/ModelRegistry.js +535 -0
- package/dist/NetworkMonitor.d.ts +27 -0
- package/dist/NetworkMonitor.js +73 -0
- package/dist/ObjectPool.d.ts +202 -0
- package/dist/ObjectPool.js +1106 -0
- package/dist/SyncClient.d.ts +489 -0
- package/dist/SyncClient.js +1555 -0
- package/dist/SyncEngineContext.d.ts +46 -0
- package/dist/SyncEngineContext.js +74 -0
- package/dist/adapters/alwaysOnline.d.ts +16 -0
- package/dist/adapters/alwaysOnline.js +19 -0
- package/dist/adapters/inMemoryStorage.d.ts +30 -0
- package/dist/adapters/inMemoryStorage.js +94 -0
- package/dist/agent/Agent.d.ts +358 -0
- package/dist/agent/Agent.js +500 -0
- package/dist/agent/index.d.ts +115 -0
- package/dist/agent/index.js +128 -0
- package/dist/agent/session.d.ts +90 -0
- package/dist/agent/session.js +156 -0
- package/dist/agent/types.d.ts +73 -0
- package/dist/agent/types.js +10 -0
- package/dist/ai-sdk/coordination-context.d.ts +51 -0
- package/dist/ai-sdk/coordination-context.js +107 -0
- package/dist/ai-sdk/index.d.ts +68 -0
- package/dist/ai-sdk/index.js +68 -0
- package/dist/ai-sdk/intent-broadcast.d.ts +77 -0
- package/dist/ai-sdk/intent-broadcast.js +72 -0
- package/dist/ai-sdk/wrap.d.ts +67 -0
- package/dist/ai-sdk/wrap.js +45 -0
- package/dist/api/index.d.ts +10 -0
- package/dist/api/index.js +9 -0
- package/dist/auth/index.d.ts +137 -0
- package/dist/auth/index.js +246 -0
- package/dist/client/Ablo.d.ts +835 -0
- package/dist/client/Ablo.js +1440 -0
- package/dist/client/ApiClient.d.ts +200 -0
- package/dist/client/ApiClient.js +659 -0
- package/dist/client/auth.d.ts +79 -0
- package/dist/client/auth.js +81 -0
- package/dist/client/createInternalComponents.d.ts +44 -0
- package/dist/client/createInternalComponents.js +88 -0
- package/dist/client/createModelProxy.d.ts +152 -0
- package/dist/client/createModelProxy.js +199 -0
- package/dist/client/identity.d.ts +63 -0
- package/dist/client/identity.js +156 -0
- package/dist/client/index.d.ts +36 -0
- package/dist/client/index.js +33 -0
- package/dist/client/persistence.d.ts +7 -0
- package/dist/client/persistence.js +11 -0
- package/dist/client/validateAbloOptions.d.ts +42 -0
- package/dist/client/validateAbloOptions.js +43 -0
- package/dist/config/index.d.ts +10 -0
- package/dist/config/index.js +12 -0
- package/dist/context.d.ts +27 -0
- package/dist/context.js +58 -0
- package/dist/core/DatabaseManager.d.ts +108 -0
- package/dist/core/DatabaseManager.js +361 -0
- package/dist/core/QueryProcessor.d.ts +77 -0
- package/dist/core/QueryProcessor.js +262 -0
- package/dist/core/QueryView.d.ts +64 -0
- package/dist/core/QueryView.js +219 -0
- package/dist/core/StoreManager.d.ts +131 -0
- package/dist/core/StoreManager.js +334 -0
- package/dist/core/ViewRegistry.d.ts +20 -0
- package/dist/core/ViewRegistry.js +55 -0
- package/dist/core/index.d.ts +34 -0
- package/dist/core/index.js +59 -0
- package/dist/core/openIDBWithTimeout.d.ts +27 -0
- package/dist/core/openIDBWithTimeout.js +63 -0
- package/dist/core/query-utils.d.ts +37 -0
- package/dist/core/query-utils.js +60 -0
- package/dist/errors.d.ts +235 -0
- package/dist/errors.js +243 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +82 -0
- package/dist/interfaces/headless.d.ts +95 -0
- package/dist/interfaces/headless.js +41 -0
- package/dist/interfaces/index.d.ts +321 -0
- package/dist/interfaces/index.js +8 -0
- package/dist/mutators/RecordingTransaction.d.ts +36 -0
- package/dist/mutators/RecordingTransaction.js +216 -0
- package/dist/mutators/Transaction.d.ts +48 -0
- package/dist/mutators/Transaction.js +64 -0
- package/dist/mutators/UndoManager.d.ts +114 -0
- package/dist/mutators/UndoManager.js +143 -0
- package/dist/mutators/defineMutators.d.ts +55 -0
- package/dist/mutators/defineMutators.js +28 -0
- package/dist/policy/index.d.ts +19 -0
- package/dist/policy/index.js +18 -0
- package/dist/policy/types.d.ts +74 -0
- package/dist/policy/types.js +17 -0
- package/dist/principal.d.ts +44 -0
- package/dist/principal.js +49 -0
- package/dist/query/client.d.ts +43 -0
- package/dist/query/client.js +84 -0
- package/dist/query/index.d.ts +6 -0
- package/dist/query/index.js +5 -0
- package/dist/query/types.d.ts +143 -0
- package/dist/query/types.js +36 -0
- package/dist/react/AbloProvider.d.ts +205 -0
- package/dist/react/AbloProvider.js +398 -0
- package/dist/react/ClientSideSuspense.d.ts +36 -0
- package/dist/react/ClientSideSuspense.js +17 -0
- package/dist/react/DefaultFallback.d.ts +24 -0
- package/dist/react/DefaultFallback.js +43 -0
- package/dist/react/SyncGroupProvider.d.ts +19 -0
- package/dist/react/SyncGroupProvider.js +44 -0
- package/dist/react/context.d.ts +161 -0
- package/dist/react/context.js +35 -0
- package/dist/react/index.d.ts +64 -0
- package/dist/react/index.js +73 -0
- package/dist/react/internalContext.d.ts +35 -0
- package/dist/react/internalContext.js +3 -0
- package/dist/react/useAblo.d.ts +72 -0
- package/dist/react/useAblo.js +63 -0
- package/dist/react/useCurrentUserId.d.ts +21 -0
- package/dist/react/useCurrentUserId.js +33 -0
- package/dist/react/useErrorListener.d.ts +20 -0
- package/dist/react/useErrorListener.js +39 -0
- package/dist/react/useIntent.d.ts +29 -0
- package/dist/react/useIntent.js +42 -0
- package/dist/react/useMutate.d.ts +83 -0
- package/dist/react/useMutate.js +122 -0
- package/dist/react/useMutationFailureListener.d.ts +26 -0
- package/dist/react/useMutationFailureListener.js +38 -0
- package/dist/react/useMutators.d.ts +56 -0
- package/dist/react/useMutators.js +66 -0
- package/dist/react/usePresence.d.ts +32 -0
- package/dist/react/usePresence.js +41 -0
- package/dist/react/useQuery.d.ts +123 -0
- package/dist/react/useQuery.js +145 -0
- package/dist/react/useReactive.d.ts +35 -0
- package/dist/react/useReactive.js +111 -0
- package/dist/react/useReader.d.ts +69 -0
- package/dist/react/useReader.js +73 -0
- package/dist/react/useSyncStatus.d.ts +61 -0
- package/dist/react/useSyncStatus.js +76 -0
- package/dist/react/useUndoScope.d.ts +36 -0
- package/dist/react/useUndoScope.js +73 -0
- package/dist/realtime/index.d.ts +10 -0
- package/dist/realtime/index.js +9 -0
- package/dist/schema/field.d.ts +134 -0
- package/dist/schema/field.js +264 -0
- package/dist/schema/index.d.ts +29 -0
- package/dist/schema/index.js +38 -0
- package/dist/schema/model.d.ts +326 -0
- package/dist/schema/model.js +89 -0
- package/dist/schema/queries.d.ts +203 -0
- package/dist/schema/queries.js +145 -0
- package/dist/schema/relation.d.ts +172 -0
- package/dist/schema/relation.js +104 -0
- package/dist/schema/schema.d.ts +259 -0
- package/dist/schema/schema.js +188 -0
- package/dist/schema/sugar.d.ts +129 -0
- package/dist/schema/sugar.js +94 -0
- package/dist/source/index.d.ts +423 -0
- package/dist/source/index.js +320 -0
- package/dist/source/pushQueue.d.ts +112 -0
- package/dist/source/pushQueue.js +249 -0
- package/dist/stores/ObjectStore.d.ts +103 -0
- package/dist/stores/ObjectStore.js +371 -0
- package/dist/stores/ObjectStoreContract.d.ts +39 -0
- package/dist/stores/ObjectStoreContract.js +1 -0
- package/dist/stores/SyncActionStore.d.ts +101 -0
- package/dist/stores/SyncActionStore.js +481 -0
- package/dist/sync/BootstrapHelper.d.ts +127 -0
- package/dist/sync/BootstrapHelper.js +434 -0
- package/dist/sync/ConnectionManager.d.ts +136 -0
- package/dist/sync/ConnectionManager.js +465 -0
- package/dist/sync/HydrationCoordinator.d.ts +137 -0
- package/dist/sync/HydrationCoordinator.js +468 -0
- package/dist/sync/NetworkProbe.d.ts +43 -0
- package/dist/sync/NetworkProbe.js +113 -0
- package/dist/sync/OfflineFlush.d.ts +9 -0
- package/dist/sync/OfflineFlush.js +22 -0
- package/dist/sync/OfflineTransactionStore.d.ts +37 -0
- package/dist/sync/OfflineTransactionStore.js +263 -0
- package/dist/sync/SyncWebSocket.d.ts +663 -0
- package/dist/sync/SyncWebSocket.js +1336 -0
- package/dist/sync/createIntentStream.d.ts +33 -0
- package/dist/sync/createIntentStream.js +243 -0
- package/dist/sync/createPresenceStream.d.ts +46 -0
- package/dist/sync/createPresenceStream.js +192 -0
- package/dist/sync/createSnapshot.d.ts +33 -0
- package/dist/sync/createSnapshot.js +124 -0
- package/dist/sync/participants.d.ts +114 -0
- package/dist/sync/participants.js +336 -0
- package/dist/sync/schemas.d.ts +79 -0
- package/dist/sync/schemas.js +78 -0
- package/dist/testing/fixtures/bootstrap.d.ts +45 -0
- package/dist/testing/fixtures/bootstrap.js +53 -0
- package/dist/testing/fixtures/deltas.d.ts +86 -0
- package/dist/testing/fixtures/deltas.js +139 -0
- package/dist/testing/fixtures/models.d.ts +82 -0
- package/dist/testing/fixtures/models.js +270 -0
- package/dist/testing/helpers/react-wrapper.d.ts +66 -0
- package/dist/testing/helpers/react-wrapper.js +64 -0
- package/dist/testing/helpers/sync-engine-harness.d.ts +55 -0
- package/dist/testing/helpers/sync-engine-harness.js +70 -0
- package/dist/testing/helpers/wait.d.ts +25 -0
- package/dist/testing/helpers/wait.js +44 -0
- package/dist/testing/index.d.ts +21 -0
- package/dist/testing/index.js +32 -0
- package/dist/testing/mocks/MockMutationExecutor.d.ts +65 -0
- package/dist/testing/mocks/MockMutationExecutor.js +139 -0
- package/dist/testing/mocks/MockNetworkMonitor.d.ts +20 -0
- package/dist/testing/mocks/MockNetworkMonitor.js +46 -0
- package/dist/testing/mocks/MockSyncContext.d.ts +64 -0
- package/dist/testing/mocks/MockSyncContext.js +100 -0
- package/dist/testing/mocks/MockSyncStore.d.ts +88 -0
- package/dist/testing/mocks/MockSyncStore.js +171 -0
- package/dist/testing/mocks/MockWebSocket.d.ts +66 -0
- package/dist/testing/mocks/MockWebSocket.js +117 -0
- package/dist/transactions/OptimisticEchoTracker.d.ts +82 -0
- package/dist/transactions/OptimisticEchoTracker.js +104 -0
- package/dist/transactions/TransactionQueue.d.ts +499 -0
- package/dist/transactions/TransactionQueue.js +1895 -0
- package/dist/transactions/index.d.ts +16 -0
- package/dist/transactions/index.js +7 -0
- package/dist/transactions/mutation-error-handler.d.ts +5 -0
- package/dist/transactions/mutation-error-handler.js +39 -0
- package/dist/types/global.d.ts +107 -0
- package/dist/types/global.js +38 -0
- package/dist/types/index.d.ts +241 -0
- package/dist/types/index.js +70 -0
- package/dist/types/streams.d.ts +495 -0
- package/dist/types/streams.js +11 -0
- package/dist/utils/asyncIterator.d.ts +41 -0
- package/dist/utils/asyncIterator.js +142 -0
- package/dist/utils/duration.d.ts +28 -0
- package/dist/utils/duration.js +47 -0
- package/dist/utils/mobx-setup.d.ts +42 -0
- package/dist/utils/mobx-setup.js +381 -0
- package/docs/api-keys.md +24 -0
- package/docs/api.md +230 -0
- package/docs/audit.md +81 -0
- package/docs/capabilities.md +163 -0
- package/docs/client-behavior.md +202 -0
- package/docs/data-sources.md +214 -0
- package/docs/examples/agent-human.md +84 -0
- package/docs/examples/ai-sdk-tool.md +92 -0
- package/docs/examples/existing-python-backend.md +249 -0
- package/docs/examples/nextjs.md +88 -0
- package/docs/examples/server-agent.md +86 -0
- package/docs/guarantees.md +148 -0
- package/docs/index.md +97 -0
- package/docs/integration-guide.md +493 -0
- package/docs/interaction-model.md +140 -0
- package/docs/mcp/claude-code.md +43 -0
- package/docs/mcp/cursor.md +53 -0
- package/docs/mcp/windsurf.md +46 -0
- package/docs/mcp.md +59 -0
- package/docs/quickstart.md +152 -0
- package/docs/react.md +115 -0
- package/docs/roadmap.md +45 -0
- package/examples/README.md +54 -0
- package/examples/data-source/README.md +102 -0
- package/examples/data-source/ablo-driver.ts +89 -0
- package/examples/data-source/customer-server.ts +208 -0
- package/examples/data-source/run.ts +101 -0
- package/examples/data-source/schema.ts +25 -0
- package/examples/quickstart.ts +54 -0
- package/examples/tsconfig.json +16 -0
- package/llms.txt +143 -0
- package/package.json +147 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import type { Schema, SchemaRecord } from '../schema/schema.js';
|
|
3
|
+
import { Ablo } from '../client/Ablo.js';
|
|
4
|
+
import type { AbloPersistence } from '../client/persistence.js';
|
|
5
|
+
import type { SyncEngineConfig, MutationExecutor, MutationDispatcher, SessionErrorDetector, OnlineStatusProvider, SyncLogger, SyncObservabilityProvider } from '../config/index.js';
|
|
6
|
+
import type { UseMutatorsOptions } from './useMutators.js';
|
|
7
|
+
import type { MutatorDefs } from '../mutators/defineMutators.js';
|
|
8
|
+
import type { ActiveIntent, Peer } from '../types/streams.js';
|
|
9
|
+
import type { EngineParticipant, ParticipantScope, ParticipantStatus } from '../sync/participants.js';
|
|
10
|
+
import { type SyncStoreContract } from './context.js';
|
|
11
|
+
/**
|
|
12
|
+
* Ablo umbrella provider — owns the sync engine, multiplayer, and
|
|
13
|
+
* the full lifecycle (Strict-Mode-safe singleton, `beforeunload`,
|
|
14
|
+
* session-expiry handling, post-bootstrap hooks).
|
|
15
|
+
*
|
|
16
|
+
* Design goals (borrowed from Liveblocks' `LiveblocksProvider` and
|
|
17
|
+
* Zero's `ZeroProvider`):
|
|
18
|
+
*
|
|
19
|
+
* - **One component, one import.** Consumers write the provider
|
|
20
|
+
* once at the root; nothing else needs to plumb the engine.
|
|
21
|
+
* - **Multiplayer is default.** React consumers are always browsers doing
|
|
22
|
+
* multiplayer UI, so `useParticipant()` / `useAblo()` are always
|
|
23
|
+
* available. No opt-in prop.
|
|
24
|
+
* - **Declarative props for app glue.** `preventUnsavedChanges`,
|
|
25
|
+
* `onSessionExpired`, `postBootstrap`, `resolveUsers` — each
|
|
26
|
+
* absorbs a class of integration code that previously lived in
|
|
27
|
+
* userland.
|
|
28
|
+
* - **Singleton safety.** The engine lives in a ref and rotates
|
|
29
|
+
* only when `userId` / account scope / `url` change. React
|
|
30
|
+
* Strict Mode double-mount does not leak a second WebSocket.
|
|
31
|
+
*/
|
|
32
|
+
export interface AbloProviderProps<R extends SchemaRecord = SchemaRecord> {
|
|
33
|
+
/** Schema from `defineSchema()`. Determines the typed hook surface. */
|
|
34
|
+
schema: Schema<R>;
|
|
35
|
+
/**
|
|
36
|
+
* WebSocket URL of the sync server (`wss://...` or `ws://...`).
|
|
37
|
+
* Hosted apps omit this.
|
|
38
|
+
*/
|
|
39
|
+
url?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Optional app user id for app-owned fields. Ablo resolves sync
|
|
42
|
+
* participant identity from auth; this is not required to connect.
|
|
43
|
+
*/
|
|
44
|
+
userId?: string;
|
|
45
|
+
/** Team IDs the user belongs to. Expanded into sync groups. */
|
|
46
|
+
teamIds?: string[];
|
|
47
|
+
/**
|
|
48
|
+
* API key for engine bootstrap auth. Used by the bootstrap fetch
|
|
49
|
+
* path; falls back to `credentials: 'include'` (session cookie)
|
|
50
|
+
* when unset. Browser apps typically omit this and rely on
|
|
51
|
+
* same-origin session cookies.
|
|
52
|
+
*/
|
|
53
|
+
apiKey?: string;
|
|
54
|
+
/** Optional Zero-style custom mutators. */
|
|
55
|
+
mutators?: MutatorDefs<Schema<R>>;
|
|
56
|
+
/** Options forwarded to the internal `useMutators` call (e.g., `undoScope`). */
|
|
57
|
+
mutatorOptions?: UseMutatorsOptions<Schema<R>>;
|
|
58
|
+
/**
|
|
59
|
+
* Block browser tab close when there are unsynced local writes.
|
|
60
|
+
* Triggers the standard `beforeunload` "Leave site?" prompt.
|
|
61
|
+
* Browsers ignore custom messages — do not pass one. Consumers
|
|
62
|
+
* who want telemetry should read
|
|
63
|
+
* `useSyncStatus().hasUnsyncedChanges` directly.
|
|
64
|
+
*/
|
|
65
|
+
preventUnsavedChanges?: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Milliseconds to tolerate connection loss before `useSyncStatus()`
|
|
68
|
+
* flips to `disconnected`. Defaults to 5000. Set to 0 to
|
|
69
|
+
* disable the grace period (immediate transition).
|
|
70
|
+
*
|
|
71
|
+
* v0.3.0 scope: reserved for future wiring. Current transition is
|
|
72
|
+
* driven by the engine's built-in state machine.
|
|
73
|
+
*/
|
|
74
|
+
lostConnectionTimeout?: number;
|
|
75
|
+
/**
|
|
76
|
+
* Fired when the server rejects the session. The provider has
|
|
77
|
+
* ALREADY called `engine.purge()` (disposed + wiped IndexedDB) by
|
|
78
|
+
* the time this runs — the callback is for app-level side effects
|
|
79
|
+
* (e.g., redirect to sign-in, clear analytics identity).
|
|
80
|
+
*/
|
|
81
|
+
onSessionExpired?: () => void | Promise<void>;
|
|
82
|
+
/**
|
|
83
|
+
* Fired on any error the provider surfaces: engine errors,
|
|
84
|
+
* WebSocket errors, uncaught `postBootstrap` exceptions. Use for
|
|
85
|
+
* Sentry / Datadog. Consumers who only want errors inside React
|
|
86
|
+
* can use the `useErrorListener()` hook instead.
|
|
87
|
+
*/
|
|
88
|
+
onError?: (error: Error) => void;
|
|
89
|
+
observability?: SyncObservabilityProvider;
|
|
90
|
+
logger?: SyncLogger;
|
|
91
|
+
mutationExecutor?: MutationExecutor;
|
|
92
|
+
mutationDispatcher?: MutationDispatcher;
|
|
93
|
+
sessionErrorDetector?: SessionErrorDetector;
|
|
94
|
+
onlineStatus?: OnlineStatusProvider;
|
|
95
|
+
configOverrides?: SyncEngineConfig;
|
|
96
|
+
syncGroups?: string[];
|
|
97
|
+
bootstrapBaseUrl?: string;
|
|
98
|
+
maxPoolSize?: number;
|
|
99
|
+
/**
|
|
100
|
+
* Local persistence mode for the underlying `Ablo` client. Defaults
|
|
101
|
+
* to `volatile` — pass `'indexeddb'` to opt back into offline-queue +
|
|
102
|
+
* reload-surviving cache in a browser. See `AbloOptions.persistence`
|
|
103
|
+
* for the full semantics.
|
|
104
|
+
*/
|
|
105
|
+
persistence?: AbloPersistence;
|
|
106
|
+
/**
|
|
107
|
+
* Rendered in place of `children` during the *first* bootstrap pass —
|
|
108
|
+
* while the engine is actively transitioning from `initial` →
|
|
109
|
+
* `connected` and has never successfully connected before. Once the
|
|
110
|
+
* engine reaches `connected` the gate latches open for the lifetime
|
|
111
|
+
* of this provider instance; transient `reconnecting` / `needs-auth`
|
|
112
|
+
* states do NOT re-show the fallback (the app's own UI handles those
|
|
113
|
+
* by then).
|
|
114
|
+
*
|
|
115
|
+
* Defaults to `<DefaultFallback />` — a neutral theme-adaptive
|
|
116
|
+
* spinner that uses `currentColor`, ships with zero design-system
|
|
117
|
+
* dependencies, and self-centers in a full-parent container. Pass
|
|
118
|
+
* your own `<Skeleton />` for a branded loading UX. Pass `null` to
|
|
119
|
+
* render nothing during bootstrap. Pass the string literal
|
|
120
|
+
* `"passthrough"` to opt out of the gate entirely — children render
|
|
121
|
+
* immediately and consumers are responsible for their own gating
|
|
122
|
+
* (`<ClientSideSuspense>` or manual `useSyncStatus()` checks).
|
|
123
|
+
* Useful for pages that mount debug helpers, error boundaries, or
|
|
124
|
+
* analytics that must run pre-ready.
|
|
125
|
+
*/
|
|
126
|
+
fallback?: ReactNode | 'passthrough';
|
|
127
|
+
children: ReactNode;
|
|
128
|
+
}
|
|
129
|
+
export declare function AbloProvider<R extends SchemaRecord = SchemaRecord>(props: AbloProviderProps<R>): React.ReactElement;
|
|
130
|
+
export type { EngineParticipant, ParticipantScope, ParticipantStatus };
|
|
131
|
+
/**
|
|
132
|
+
* Options for `useParticipant`. The hook reuses the engine's single
|
|
133
|
+
* WebSocket and opens a scoped claim on it when `scope` is provided:
|
|
134
|
+
* one TCP connection, N logical sub-syncgroup participants.
|
|
135
|
+
*/
|
|
136
|
+
export interface UseParticipantOptions {
|
|
137
|
+
readonly scope?: ParticipantScope;
|
|
138
|
+
readonly label?: string;
|
|
139
|
+
readonly as?: unknown;
|
|
140
|
+
readonly ttlSeconds?: number | string | null;
|
|
141
|
+
readonly agent?: unknown;
|
|
142
|
+
readonly idempotencyKey?: string | null;
|
|
143
|
+
readonly autoRefreshThresholdSeconds?: number | null;
|
|
144
|
+
/** Tear down + don't re-join while true. */
|
|
145
|
+
readonly paused?: boolean;
|
|
146
|
+
}
|
|
147
|
+
/** @deprecated Use `ParticipantStatus`. */
|
|
148
|
+
export type MeshParticipantStatus = ParticipantStatus;
|
|
149
|
+
export interface UseParticipantReturn {
|
|
150
|
+
readonly participant: EngineParticipant | null;
|
|
151
|
+
/** Everyone else on the engine's sync groups (`participant.presence.others`), bridged to React. */
|
|
152
|
+
readonly peers: ReadonlyArray<Peer>;
|
|
153
|
+
/** Active intent claims by peers (`participant.intents.others`), bridged to React. */
|
|
154
|
+
readonly claims: ReadonlyArray<ActiveIntent>;
|
|
155
|
+
readonly status: ParticipantStatus;
|
|
156
|
+
readonly error: Error | null;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Join multiplayer for a given scope. Returns the participant and its
|
|
160
|
+
* lifecycle status. Auto-cleans up on unmount or when `paused`
|
|
161
|
+
* flips to true.
|
|
162
|
+
*
|
|
163
|
+
* The returned `participant` is an `EngineParticipant` — `.presence`
|
|
164
|
+
* + `.intents` only — backed by the engine's existing socket. For
|
|
165
|
+
* headless-bot patterns (a separate identity in the same browser
|
|
166
|
+
* tab), construct a second `Ablo({ kind: 'agent', ... })` directly.
|
|
167
|
+
*/
|
|
168
|
+
export declare function useParticipant(opts: UseParticipantOptions): UseParticipantReturn;
|
|
169
|
+
/**
|
|
170
|
+
* Returns the raw `SyncEngine` proxy. Typically you want the typed
|
|
171
|
+
* hooks (`useQuery`, `useOne`, `useMutate`) — this is for rare cases
|
|
172
|
+
* where you need direct access (e.g., `sync.tasks.subscribe(cb)`).
|
|
173
|
+
*
|
|
174
|
+
* The generic parameter narrows the return type to your schema's
|
|
175
|
+
* model record so call sites get typed `sync.tasks.findMany()` /
|
|
176
|
+
* `sync.slides.create(...)` without a cast at the call site:
|
|
177
|
+
*
|
|
178
|
+
* ```ts
|
|
179
|
+
* const sync = useSync<(typeof schema)['models']>();
|
|
180
|
+
* ```
|
|
181
|
+
*
|
|
182
|
+
* The runtime value is the exact engine the provider constructed;
|
|
183
|
+
* the generic just widens the compile-time type.
|
|
184
|
+
*/
|
|
185
|
+
export declare function useSync<R extends SchemaRecord = SchemaRecord>(): Ablo<R>;
|
|
186
|
+
/**
|
|
187
|
+
* Returns the underlying `SyncStoreContract` (the BaseSyncedStore).
|
|
188
|
+
* Most consumers should prefer the typed hooks (`useQuery` etc.); this
|
|
189
|
+
* is for advanced cases like direct ObjectPool access or custom
|
|
190
|
+
* reactive bridges. Throws if the provider hasn't mounted the store
|
|
191
|
+
* yet — wrap consumers in `<ClientSideSuspense>` to gate correctly.
|
|
192
|
+
*
|
|
193
|
+
* The generic parameter lets consumers widen the return type to a
|
|
194
|
+
* concrete `BaseSyncedStore<...>` subclass if they track one:
|
|
195
|
+
*
|
|
196
|
+
* ```ts
|
|
197
|
+
* type AppStore = BaseSyncedStore<AppEvents, typeof schema>;
|
|
198
|
+
* const store = useSyncStore<AppStore>(); // no cast needed at call site
|
|
199
|
+
* ```
|
|
200
|
+
*
|
|
201
|
+
* The runtime value is always the concrete store the SDK constructed,
|
|
202
|
+
* so widening the type is safe. The bounded generic (`T extends
|
|
203
|
+
* SyncStoreContract`) keeps the widening honest.
|
|
204
|
+
*/
|
|
205
|
+
export declare function useSyncStore<T extends SyncStoreContract = SyncStoreContract>(): T;
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
import { useContext, useEffect, useMemo, useRef, useState, } from 'react';
|
|
4
|
+
import { Ablo } from '../client/Ablo.js';
|
|
5
|
+
import { createParticipantClaimId, parseParticipantTtlSeconds, resolveParticipantSyncGroups, } from '../sync/participants.js';
|
|
6
|
+
import { SyncContext } from './context.js';
|
|
7
|
+
import { AbloInternalContext } from './internalContext.js';
|
|
8
|
+
import { AbloValidationError } from '../errors.js';
|
|
9
|
+
import { useSyncStatus } from './useSyncStatus.js';
|
|
10
|
+
import { DefaultFallback } from './DefaultFallback.js';
|
|
11
|
+
// ── Implementation ───────────────────────────────────────────────────
|
|
12
|
+
/**
|
|
13
|
+
* Lightweight event emitter for provider-level errors. Lives on the
|
|
14
|
+
* provider instance (ref-based) so `useErrorListener` subscriptions
|
|
15
|
+
* survive re-renders without thrashing.
|
|
16
|
+
*/
|
|
17
|
+
function createErrorEmitter() {
|
|
18
|
+
const listeners = new Set();
|
|
19
|
+
return {
|
|
20
|
+
subscribe(fn) {
|
|
21
|
+
listeners.add(fn);
|
|
22
|
+
return () => { listeners.delete(fn); };
|
|
23
|
+
},
|
|
24
|
+
emit(err) {
|
|
25
|
+
for (const fn of listeners) {
|
|
26
|
+
try {
|
|
27
|
+
fn(err);
|
|
28
|
+
}
|
|
29
|
+
catch { }
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function AbloProvider(props) {
|
|
35
|
+
const { schema, url = 'wss://mesh.ablo.finance', userId, teamIds, apiKey, preventUnsavedChanges, onSessionExpired, onError, observability, logger, mutationExecutor, mutationDispatcher, sessionErrorDetector, onlineStatus, configOverrides, syncGroups, bootstrapBaseUrl, maxPoolSize, persistence, fallback = _jsx(DefaultFallback, {}), children, } = props;
|
|
36
|
+
// Account scope is no longer accepted from props. The engine learns
|
|
37
|
+
// it from auth (capability token) at bootstrap and we read it back
|
|
38
|
+
// out of `_store.orgId` once `engine.ready()` resolves.
|
|
39
|
+
const [resolvedAccountScope, setResolvedAccountScope] = useState(null);
|
|
40
|
+
// ── Error emitter (provider-instance scoped) ─────────────────────
|
|
41
|
+
//
|
|
42
|
+
// Built once, reused for the lifetime of this provider. Survives
|
|
43
|
+
// engine rotations so error listeners don't need to resubscribe.
|
|
44
|
+
const errorEmitterRef = useRef(null);
|
|
45
|
+
if (!errorEmitterRef.current) {
|
|
46
|
+
errorEmitterRef.current = createErrorEmitter();
|
|
47
|
+
}
|
|
48
|
+
const errorEmitter = errorEmitterRef.current;
|
|
49
|
+
// Stash `onError` in a ref so forwarding it doesn't trigger
|
|
50
|
+
// engine rotation. The provider wraps it and calls via ref at fire
|
|
51
|
+
// time, matching the `useEventCallback` idiom.
|
|
52
|
+
const onErrorRef = useRef(onError);
|
|
53
|
+
onErrorRef.current = onError;
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
return errorEmitter.subscribe((err) => onErrorRef.current?.(err));
|
|
56
|
+
}, [errorEmitter]);
|
|
57
|
+
// ── Engine lifecycle keyed on (userId, url) ─────────────────────
|
|
58
|
+
//
|
|
59
|
+
// The engine rotates when either of these change. For everything
|
|
60
|
+
// else (mutators, DI, callbacks) we stash in refs so mutations to
|
|
61
|
+
// those props don't tear down the engine.
|
|
62
|
+
const engineKey = JSON.stringify({
|
|
63
|
+
userId: userId ?? null,
|
|
64
|
+
url,
|
|
65
|
+
});
|
|
66
|
+
const [engineState, setEngineState] = useState({ key: engineKey, engine: null });
|
|
67
|
+
// Keep a ref to the current engine key so the rotation effect can
|
|
68
|
+
// detect late-arriving prop changes without causing React churn.
|
|
69
|
+
const currentKeyRef = useRef(engineState.key);
|
|
70
|
+
currentKeyRef.current = engineState.key;
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
const abort = new AbortController();
|
|
73
|
+
let isStale = false;
|
|
74
|
+
setResolvedAccountScope(null);
|
|
75
|
+
// Construct engine + multiplayer streams for this key.
|
|
76
|
+
const engineOptions = {
|
|
77
|
+
baseURL: url,
|
|
78
|
+
schema,
|
|
79
|
+
...(userId ? { user: { id: userId, teamIds } } : {}),
|
|
80
|
+
apiKey,
|
|
81
|
+
logger,
|
|
82
|
+
observability,
|
|
83
|
+
sessionErrorDetector,
|
|
84
|
+
onlineStatus,
|
|
85
|
+
mutationExecutor,
|
|
86
|
+
mutationDispatcher,
|
|
87
|
+
configOverrides,
|
|
88
|
+
syncGroups,
|
|
89
|
+
bootstrapBaseUrl,
|
|
90
|
+
maxPoolSize,
|
|
91
|
+
persistence,
|
|
92
|
+
autoStart: false,
|
|
93
|
+
};
|
|
94
|
+
const engine = Ablo(engineOptions);
|
|
95
|
+
setEngineState({ key: engineKey, engine });
|
|
96
|
+
// Forward session-error events to the consumer. Purge first so
|
|
97
|
+
// the IndexedDB is wiped before the app redirects to /signin.
|
|
98
|
+
const unsubscribeSession = engine.onSessionError(async (err) => {
|
|
99
|
+
errorEmitter.emit(err);
|
|
100
|
+
try {
|
|
101
|
+
await engine.purge();
|
|
102
|
+
}
|
|
103
|
+
catch { }
|
|
104
|
+
try {
|
|
105
|
+
await onSessionExpired?.();
|
|
106
|
+
}
|
|
107
|
+
catch (hookErr) {
|
|
108
|
+
errorEmitter.emit(hookErr);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
// Drive initial bootstrap. Consumer code that wants to run logic
|
|
112
|
+
// after the engine is ready calls `useAblo()` and wires its own
|
|
113
|
+
// `useEffect` — the SDK no longer holds a registry of "post-
|
|
114
|
+
// bootstrap hooks" because the indirection costs more than it
|
|
115
|
+
// saves once `useAblo` exists.
|
|
116
|
+
(async () => {
|
|
117
|
+
try {
|
|
118
|
+
await engine.ready();
|
|
119
|
+
if (isStale || abort.signal.aborted)
|
|
120
|
+
return;
|
|
121
|
+
setResolvedAccountScope(engine._store.orgId ?? null);
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
if (isStale || abort.signal.aborted)
|
|
125
|
+
return;
|
|
126
|
+
errorEmitter.emit(err);
|
|
127
|
+
}
|
|
128
|
+
})();
|
|
129
|
+
return () => {
|
|
130
|
+
isStale = true;
|
|
131
|
+
abort.abort();
|
|
132
|
+
unsubscribeSession();
|
|
133
|
+
void engine.dispose();
|
|
134
|
+
// AbloClient is stateless-ish — participants manage their own
|
|
135
|
+
// WebSocket connections via `participant.disconnect()`. No client
|
|
136
|
+
// close is needed.
|
|
137
|
+
};
|
|
138
|
+
// We intentionally only re-run on engineKey. All other DI is
|
|
139
|
+
// captured at first render; rotating the engine on every
|
|
140
|
+
// `mutationExecutor` identity change would destroy the WebSocket.
|
|
141
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
142
|
+
}, [engineKey]);
|
|
143
|
+
// ── beforeunload + preventUnsavedChanges ─────────────────────────
|
|
144
|
+
useEffect(() => {
|
|
145
|
+
if (typeof window === 'undefined')
|
|
146
|
+
return;
|
|
147
|
+
const handler = (event) => {
|
|
148
|
+
const engine = engineState.engine;
|
|
149
|
+
if (!engine)
|
|
150
|
+
return;
|
|
151
|
+
// Best-effort: dispose on unload. The async work may not
|
|
152
|
+
// complete before the tab closes — that's fine for IDB, which
|
|
153
|
+
// flushes pending writes transactionally.
|
|
154
|
+
void engine.dispose();
|
|
155
|
+
if (preventUnsavedChanges && engine._store.hasUnsyncedChanges) {
|
|
156
|
+
event.preventDefault();
|
|
157
|
+
event.returnValue = '';
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
window.addEventListener('beforeunload', handler);
|
|
161
|
+
return () => window.removeEventListener('beforeunload', handler);
|
|
162
|
+
}, [engineState.engine, preventUnsavedChanges]);
|
|
163
|
+
// ── SyncContext value (for useQuery/useOne/useMutate hooks) ──────
|
|
164
|
+
const syncValue = useMemo(() => {
|
|
165
|
+
if (!engineState.engine)
|
|
166
|
+
return null;
|
|
167
|
+
const currentAccountScope = resolvedAccountScope ??
|
|
168
|
+
engineState.engine._store.orgId;
|
|
169
|
+
if (!currentAccountScope)
|
|
170
|
+
return null;
|
|
171
|
+
return {
|
|
172
|
+
store: engineState.engine._store,
|
|
173
|
+
organizationId: currentAccountScope,
|
|
174
|
+
schema,
|
|
175
|
+
};
|
|
176
|
+
}, [engineState.engine, resolvedAccountScope, schema]);
|
|
177
|
+
// ── Internal context (currentUserId + error subscription) ────────
|
|
178
|
+
const internalValue = useMemo(() => ({
|
|
179
|
+
currentUserId: userId ?? null,
|
|
180
|
+
subscribeError: errorEmitter.subscribe,
|
|
181
|
+
emitError: errorEmitter.emit,
|
|
182
|
+
// `engine` is null until bootstrap finishes; `useSync()` throws
|
|
183
|
+
// on null so callers are forced to gate with <ClientSideSuspense>.
|
|
184
|
+
engine: engineState.engine,
|
|
185
|
+
}), [userId, errorEmitter, engineState.engine]);
|
|
186
|
+
// ── Render ───────────────────────────────────────────────────────
|
|
187
|
+
//
|
|
188
|
+
// Two-phase gate (see `BootstrapGate` below for the latch logic):
|
|
189
|
+
//
|
|
190
|
+
// 1. Engine is null on first render (constructed in the effect
|
|
191
|
+
// above, not in render). We render `fallback` directly — there
|
|
192
|
+
// is no SyncContext to read status from, and by definition the
|
|
193
|
+
// engine hasn't started bootstrapping.
|
|
194
|
+
// 2. Engine exists. Mount SyncContext. `BootstrapGate` then reads
|
|
195
|
+
// `useSyncStatus()` and shows `fallback` only during the very
|
|
196
|
+
// first `connecting` transition; children render on every
|
|
197
|
+
// subsequent state change, including reconnects and auth
|
|
198
|
+
// failures (the app's own UI handles those).
|
|
199
|
+
//
|
|
200
|
+
// `fallback === 'passthrough'` short-circuits both branches — children
|
|
201
|
+
// render immediately without any gate, restoring pre-gate behavior
|
|
202
|
+
// for consumers who need debug helpers / error boundaries / analytics
|
|
203
|
+
// to mount before the engine is ready.
|
|
204
|
+
const passthrough = fallback === 'passthrough';
|
|
205
|
+
const initialFallback = passthrough ? children : fallback;
|
|
206
|
+
if (!syncValue) {
|
|
207
|
+
return (_jsx(AbloInternalContext.Provider, { value: internalValue, children: initialFallback }));
|
|
208
|
+
}
|
|
209
|
+
return (_jsx(AbloInternalContext.Provider, { value: internalValue, children: _jsx(SyncContext.Provider, { value: syncValue, children: passthrough ? (children) : (_jsx(BootstrapGate, { fallback: fallback, children: children }, engineState.key)) }) }));
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Internal gate that renders `fallback` only during the very first
|
|
213
|
+
* bootstrap pass. Latches open on the first `connected` / `reconnecting`
|
|
214
|
+
* / `disconnected` transition and stays open — subsequent transient
|
|
215
|
+
* `connecting` states (hard reconnect after an offline stretch) do NOT
|
|
216
|
+
* re-show the fallback, because by then the app has already rendered
|
|
217
|
+
* once and its own reconnect UI should take over.
|
|
218
|
+
*
|
|
219
|
+
* Re-keyed on `engineState.key` in the parent so engine rotations
|
|
220
|
+
* (userId/org/url change) reset the latch — a new engine genuinely IS
|
|
221
|
+
* a new "first bootstrap" cycle.
|
|
222
|
+
*/
|
|
223
|
+
function BootstrapGate({ fallback, children, }) {
|
|
224
|
+
const status = useSyncStatus();
|
|
225
|
+
const [everConnected, setEverConnected] = useState(false);
|
|
226
|
+
useEffect(() => {
|
|
227
|
+
if (status.name === 'connected' ||
|
|
228
|
+
status.name === 'reconnecting' ||
|
|
229
|
+
status.name === 'disconnected') {
|
|
230
|
+
setEverConnected(true);
|
|
231
|
+
}
|
|
232
|
+
}, [status.name]);
|
|
233
|
+
const showFallback = !everConnected && status.name === 'connecting';
|
|
234
|
+
return _jsx(_Fragment, { children: showFallback ? fallback : children });
|
|
235
|
+
}
|
|
236
|
+
const EMPTY_PRESENCE = Object.freeze([]);
|
|
237
|
+
const EMPTY_INTENTS = Object.freeze([]);
|
|
238
|
+
/**
|
|
239
|
+
* Join multiplayer for a given scope. Returns the participant and its
|
|
240
|
+
* lifecycle status. Auto-cleans up on unmount or when `paused`
|
|
241
|
+
* flips to true.
|
|
242
|
+
*
|
|
243
|
+
* The returned `participant` is an `EngineParticipant` — `.presence`
|
|
244
|
+
* + `.intents` only — backed by the engine's existing socket. For
|
|
245
|
+
* headless-bot patterns (a separate identity in the same browser
|
|
246
|
+
* tab), construct a second `Ablo({ kind: 'agent', ... })` directly.
|
|
247
|
+
*/
|
|
248
|
+
export function useParticipant(opts) {
|
|
249
|
+
const ctx = useContext(AbloInternalContext);
|
|
250
|
+
const engine = ctx?.engine ?? null;
|
|
251
|
+
const { paused = false } = opts;
|
|
252
|
+
const scopeKey = JSON.stringify(resolveParticipantSyncGroups(opts.scope).sort());
|
|
253
|
+
const scopedSyncGroups = useMemo(() => JSON.parse(scopeKey), [scopeKey]);
|
|
254
|
+
const [claimError, setClaimError] = useState(null);
|
|
255
|
+
const [claimConnected, setClaimConnected] = useState(false);
|
|
256
|
+
// Reference-stable participant facade — same socket as entity sync,
|
|
257
|
+
// so there is no `connect()` / `disconnect()` lifecycle here. The
|
|
258
|
+
// engine manages the connection; the hook is a thin window onto its
|
|
259
|
+
// already-attached presence + intent streams.
|
|
260
|
+
const participant = useMemo(() => {
|
|
261
|
+
if (!engine)
|
|
262
|
+
return null;
|
|
263
|
+
return { presence: engine.presence, intents: engine.intents };
|
|
264
|
+
}, [engine]);
|
|
265
|
+
// Status maps to the engine's sync state. `connecting` while the
|
|
266
|
+
// engine bootstraps; `connected` once `engine.ready()` resolves and
|
|
267
|
+
// any scoped participant claim has acked; `error` if the claim
|
|
268
|
+
// fails; `disconnected` while paused or before the engine exists.
|
|
269
|
+
const syncStatus = useSyncStatus();
|
|
270
|
+
const needsClaim = scopedSyncGroups.length > 0;
|
|
271
|
+
const status = paused || !engine
|
|
272
|
+
? 'disconnected'
|
|
273
|
+
: claimError
|
|
274
|
+
? 'error'
|
|
275
|
+
: syncStatus.name === 'connected'
|
|
276
|
+
? needsClaim && !claimConnected
|
|
277
|
+
? 'connecting'
|
|
278
|
+
: 'connected'
|
|
279
|
+
: syncStatus.name === 'disconnected' || syncStatus.name === 'needs-auth'
|
|
280
|
+
? 'disconnected'
|
|
281
|
+
: 'connecting';
|
|
282
|
+
const error = claimError;
|
|
283
|
+
useEffect(() => {
|
|
284
|
+
setClaimError(null);
|
|
285
|
+
setClaimConnected(false);
|
|
286
|
+
if (paused || !engine || scopedSyncGroups.length === 0)
|
|
287
|
+
return;
|
|
288
|
+
if (syncStatus.name !== 'connected')
|
|
289
|
+
return;
|
|
290
|
+
const ws = engine._ws;
|
|
291
|
+
if (!ws)
|
|
292
|
+
return;
|
|
293
|
+
let cancelled = false;
|
|
294
|
+
const claimId = createParticipantClaimId();
|
|
295
|
+
ws.sendClaim(claimId, scopedSyncGroups, {
|
|
296
|
+
ttlSeconds: parseParticipantTtlSeconds(opts.ttlSeconds),
|
|
297
|
+
})
|
|
298
|
+
.then(() => {
|
|
299
|
+
if (!cancelled)
|
|
300
|
+
setClaimConnected(true);
|
|
301
|
+
})
|
|
302
|
+
.catch((err) => {
|
|
303
|
+
if (!cancelled) {
|
|
304
|
+
setClaimError(err instanceof Error ? err : new Error(String(err)));
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
return () => {
|
|
308
|
+
cancelled = true;
|
|
309
|
+
ws.sendRelease(claimId);
|
|
310
|
+
};
|
|
311
|
+
}, [engine, paused, scopeKey, syncStatus.name, opts.ttlSeconds]);
|
|
312
|
+
// Bridge the engine's presence + intents streams into React state.
|
|
313
|
+
// Plain useState + useEffect is sufficient — mid-frame tearing on a
|
|
314
|
+
// peer list is harmless (users won't notice one frame of stale
|
|
315
|
+
// presence). Queries and sync status use useSyncExternalStore
|
|
316
|
+
// because transactions CAN tear visibly; presence can't.
|
|
317
|
+
const [peers, setPeers] = useState(EMPTY_PRESENCE);
|
|
318
|
+
const [claims, setClaims] = useState(EMPTY_INTENTS);
|
|
319
|
+
useEffect(() => {
|
|
320
|
+
if (!participant || paused) {
|
|
321
|
+
setPeers(EMPTY_PRESENCE);
|
|
322
|
+
setClaims(EMPTY_INTENTS);
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
setPeers(participant.presence.others);
|
|
326
|
+
setClaims(participant.intents.others);
|
|
327
|
+
const unsubPresence = participant.presence.subscribe(() => {
|
|
328
|
+
setPeers(participant.presence.others);
|
|
329
|
+
});
|
|
330
|
+
const unsubIntents = participant.intents.subscribe(() => {
|
|
331
|
+
setClaims(participant.intents.others);
|
|
332
|
+
});
|
|
333
|
+
return () => {
|
|
334
|
+
unsubPresence();
|
|
335
|
+
unsubIntents();
|
|
336
|
+
};
|
|
337
|
+
}, [participant, paused]);
|
|
338
|
+
// `opts.as`, `opts.agent`, `opts.idempotencyKey`, and
|
|
339
|
+
// `opts.autoRefreshThresholdSeconds` remain migration placeholders
|
|
340
|
+
// for future capability-mint/attenuation wiring. `scope` is already
|
|
341
|
+
// active: it opens a multiplexed claim on the engine WebSocket.
|
|
342
|
+
return { participant, peers, claims, status, error };
|
|
343
|
+
}
|
|
344
|
+
// ── Escape-hatches: raw engine/store access ──────────────────────────
|
|
345
|
+
/**
|
|
346
|
+
* Returns the raw `SyncEngine` proxy. Typically you want the typed
|
|
347
|
+
* hooks (`useQuery`, `useOne`, `useMutate`) — this is for rare cases
|
|
348
|
+
* where you need direct access (e.g., `sync.tasks.subscribe(cb)`).
|
|
349
|
+
*
|
|
350
|
+
* The generic parameter narrows the return type to your schema's
|
|
351
|
+
* model record so call sites get typed `sync.tasks.findMany()` /
|
|
352
|
+
* `sync.slides.create(...)` without a cast at the call site:
|
|
353
|
+
*
|
|
354
|
+
* ```ts
|
|
355
|
+
* const sync = useSync<(typeof schema)['models']>();
|
|
356
|
+
* ```
|
|
357
|
+
*
|
|
358
|
+
* The runtime value is the exact engine the provider constructed;
|
|
359
|
+
* the generic just widens the compile-time type.
|
|
360
|
+
*/
|
|
361
|
+
export function useSync() {
|
|
362
|
+
const ctx = useContext(AbloInternalContext);
|
|
363
|
+
if (!ctx) {
|
|
364
|
+
throw new AbloValidationError('useSync: no <AbloProvider> mounted above this component.', { code: 'no_ablo_provider' });
|
|
365
|
+
}
|
|
366
|
+
if (!ctx.engine) {
|
|
367
|
+
throw new AbloValidationError('useSync: the sync engine has not yet initialized. Wrap your ' +
|
|
368
|
+
'consumer in <ClientSideSuspense> or guard on useSyncStatus().', { code: 'sync_not_ready' });
|
|
369
|
+
}
|
|
370
|
+
return ctx.engine;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Returns the underlying `SyncStoreContract` (the BaseSyncedStore).
|
|
374
|
+
* Most consumers should prefer the typed hooks (`useQuery` etc.); this
|
|
375
|
+
* is for advanced cases like direct ObjectPool access or custom
|
|
376
|
+
* reactive bridges. Throws if the provider hasn't mounted the store
|
|
377
|
+
* yet — wrap consumers in `<ClientSideSuspense>` to gate correctly.
|
|
378
|
+
*
|
|
379
|
+
* The generic parameter lets consumers widen the return type to a
|
|
380
|
+
* concrete `BaseSyncedStore<...>` subclass if they track one:
|
|
381
|
+
*
|
|
382
|
+
* ```ts
|
|
383
|
+
* type AppStore = BaseSyncedStore<AppEvents, typeof schema>;
|
|
384
|
+
* const store = useSyncStore<AppStore>(); // no cast needed at call site
|
|
385
|
+
* ```
|
|
386
|
+
*
|
|
387
|
+
* The runtime value is always the concrete store the SDK constructed,
|
|
388
|
+
* so widening the type is safe. The bounded generic (`T extends
|
|
389
|
+
* SyncStoreContract`) keeps the widening honest.
|
|
390
|
+
*/
|
|
391
|
+
export function useSyncStore() {
|
|
392
|
+
const sync = useContext(SyncContext);
|
|
393
|
+
if (!sync || !sync.store) {
|
|
394
|
+
throw new AbloValidationError('useSyncStore: the sync engine has not yet initialized. Wrap ' +
|
|
395
|
+
'consumers in <ClientSideSuspense> or guard on useSyncStatus().', { code: 'sync_not_ready' });
|
|
396
|
+
}
|
|
397
|
+
return sync.store;
|
|
398
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Nested bootstrap gate for a subtree. `<AbloProvider>` already ships
|
|
4
|
+
* its own built-in gate (via its `fallback` prop) that handles the
|
|
5
|
+
* common "wait for first bootstrap" case. Use `ClientSideSuspense`
|
|
6
|
+
* only when you need a SEPARATE gate inside an already-ready provider —
|
|
7
|
+
* for example, rendering app chrome immediately while gating a single
|
|
8
|
+
* heavy product surface on its own query resolving.
|
|
9
|
+
*
|
|
10
|
+
* Like the provider-level gate, this component latches open on the
|
|
11
|
+
* first `connected` / `reconnecting` / `disconnected` transition and
|
|
12
|
+
* stays open. Subsequent transient `connecting` states (hard reconnect
|
|
13
|
+
* after offline) do NOT re-show the fallback — the app has already
|
|
14
|
+
* rendered once and its own reconnect UI should take over.
|
|
15
|
+
*
|
|
16
|
+
* v0.3.x implementation is non-Suspense: reads `useSyncStatus()` and
|
|
17
|
+
* conditionally renders. v0.3.x+ will ship a
|
|
18
|
+
* `@ablo/sync-engine/react/suspense` subpath where `useQuery` / `useOne`
|
|
19
|
+
* actually throw Promises; this component becomes a thin wrapper around
|
|
20
|
+
* React's real `<Suspense>` at that point.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* <AbloProvider fallback={<AppSkeleton />}>
|
|
24
|
+
* <AppChrome />
|
|
25
|
+
* <ClientSideSuspense fallback={<CanvasSkeleton />}>
|
|
26
|
+
* <HeavyCanvas />
|
|
27
|
+
* </ClientSideSuspense>
|
|
28
|
+
* </AbloProvider>
|
|
29
|
+
*/
|
|
30
|
+
export interface ClientSideSuspenseProps {
|
|
31
|
+
/** What to render while the nested subtree is waiting for first bootstrap. */
|
|
32
|
+
fallback: ReactNode;
|
|
33
|
+
/** What to render once the subtree is cleared to render. */
|
|
34
|
+
children: ReactNode;
|
|
35
|
+
}
|
|
36
|
+
export declare function ClientSideSuspense({ fallback, children }: ClientSideSuspenseProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useState } from 'react';
|
|
4
|
+
import { useSyncStatus } from './useSyncStatus.js';
|
|
5
|
+
export function ClientSideSuspense({ fallback, children }) {
|
|
6
|
+
const status = useSyncStatus();
|
|
7
|
+
const [everConnected, setEverConnected] = useState(false);
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
if (status.name === 'connected' ||
|
|
10
|
+
status.name === 'reconnecting' ||
|
|
11
|
+
status.name === 'disconnected') {
|
|
12
|
+
setEverConnected(true);
|
|
13
|
+
}
|
|
14
|
+
}, [status.name]);
|
|
15
|
+
const showFallback = !everConnected && status.name === 'connecting';
|
|
16
|
+
return _jsx(_Fragment, { children: showFallback ? fallback : children });
|
|
17
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Neutral loading placeholder — the default value of `<AbloProvider>`'s
|
|
3
|
+
* `fallback` prop. Rendered during the first bootstrap pass when the
|
|
4
|
+
* consumer hasn't supplied their own skeleton.
|
|
5
|
+
*
|
|
6
|
+
* Design goals:
|
|
7
|
+
* - Zero design-system dependency. Inline styles only; no CSS file,
|
|
8
|
+
* no UI-lib imports, no Tailwind assumptions.
|
|
9
|
+
* - Theme-adaptive. Uses `currentColor` for the ring so the spinner
|
|
10
|
+
* inherits the text color from whichever ancestor defines it —
|
|
11
|
+
* works in light + dark contexts without a prop.
|
|
12
|
+
* - Self-centering. Flex-centered in a full-parent container so the
|
|
13
|
+
* common case (provider at the layout root) renders a spinner in
|
|
14
|
+
* the middle of the viewport. Consumers who need different
|
|
15
|
+
* positioning compose their own fallback and pass it explicitly.
|
|
16
|
+
* - Minimal bundle footprint. The whole component + keyframe is ~50
|
|
17
|
+
* bytes gzipped.
|
|
18
|
+
*
|
|
19
|
+
* Consumers wanting a branded loader should pass `fallback={<YourSkeleton />}`
|
|
20
|
+
* on `<AbloProvider>`. Consumers wanting NO visual during bootstrap
|
|
21
|
+
* pass `fallback={null}`. Consumers who want to skip the gate entirely
|
|
22
|
+
* pass `fallback="passthrough"`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function DefaultFallback(): import("react/jsx-runtime").JSX.Element;
|