Код: Выделить всё
import { api } from "../../api";
import { createSocketFactory } from "@/services/socket";
import type { Coin } from "@/interfaces/global/coin.types";
export const pricesApi = api.injectEndpoints({
endpoints: (build) => ({
getCoinPrices: build.query({
queryFn: () => ({ data: [] }),
onCacheEntryAdded: async (
arg,
{ updateCachedData, cacheDataLoaded, cacheEntryRemoved }
) => {
// Create the socket IO instance and start the connection when the cache subscription starts
const socket = await createSocketFactory();
try {
// Wait for the initial query to resolve before proceeding
await cacheDataLoaded;
// Listen for price updates
const listener = (data: Coin[]) => {
updateCachedData(() => data);
};
// Start listening for price updates
socket.on("prices", listener);
} catch (error) {}
// `cacheEntryRemoved` will resolve when the cache subscription is no longer active
await cacheEntryRemoved;
// Perform cleanup steps once the `cacheEntryRemoved` promise resolves
socket.off();
socket.disconnect();
},
}),
}),
});
export const { useGetCoinPricesQuery } = pricesApi;
< /p>
Ожидается ли это поведение? Потому что я использовал такой же способ обработки подключения к сокетам в другом проекте, который использовал RTK v1.9, а ISloading получит false после загрузки данных.
Как я могу отслеживать загрузку Укажите, если это не реальная проблема?
Подробнее здесь: https://stackoverflow.com/questions/793 ... en-fetched