@ai4b-team/fsaos-gateway-sdk 3.2.0 → 3.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/dist/index.js CHANGED
@@ -2449,8 +2449,72 @@ function useInfiniteChannelMessages(options) {
2449
2449
  "vfs_change",
2450
2450
  () => {
2451
2451
  if (debounceRef.current) clearTimeout(debounceRef.current);
2452
- debounceRef.current = setTimeout(() => {
2453
- qc.invalidateQueries({ queryKey: stableKey });
2452
+ debounceRef.current = setTimeout(async () => {
2453
+ const currentData = qc.getQueryData(stableKey);
2454
+ if (!currentData?.pages?.length) {
2455
+ qc.invalidateQueries({ queryKey: stableKey });
2456
+ return;
2457
+ }
2458
+ let maxSeq = 0;
2459
+ for (const page of currentData.pages) {
2460
+ for (const msg of page.messages) {
2461
+ if (msg.seq > maxSeq) maxSeq = msg.seq;
2462
+ }
2463
+ }
2464
+ if (maxSeq === 0) {
2465
+ qc.invalidateQueries({ queryKey: stableKey });
2466
+ return;
2467
+ }
2468
+ try {
2469
+ const newPage = await fetchChannelMessagesPage(channelPath, {
2470
+ parentMessageId,
2471
+ limit,
2472
+ after_seq: maxSeq
2473
+ });
2474
+ if (newPage.messages.length === 0 && newPage.siblingCards.length === 0) {
2475
+ qc.invalidateQueries({ queryKey: stableKey });
2476
+ return;
2477
+ }
2478
+ if (newPage.messages.length > 0) {
2479
+ const minNewSeq = Math.min(...newPage.messages.map((m) => m.seq));
2480
+ if (minNewSeq > maxSeq + 1) {
2481
+ console.warn(
2482
+ `[useInfiniteChannelMessages] sequence gap detected: maxSeq=${maxSeq}, minNewSeq=${minNewSeq}. Triggering full refetch.`
2483
+ );
2484
+ qc.invalidateQueries({ queryKey: stableKey });
2485
+ return;
2486
+ }
2487
+ }
2488
+ qc.setQueryData(stableKey, (old) => {
2489
+ if (!old) return old;
2490
+ const pages = [...old.pages];
2491
+ const lastIdx = pages.length - 1;
2492
+ const lastPage = pages[lastIdx];
2493
+ const existingIds = new Set(lastPage.messages.map((m) => m.id));
2494
+ const uniqueNew = newPage.messages.filter((m) => !existingIds.has(m.id));
2495
+ const mergedMessages = [...lastPage.messages, ...uniqueNew].sort((a, b) => a.seq - b.seq);
2496
+ const mergedMsgCards = new Map(lastPage.messageCards);
2497
+ for (const [path, cards] of newPage.messageCards) {
2498
+ const existing = mergedMsgCards.get(path) || [];
2499
+ const existingCardIds = new Set(existing.map((c) => c.id));
2500
+ const newCards = cards.filter((c) => !existingCardIds.has(c.id));
2501
+ mergedMsgCards.set(path, [...existing, ...newCards]);
2502
+ }
2503
+ const existingSibIds = new Set(lastPage.siblingCards.map((c) => c.id));
2504
+ const newSiblings = newPage.siblingCards.filter((c) => !existingSibIds.has(c.id));
2505
+ const mergedSiblings = [...lastPage.siblingCards, ...newSiblings];
2506
+ pages[lastIdx] = {
2507
+ ...lastPage,
2508
+ messages: mergedMessages,
2509
+ messageCards: mergedMsgCards,
2510
+ siblingCards: mergedSiblings
2511
+ };
2512
+ return { ...old, pages };
2513
+ });
2514
+ } catch (err) {
2515
+ console.warn("[useInfiniteChannelMessages] live tail fetch failed, falling back to full invalidation:", err);
2516
+ qc.invalidateQueries({ queryKey: stableKey });
2517
+ }
2454
2518
  }, 500);
2455
2519
  },
2456
2520
  (data) => {
@@ -2461,7 +2525,7 @@ function useInfiniteChannelMessages(options) {
2461
2525
  unsub();
2462
2526
  if (debounceRef.current) clearTimeout(debounceRef.current);
2463
2527
  };
2464
- }, [realtime, channelPath, queryEnabled, stableKey, qc]);
2528
+ }, [realtime, channelPath, queryEnabled, stableKey, qc, parentMessageId, limit]);
2465
2529
  return {
2466
2530
  messages,
2467
2531
  /** Cards grouped by parent message path (child cards of messages). */