@syncular/client-react 0.0.3-3 → 0.0.3-7

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/src/index.ts CHANGED
@@ -29,8 +29,24 @@ export type {
29
29
  UseResolveConflictResult,
30
30
  UseSyncConnectionResult,
31
31
  UseSyncEngineResult,
32
+ UseSyncInspectorOptions,
33
+ UseSyncInspectorResult,
34
+ UseSyncProgressOptions,
35
+ UseSyncProgressResult,
32
36
  UseSyncQueryOptions,
33
37
  UseSyncQueryResult,
38
+ UseSyncStatusOptions,
39
+ UseSyncSubscriptionResult,
40
+ UseSyncSubscriptionsOptions,
41
+ UseSyncSubscriptionsResult,
42
+ UseTransportHealthResult,
34
43
  } from './createSyncularReact';
35
44
  // Re-export core client types for convenience.
36
45
  export { createSyncularReact } from './createSyncularReact';
46
+ export type {
47
+ SyncGroupChannel,
48
+ SyncGroupChannelSnapshot,
49
+ SyncGroupStatus,
50
+ UseSyncGroupResult,
51
+ } from './useSyncGroup';
52
+ export { useSyncGroup } from './useSyncGroup';
@@ -0,0 +1,169 @@
1
+ import type {
2
+ SyncClientDb,
3
+ SyncEngine,
4
+ SyncProgress,
5
+ SyncRepairOptions,
6
+ SyncResetOptions,
7
+ SyncResetResult,
8
+ SyncResult,
9
+ SyncTransportMode,
10
+ TransportHealth,
11
+ } from '@syncular/client';
12
+ import { useCallback, useMemo, useSyncExternalStore } from 'react';
13
+
14
+ export interface SyncGroupChannel<DB extends SyncClientDb = SyncClientDb> {
15
+ id: string;
16
+ engine: SyncEngine<DB>;
17
+ }
18
+
19
+ export interface SyncGroupStatus {
20
+ phase: 'idle' | 'syncing' | 'live' | 'error';
21
+ isOnline: boolean;
22
+ isSyncing: boolean;
23
+ pendingCount: number;
24
+ retryCount: number;
25
+ hasError: boolean;
26
+ }
27
+
28
+ export interface SyncGroupChannelSnapshot {
29
+ id: string;
30
+ transportMode: SyncTransportMode;
31
+ transport: TransportHealth;
32
+ isOnline: boolean;
33
+ isSyncing: boolean;
34
+ pendingCount: number;
35
+ retryCount: number;
36
+ lastSyncAt: number | null;
37
+ error: { code: string; message: string } | null;
38
+ }
39
+
40
+ export interface UseSyncGroupResult {
41
+ status: SyncGroupStatus;
42
+ channels: SyncGroupChannelSnapshot[];
43
+ syncNow: () => Promise<SyncResult[]>;
44
+ reset: (options: SyncResetOptions) => Promise<SyncResetResult[]>;
45
+ repair: (options: SyncRepairOptions) => Promise<SyncResetResult[]>;
46
+ getProgress: () => Promise<Array<{ id: string; progress: SyncProgress }>>;
47
+ }
48
+
49
+ export function useSyncGroup<DB extends SyncClientDb = SyncClientDb>(args: {
50
+ channels: SyncGroupChannel<DB>[];
51
+ }): UseSyncGroupResult {
52
+ const { channels } = args;
53
+
54
+ const subscribe = useCallback(
55
+ (callback: () => void) => {
56
+ const unsubs = channels.flatMap((channel) => [
57
+ channel.engine.subscribe(callback),
58
+ channel.engine.on('connection:change', callback),
59
+ channel.engine.on('sync:complete', callback),
60
+ channel.engine.on('sync:error', callback),
61
+ channel.engine.on('outbox:change', callback),
62
+ ]);
63
+
64
+ return () => {
65
+ for (const unsubscribe of unsubs) unsubscribe();
66
+ };
67
+ },
68
+ [channels]
69
+ );
70
+
71
+ const getSnapshot = useCallback(
72
+ () =>
73
+ channels.map((channel) => {
74
+ const state = channel.engine.getState();
75
+ const transport = channel.engine.getTransportHealth();
76
+
77
+ return {
78
+ id: channel.id,
79
+ transportMode: state.transportMode,
80
+ transport,
81
+ isOnline: state.connectionState === 'connected',
82
+ isSyncing: state.isSyncing,
83
+ pendingCount: state.pendingCount,
84
+ retryCount: state.retryCount,
85
+ lastSyncAt: state.lastSyncAt,
86
+ error: state.error
87
+ ? { code: state.error.code, message: state.error.message }
88
+ : null,
89
+ };
90
+ }),
91
+ [channels]
92
+ );
93
+
94
+ const channelSnapshots = useSyncExternalStore(
95
+ subscribe,
96
+ getSnapshot,
97
+ getSnapshot
98
+ );
99
+
100
+ const status = useMemo<SyncGroupStatus>(() => {
101
+ const hasError = channelSnapshots.some((channel) => channel.error !== null);
102
+ const isSyncing = channelSnapshots.some((channel) => channel.isSyncing);
103
+ const isOnline =
104
+ channelSnapshots.length > 0 &&
105
+ channelSnapshots.every((channel) => channel.isOnline);
106
+ const pendingCount = channelSnapshots.reduce(
107
+ (sum, channel) => sum + channel.pendingCount,
108
+ 0
109
+ );
110
+ const retryCount = channelSnapshots.reduce(
111
+ (sum, channel) => sum + channel.retryCount,
112
+ 0
113
+ );
114
+
115
+ const phase: SyncGroupStatus['phase'] = hasError
116
+ ? 'error'
117
+ : isSyncing
118
+ ? 'syncing'
119
+ : channelSnapshots.every((channel) => channel.lastSyncAt !== null)
120
+ ? 'live'
121
+ : 'idle';
122
+
123
+ return {
124
+ phase,
125
+ isOnline,
126
+ isSyncing,
127
+ pendingCount,
128
+ retryCount,
129
+ hasError,
130
+ };
131
+ }, [channelSnapshots]);
132
+
133
+ const syncNow = useCallback(
134
+ () => Promise.all(channels.map((channel) => channel.engine.sync())),
135
+ [channels]
136
+ );
137
+
138
+ const reset = useCallback(
139
+ (options: SyncResetOptions) =>
140
+ Promise.all(channels.map((channel) => channel.engine.reset(options))),
141
+ [channels]
142
+ );
143
+
144
+ const repair = useCallback(
145
+ (options: SyncRepairOptions) =>
146
+ Promise.all(channels.map((channel) => channel.engine.repair(options))),
147
+ [channels]
148
+ );
149
+
150
+ const getProgress = useCallback(
151
+ () =>
152
+ Promise.all(
153
+ channels.map(async (channel) => ({
154
+ id: channel.id,
155
+ progress: await channel.engine.getProgress(),
156
+ }))
157
+ ),
158
+ [channels]
159
+ );
160
+
161
+ return {
162
+ status,
163
+ channels: channelSnapshots,
164
+ syncNow,
165
+ reset,
166
+ repair,
167
+ getProgress,
168
+ };
169
+ }