-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathbaseEndpoints.ts
More file actions
46 lines (41 loc) · 1.7 KB
/
baseEndpoints.ts
File metadata and controls
46 lines (41 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import {getFramework, getSubgraphClient} from '../../../../sdkReduxConfig';
import {MillisecondTimes} from '../../../../utils';
import {invalidateSpecificCacheTagsForEvents} from '../../cacheTags/invalidateSpecificCacheTagsForEvents';
import {CacheTime} from '../../cacheTime';
import {type SubgraphEndpointBuilder} from '../subgraphEndpointBuilder';
import {type CustomSubgraphQuery, type MonitorForEventsToInvalidateCacheMutation} from './baseArgs';
export const createBaseEndpoints = (builder: SubgraphEndpointBuilder) => ({
custom: builder.query<unknown, CustomSubgraphQuery>({
keepUnusedDataFor: CacheTime.None,
queryFn: async (arg) => {
const subgraphClient = await getSubgraphClient(arg.chainId);
return {
data: await subgraphClient.request<unknown>(arg.document, arg.variables),
};
},
}),
monitorForEventsToInvalidateCache: builder.mutation<true, MonitorForEventsToInvalidateCacheMutation>({
queryFn: () => {
// No-op
return {
data: true,
};
},
onCacheEntryAdded: async (arg, {dispatch, cacheDataLoaded, cacheEntryRemoved}) => {
const framework = await getFramework(arg.chainId);
await cacheDataLoaded;
const unsubscribe = framework.query.on(
(events) => {
invalidateSpecificCacheTagsForEvents(arg.chainId, events, dispatch);
},
MillisecondTimes.TwentySeconds,
arg.address
);
try {
await cacheEntryRemoved;
} finally {
unsubscribe();
}
},
}),
});