amplifyquery 1.0.9 β 1.0.11
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 +40 -14
- package/package.json +1 -1
package/dist/service.js
CHANGED
|
@@ -11,9 +11,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.createAmplifyService = createAmplifyService;
|
|
13
13
|
const client_1 = require("./client");
|
|
14
|
+
const config_1 = require("./config");
|
|
14
15
|
const query_1 = require("./query");
|
|
15
16
|
const utils_1 = require("./utils");
|
|
16
|
-
const config_1 = require("./config");
|
|
17
17
|
const react_query_1 = require("@tanstack/react-query");
|
|
18
18
|
const auth_1 = require("aws-amplify/auth");
|
|
19
19
|
const expo_crypto_1 = require("expo-crypto");
|
|
@@ -492,7 +492,10 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
492
492
|
let item = apiResponse;
|
|
493
493
|
if (Array.isArray(apiResponse)) {
|
|
494
494
|
console.warn(`π¬ ${modelName} get: API returned array instead of single item. Taking first item.`);
|
|
495
|
-
item =
|
|
495
|
+
item =
|
|
496
|
+
apiResponse.find((i) => (i === null || i === void 0 ? void 0 : i.id) === id) ||
|
|
497
|
+
apiResponse[0] ||
|
|
498
|
+
null;
|
|
496
499
|
}
|
|
497
500
|
// Update cache
|
|
498
501
|
if (item) {
|
|
@@ -1186,8 +1189,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1186
1189
|
console.error(`π¬ ${modelName} useHook create error:`, _error);
|
|
1187
1190
|
throw _error; // Re-throw error
|
|
1188
1191
|
}
|
|
1189
|
-
}), [service, refetch]
|
|
1190
|
-
);
|
|
1192
|
+
}), [service, refetch]);
|
|
1191
1193
|
const updateItem = (0, react_1.useCallback)((data) => __awaiter(this, void 0, void 0, function* () {
|
|
1192
1194
|
try {
|
|
1193
1195
|
const result = yield service.update(data);
|
|
@@ -1199,8 +1201,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1199
1201
|
console.error(`π¬ ${modelName} useHook update error:`, _error);
|
|
1200
1202
|
throw _error;
|
|
1201
1203
|
}
|
|
1202
|
-
}), [service, refetch]
|
|
1203
|
-
);
|
|
1204
|
+
}), [service, refetch]);
|
|
1204
1205
|
const deleteItem = (0, react_1.useCallback)((id) => __awaiter(this, void 0, void 0, function* () {
|
|
1205
1206
|
try {
|
|
1206
1207
|
const result = yield service.delete(id);
|
|
@@ -1212,8 +1213,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1212
1213
|
console.error(`π¬ ${modelName} useHook delete error:`, error);
|
|
1213
1214
|
throw error;
|
|
1214
1215
|
}
|
|
1215
|
-
}), [service, refetch]
|
|
1216
|
-
);
|
|
1216
|
+
}), [service, refetch]);
|
|
1217
1217
|
const refresh = (0, react_1.useCallback)((refreshOptions) => __awaiter(this, void 0, void 0, function* () {
|
|
1218
1218
|
console.log(`π¬ ${modelName} useHook refresh called`, queryKey);
|
|
1219
1219
|
const { data } = yield refetch({ throwOnError: true }); // Throw on error
|
|
@@ -1247,12 +1247,26 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1247
1247
|
const singleItemQueryKey = [modelName, id];
|
|
1248
1248
|
// First check data from cache
|
|
1249
1249
|
const rawCachedData = hookQueryClient.getQueryData(singleItemQueryKey);
|
|
1250
|
-
// π§ λ²κ·Έ μμ :
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
:
|
|
1254
|
-
|
|
1250
|
+
// π§ λ²κ·Έ μμ : λ°°μ΄μ΄ μΊμλμ΄ μλ κ²½μ° μ²λ¦¬
|
|
1251
|
+
let cachedData;
|
|
1252
|
+
if (Array.isArray(rawCachedData)) {
|
|
1253
|
+
console.warn(`π¬ ${modelName} useItemHook: Cache contains array instead of single item. Finding matching item.`);
|
|
1254
|
+
const matchingItem = rawCachedData.find((item) => (item === null || item === void 0 ? void 0 : item.id) === id);
|
|
1255
|
+
cachedData = matchingItem || undefined;
|
|
1256
|
+
// λ°°μ΄μ΄ μΊμλμ΄ μμΌλ©΄ μ‘°μ©ν μΊμλ₯Ό μ κ±° (무ν 루ν λ°©μ§)
|
|
1257
|
+
setTimeout(() => {
|
|
1258
|
+
hookQueryClient.removeQueries({
|
|
1259
|
+
queryKey: singleItemQueryKey,
|
|
1260
|
+
exact: true,
|
|
1261
|
+
});
|
|
1262
|
+
}, 0);
|
|
1263
|
+
}
|
|
1264
|
+
else if (rawCachedData && (rawCachedData === null || rawCachedData === void 0 ? void 0 : rawCachedData.id) === id) {
|
|
1265
|
+
cachedData = rawCachedData;
|
|
1266
|
+
}
|
|
1267
|
+
else if (rawCachedData) {
|
|
1255
1268
|
console.warn(`π¬ ${modelName} useItemHook: Cache ID mismatch! Requested: ${id}, Cached: ${rawCachedData === null || rawCachedData === void 0 ? void 0 : rawCachedData.id}. Ignoring cached data.`);
|
|
1269
|
+
cachedData = undefined;
|
|
1256
1270
|
}
|
|
1257
1271
|
// Single item query
|
|
1258
1272
|
const { data: item, isLoading, error, refetch, } = (0, react_query_1.useQuery)({
|
|
@@ -1260,7 +1274,8 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1260
1274
|
queryFn: () => service.get(id),
|
|
1261
1275
|
initialData: cachedData, // Use cached data as initial value if available
|
|
1262
1276
|
staleTime: 1000 * 60, // Keep data "fresh" for 1 minute
|
|
1263
|
-
refetchOnMount:
|
|
1277
|
+
refetchOnMount: cachedData ? false : true, // Only refetch if no cached data
|
|
1278
|
+
refetchOnWindowFocus: false, // Disable window focus refetch to prevent loops
|
|
1264
1279
|
enabled: !!id, // Only enable query when id exists
|
|
1265
1280
|
});
|
|
1266
1281
|
// useMutation hooks call service methods,
|
|
@@ -1305,6 +1320,17 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1305
1320
|
}), [deleteMutation]);
|
|
1306
1321
|
// Change loading state to false when isLoading is true and cached data exists
|
|
1307
1322
|
const effectiveLoading = isLoading && !cachedData;
|
|
1323
|
+
// μΊμ μ 리λ₯Ό μν ν¨κ³Ό μ΅μ ν (ν λ²λ§ μ€ν)
|
|
1324
|
+
const shouldCleanCache = Array.isArray(rawCachedData);
|
|
1325
|
+
if (shouldCleanCache && !isLoading) {
|
|
1326
|
+
// λ‘λ©μ΄ μλ£λ νμλ§ μΊμ μ 리 μ€ν
|
|
1327
|
+
setTimeout(() => {
|
|
1328
|
+
const currentCache = hookQueryClient.getQueryData(singleItemQueryKey);
|
|
1329
|
+
if (Array.isArray(currentCache)) {
|
|
1330
|
+
hookQueryClient.setQueryData(singleItemQueryKey, item);
|
|
1331
|
+
}
|
|
1332
|
+
}, 100);
|
|
1333
|
+
}
|
|
1308
1334
|
return {
|
|
1309
1335
|
item: item || null,
|
|
1310
1336
|
isLoading: effectiveLoading, // Not loading if cached data exists
|