Skip to content

Commit 2e520f2

Browse files
authored
fix(rwa): updating roles in realtime (#2801)
1 parent 4cb2535 commit 2e520f2

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

packages/apps/rwa-demo/src/hooks/getAgentRoles.ts

+35-7
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,24 @@ export const useGetAgentRoles = ({
4545
},
4646
});
4747

48+
const { data: subscriptionAgentRemovedData } =
49+
useEventSubscriptionSubscription({
50+
variables: {
51+
qualifiedName: `${getAsset()}.AGENT-REMOVED`,
52+
},
53+
});
54+
55+
const { data: subscriptionAgentAddedData } = useEventSubscriptionSubscription(
56+
{
57+
variables: {
58+
qualifiedName: `${getAsset()}.AGENT-ADDED`,
59+
},
60+
},
61+
);
62+
4863
const initInnerData = async (agentArg: string) => {
4964
const data = await getAgentRoles({ agent: agentArg });
65+
5066
setInnerData(data);
5167
setIsMounted(true);
5268
};
@@ -62,13 +78,25 @@ export const useGetAgentRoles = ({
6278
}, [agent]);
6379

6480
useEffect(() => {
65-
subscriptionData?.events?.map((event) => {
66-
const params = JSON.parse(event.parameters ?? '[]');
67-
if (params[0] === agent) {
68-
setInnerData(params[1]);
69-
}
70-
});
71-
}, [subscriptionData]);
81+
const { events: rolesUpdatedEvents } = subscriptionData ?? {};
82+
const { events: agentRemovedEvents } = subscriptionAgentRemovedData ?? {};
83+
const { events: agentAddedEvents } = subscriptionAgentAddedData ?? {};
84+
85+
[rolesUpdatedEvents, agentRemovedEvents, agentAddedEvents]
86+
.flat()
87+
?.find((event) => {
88+
if (!event) return;
89+
const params = JSON.parse(event.parameters ?? '[]');
90+
if (params[0] === agent && !!agent) {
91+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
92+
initInnerData(agent!);
93+
}
94+
});
95+
}, [
96+
subscriptionData,
97+
subscriptionAgentRemovedData,
98+
subscriptionAgentAddedData,
99+
]);
72100

73101
const getAll = useCallback(() => {
74102
return innerData;

0 commit comments

Comments
 (0)