amplifyquery 1.0.16 → 1.0.17

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/dist/service.js CHANGED
@@ -1122,7 +1122,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
1122
1122
  },
1123
1123
  // React Hook returning method - Reimplemented based on TanStack Query
1124
1124
  useHook: (options) => {
1125
- var _a, _b, _c, _d, _e, _f, _g, _h;
1125
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
1126
1126
  const hookQueryClient = (0, react_query_1.useQueryClient)();
1127
1127
  // Determine query key
1128
1128
  const queryKey = (0, react_1.useMemo)(() => {
@@ -1170,16 +1170,82 @@ function createAmplifyService(modelName, defaultAuthMode) {
1170
1170
  (_g = options === null || options === void 0 ? void 0 : options.customList) === null || _g === void 0 ? void 0 : _g.forceRefresh,
1171
1171
  service,
1172
1172
  ]);
1173
+ const realtimeEnabled = ((_h = options === null || options === void 0 ? void 0 : options.realtime) === null || _h === void 0 ? void 0 : _h.enabled) === true;
1173
1174
  const queryOptions = {
1174
1175
  queryKey,
1175
1176
  queryFn,
1176
- enabled: ((_h = options === null || options === void 0 ? void 0 : options.initialFetchOptions) === null || _h === void 0 ? void 0 : _h.fetch) !== false,
1177
+ enabled: ((_j = options === null || options === void 0 ? void 0 : options.initialFetchOptions) === null || _j === void 0 ? void 0 : _j.fetch) !== false && !realtimeEnabled,
1177
1178
  staleTime: 1000 * 30, // Keep fresh for 30 seconds (refresh more frequently)
1178
1179
  refetchOnMount: true, // Refetch on component mount
1179
1180
  refetchOnWindowFocus: false, // Don't auto-refetch on window focus
1180
1181
  refetchOnReconnect: true, // Refetch on network reconnect
1181
1182
  };
1183
+ const [isSynced, setIsSynced] = (0, react_1.useState)(undefined);
1182
1184
  const { data: items = [], isLoading, error, refetch, } = (0, react_query_1.useQuery)(queryOptions);
1185
+ (0, react_1.useEffect)(() => {
1186
+ var _a, _b, _c;
1187
+ if (!realtimeEnabled)
1188
+ return;
1189
+ if (options === null || options === void 0 ? void 0 : options.customList) {
1190
+ console.warn(`🍬 ${modelName} useHook realtime: customList is not supported.`);
1191
+ return;
1192
+ }
1193
+ const client = (0, client_1.getClient)();
1194
+ const model = (_a = client.models) === null || _a === void 0 ? void 0 : _a[modelName];
1195
+ if (!(model === null || model === void 0 ? void 0 : model.observeQuery)) {
1196
+ console.warn(`🍬 ${modelName} useHook realtime: observeQuery not available.`);
1197
+ return;
1198
+ }
1199
+ const observeOptions = Object.assign({}, (((_b = options === null || options === void 0 ? void 0 : options.realtime) === null || _b === void 0 ? void 0 : _b.observeOptions) || {}));
1200
+ if (observeOptions.filter === undefined &&
1201
+ ((_c = options === null || options === void 0 ? void 0 : options.initialFetchOptions) === null || _c === void 0 ? void 0 : _c.filter)) {
1202
+ observeOptions.filter = options.initialFetchOptions.filter;
1203
+ }
1204
+ let isMounted = true;
1205
+ const subscription = model.observeQuery(observeOptions).subscribe({
1206
+ next: ({ items: nextItems, isSynced: synced }) => {
1207
+ if (!isMounted)
1208
+ return;
1209
+ const safeItems = Array.isArray(nextItems)
1210
+ ? nextItems.filter(Boolean)
1211
+ : [];
1212
+ const previousItems = hookQueryClient.getQueryData(queryKey) || [];
1213
+ const previousIds = new Set(previousItems.map((item) => item === null || item === void 0 ? void 0 : item.id).filter(Boolean));
1214
+ const nextIds = new Set(safeItems.map((item) => item === null || item === void 0 ? void 0 : item.id).filter(Boolean));
1215
+ previousIds.forEach((id) => {
1216
+ if (!nextIds.has(id)) {
1217
+ hookQueryClient.removeQueries({
1218
+ queryKey: itemKey(modelName, id),
1219
+ exact: true,
1220
+ });
1221
+ }
1222
+ });
1223
+ hookQueryClient.setQueryData(queryKey, safeItems);
1224
+ safeItems.forEach((item) => {
1225
+ if (item === null || item === void 0 ? void 0 : item.id) {
1226
+ hookQueryClient.setQueryData(itemKey(modelName, item.id), item);
1227
+ }
1228
+ });
1229
+ setIsSynced(Boolean(synced));
1230
+ },
1231
+ error: (err) => {
1232
+ console.error(`🍬 ${modelName} useHook realtime subscribe error:`, err);
1233
+ },
1234
+ });
1235
+ return () => {
1236
+ var _a;
1237
+ isMounted = false;
1238
+ (_a = subscription === null || subscription === void 0 ? void 0 : subscription.unsubscribe) === null || _a === void 0 ? void 0 : _a.call(subscription);
1239
+ };
1240
+ }, [
1241
+ realtimeEnabled,
1242
+ modelName,
1243
+ queryKey,
1244
+ hookQueryClient,
1245
+ options === null || options === void 0 ? void 0 : options.customList,
1246
+ (_k = options === null || options === void 0 ? void 0 : options.initialFetchOptions) === null || _k === void 0 ? void 0 : _k.filter,
1247
+ (_l = options === null || options === void 0 ? void 0 : options.realtime) === null || _l === void 0 ? void 0 : _l.observeOptions,
1248
+ ]);
1183
1249
  // Interface functions implementation
1184
1250
  const getItem = (0, react_1.useCallback)((id) => {
1185
1251
  // Use useQueryData to get latest single item from current cache
@@ -1282,6 +1348,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
1282
1348
  items,
1283
1349
  isLoading,
1284
1350
  error: error,
1351
+ isSynced,
1285
1352
  getItem,
1286
1353
  refresh,
1287
1354
  create: createItem,
package/dist/types.d.ts CHANGED
@@ -20,6 +20,7 @@ export type StoreState<T> = {
20
20
  export type ModelHook<T> = {
21
21
  items: T[];
22
22
  isLoading: boolean;
23
+ isSynced?: boolean;
23
24
  error: Error | null;
24
25
  getItem: (id: string) => T | null | undefined;
25
26
  refresh: (options?: {
@@ -92,6 +93,10 @@ export interface AmplifyDataService<T> {
92
93
  args: Record<string, any>;
93
94
  forceRefresh?: boolean;
94
95
  };
96
+ realtime?: {
97
+ enabled?: boolean;
98
+ observeOptions?: Record<string, any>;
99
+ };
95
100
  }) => ModelHook<T>;
96
101
  useItemHook: (id: string) => ItemHook<T>;
97
102
  modelName: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "amplifyquery",
3
- "version": "1.0.16",
3
+ "version": "1.0.17",
4
4
  "description": "Amplify+Query",
5
5
  "keywords": [
6
6
  "Amplify",