doover-js 0.4.8 → 0.4.9
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.
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { type InfiniteData, type UseInfiniteQueryResult } from "@tanstack/react-query";
|
|
2
2
|
import type { MessageStructure } from "../types/common";
|
|
3
3
|
import type { ChannelIdentifier } from "../types/viewer";
|
|
4
|
-
export declare function channelMessagesQueryKey(agentId: string | undefined, channelName: string | undefined): readonly ["doover", "agent", string | undefined, "channel", string | undefined, "messages"]
|
|
4
|
+
export declare function channelMessagesQueryKey(agentId: string | undefined, channelName: string | undefined, fields?: readonly string[]): readonly ["doover", "agent", string | undefined, "channel", string | undefined, "messages"] | readonly ["doover", "agent", string | undefined, "channel", string | undefined, "messages", {
|
|
5
|
+
readonly fields: string[];
|
|
6
|
+
}];
|
|
5
7
|
export interface UseChannelMessagesOptions {
|
|
6
8
|
limit?: number;
|
|
7
9
|
/** If false, skip subscribing for live message-create updates. Defaults true. */
|
|
@@ -6,8 +6,17 @@ const react_1 = require("react");
|
|
|
6
6
|
const react_query_1 = require("@tanstack/react-query");
|
|
7
7
|
const context_1 = require("./context");
|
|
8
8
|
const useChannelSubscription_1 = require("./useChannelSubscription");
|
|
9
|
-
function channelMessagesQueryKey(agentId, channelName) {
|
|
10
|
-
|
|
9
|
+
function channelMessagesQueryKey(agentId, channelName, fields) {
|
|
10
|
+
const key = ["doover", "agent", agentId, "channel", channelName, "messages"];
|
|
11
|
+
if (!fields || fields.length === 0)
|
|
12
|
+
return key;
|
|
13
|
+
// A `field_name`-filtered request returns a different message stream than
|
|
14
|
+
// the unfiltered (or differently-filtered) one, so it must live under its
|
|
15
|
+
// own cache entry — otherwise one filter's pagination cursor leaks into
|
|
16
|
+
// another's `getNextPageParam`, and the "next page" fetch ends up anchored
|
|
17
|
+
// to an unrelated (often much older) message id. Sort so call-site argument
|
|
18
|
+
// order doesn't fragment the cache.
|
|
19
|
+
return [...key, { fields: [...fields].sort() }];
|
|
11
20
|
}
|
|
12
21
|
/**
|
|
13
22
|
* Paginated infinite query over `DooverDataProvider.getMessages`, with live
|
|
@@ -24,8 +33,19 @@ function useChannelMessages(identifier, options) {
|
|
|
24
33
|
const initialBefore = options?.initialBefore;
|
|
25
34
|
const after = options?.after;
|
|
26
35
|
const autoPaginate = options?.autoPaginate ?? false;
|
|
27
|
-
const key = channelMessagesQueryKey(agentId, channelName);
|
|
36
|
+
const key = channelMessagesQueryKey(agentId, channelName, fields);
|
|
28
37
|
const onMessage = (0, react_1.useCallback)((message) => {
|
|
38
|
+
// This cache entry is scoped to `fields`; the gateway delivers every
|
|
39
|
+
// message on the channel, so drop live pushes whose payload doesn't
|
|
40
|
+
// carry one of those fields rather than letting them pollute it.
|
|
41
|
+
if (fields && fields.length > 0) {
|
|
42
|
+
const data = message.data;
|
|
43
|
+
if (!data ||
|
|
44
|
+
typeof data !== "object" ||
|
|
45
|
+
!fields.some((f) => f in data)) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
29
49
|
queryClient.setQueryData(key, (current) => {
|
|
30
50
|
if (!current)
|
|
31
51
|
return current;
|
|
@@ -39,7 +59,7 @@ function useChannelMessages(identifier, options) {
|
|
|
39
59
|
});
|
|
40
60
|
},
|
|
41
61
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
42
|
-
[queryClient, agentId, channelName]);
|
|
62
|
+
[queryClient, agentId, channelName, fields?.join(",")]);
|
|
43
63
|
(0, useChannelSubscription_1.useChannelSubscription)(liveUpdates ? identifier : undefined, { onMessage });
|
|
44
64
|
const query = (0, react_query_1.useInfiniteQuery)({
|
|
45
65
|
queryKey: key,
|