Skip to content

Commit 74da85c

Browse files
committed
update macro updater to use useQuery
1 parent 155c749 commit 74da85c

2 files changed

Lines changed: 195 additions & 189 deletions

File tree

dashboard/src/data/EventStream.ts

Lines changed: 86 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
1-
import { useUid } from 'data/UserInfo';
2-
import { addInstance, deleteInstance, updateInstance } from 'data/InstanceList';
3-
import { LodestoneContext } from 'data/LodestoneContext';
4-
import { useQueryClient } from '@tanstack/react-query';
5-
import { useCallback, useContext, useEffect, useMemo, useRef } from 'react';
6-
import { InstanceState } from 'bindings/InstanceState';
7-
import { ClientEvent } from 'bindings/ClientEvent';
8-
import { match, otherwise } from 'variant';
9-
import { NotificationContext } from './NotificationContext';
10-
import { EventQuery } from 'bindings/EventQuery';
1+
import {useUid} from 'data/UserInfo';
2+
import {addInstance, deleteInstance, updateInstance} from 'data/InstanceList';
3+
import {LodestoneContext} from 'data/LodestoneContext';
4+
import {useQueryClient} from '@tanstack/react-query';
5+
import {useCallback, useContext, useEffect, useMemo, useRef} from 'react';
6+
import {InstanceState} from 'bindings/InstanceState';
7+
import {ClientEvent} from 'bindings/ClientEvent';
8+
import {match, otherwise} from 'variant';
9+
import {NotificationContext} from './NotificationContext';
10+
import {EventQuery} from 'bindings/EventQuery';
1111
import axios from 'axios';
12-
import { LODESTONE_PORT } from 'utils/util';
13-
import { UserPermission } from 'bindings/UserPermission';
14-
import { PublicUser } from 'bindings/PublicUser';
15-
import { toast } from 'react-toastify';
16-
import { Player } from 'bindings/Player';
12+
import {LODESTONE_PORT} from 'utils/util';
13+
import {UserPermission} from 'bindings/UserPermission';
14+
import {PublicUser} from 'bindings/PublicUser';
15+
import {toast} from 'react-toastify';
16+
import {Player} from 'bindings/Player';
17+
import {TaskEntry} from "../bindings/TaskEntry";
18+
import {getTasks} from "../utils/apis";
19+
import {HistoryEntry} from "../bindings/HistoryEntry";
1720

1821
/**
1922
* does not return anything, call this for the side effect of subscribing to the event stream
2023
* information will be available in the query cache of the respective query cache
2124
*/
2225
export const useEventStream = () => {
2326
const queryClient = useQueryClient();
24-
const { dispatch, ongoingDispatch } = useContext(NotificationContext);
27+
const {dispatch, ongoingDispatch} = useContext(NotificationContext);
2528
const selfUid = useUid();
26-
const { token, core, setCoreConnectionStatus, setToken } =
29+
const {token, core, setCoreConnectionStatus, setToken} =
2730
useContext(LodestoneContext);
2831
const socket = `${core.address}:${core.port}`;
2932
const wsRef = useRef<WebSocket | null>(null);
@@ -46,7 +49,7 @@ export const useEventStream = () => {
4649
const updateInstanceState = useCallback(
4750
(uuid: string, state: InstanceState) => {
4851
updateInstance(uuid, queryClient, (oldInfo) => {
49-
return { ...oldInfo, state };
52+
return {...oldInfo, state};
5053
});
5154
},
5255
[queryClient]
@@ -91,16 +94,16 @@ export const useEventStream = () => {
9194

9295
const handleEvent = useCallback(
9396
(event: ClientEvent, fresh: boolean) => {
94-
const { event_inner, snowflake } = event;
97+
const {event_inner, snowflake} = event;
9598

9699
match(event_inner, {
97100
InstanceEvent: ({
98-
instance_event_inner: event_inner,
99-
instance_uuid: uuid,
100-
instance_name: name,
101-
}) =>
101+
instance_event_inner: event_inner,
102+
instance_uuid: uuid,
103+
instance_name: name,
104+
}) =>
102105
match(event_inner, {
103-
StateTransition: ({ to }) => {
106+
StateTransition: ({to}) => {
104107
if (fresh) updateInstanceState(uuid, to);
105108
dispatch({
106109
title: `Instance ${name} ${
@@ -137,16 +140,16 @@ export const useEventStream = () => {
137140
fresh,
138141
});
139142
},
140-
InstanceInput: ({ message }) => {
143+
InstanceInput: ({message}) => {
141144
console.log(`Got input on ${name}: ${message}`);
142145
},
143-
InstanceOutput: ({ message }) => {
146+
InstanceOutput: ({message}) => {
144147
console.log(`Got output on ${name}: ${message}`);
145148
},
146-
SystemMessage: ({ message }) => {
149+
SystemMessage: ({message}) => {
147150
console.log(`Got system message on ${name}: ${message}`);
148151
},
149-
PlayerChange: ({ player_list, players_joined, players_left }) => {
152+
PlayerChange: ({player_list, players_joined, players_left}) => {
150153
console.log(`Got player change on ${name}: ${player_list}`);
151154
console.log(`${players_joined} joined ${name}`);
152155
console.log(`${players_left} left ${name}`);
@@ -178,7 +181,7 @@ export const useEventStream = () => {
178181
fresh,
179182
});
180183
},
181-
PlayerMessage: ({ player, player_message }) => {
184+
PlayerMessage: ({player, player_message}) => {
182185
console.log(`${player} said ${player_message} on ${name}`);
183186
dispatch({
184187
title: `${player} said ${player_message} on ${name}`,
@@ -188,7 +191,7 @@ export const useEventStream = () => {
188191
});
189192
},
190193
}),
191-
UserEvent: ({ user_id: uid, user_event_inner: event_inner }) =>
194+
UserEvent: ({user_id: uid, user_event_inner: event_inner}) =>
192195
match(event_inner, {
193196
UserCreated: () => {
194197
console.log(`User ${uid} created`);
@@ -211,7 +214,7 @@ export const useEventStream = () => {
211214
['user', 'list'],
212215
(oldList: { [uid: string]: PublicUser } | undefined) => {
213216
if (!oldList) return oldList;
214-
const newList = { ...oldList };
217+
const newList = {...oldList};
215218
delete newList[uid];
216219
return newList;
217220
}
@@ -239,7 +242,7 @@ export const useEventStream = () => {
239242
// type: 'add',
240243
// });
241244
},
242-
PermissionChanged: ({ new_permissions }) => {
245+
PermissionChanged: ({new_permissions}) => {
243246
if (fresh) {
244247
if (uid === selfUid) {
245248
updatePermission(new_permissions);
@@ -252,7 +255,7 @@ export const useEventStream = () => {
252255
...oldList[uid],
253256
permissions: new_permissions,
254257
};
255-
const newList = { ...oldList };
258+
const newList = {...oldList};
256259
newList[uid] = newUser;
257260
return newList;
258261
}
@@ -266,22 +269,14 @@ export const useEventStream = () => {
266269
},
267270
}),
268271
MacroEvent: ({
269-
instance_uuid: uuid,
270-
macro_pid,
271-
macro_event_inner: event_inner,
272-
}) =>
272+
instance_uuid: uuid,
273+
macro_pid,
274+
macro_event_inner: event_inner
275+
}) =>
273276
match(event_inner, {
274277
Started: () => {
275278
console.log(`Macro ${macro_pid} started on ${uuid}`);
276-
queryClient.setQueryData(
277-
['instance', uuid, 'taskList'],
278-
(oldList: number[] | undefined) => {
279-
if (!oldList) oldList = [];
280-
const newList = [...oldList];
281-
newList.push(macro_pid);
282-
return newList;
283-
}
284-
);
279+
queryClient.invalidateQueries(['instance', uuid, 'taskList']) // need to invalidate the query to get the new task list
285280
dispatch({
286281
title: `Macro ${macro_pid} started on ${uuid}`,
287282
event,
@@ -298,17 +293,41 @@ export const useEventStream = () => {
298293
fresh,
299294
});
300295
},
301-
Stopped: ({ exit_status }) => {
296+
Stopped: ({exit_status}) => {
302297
console.log(
303298
`Macro ${macro_pid} stopped on ${uuid} with status ${exit_status.type}`
304299
);
305-
queryClient.setQueryData(
306-
['instance', uuid, 'taskList'],
307-
(oldList: number[] | undefined) => {
308-
if (!oldList) return oldList;
309-
return oldList.filter((task_pid) => task_pid !== macro_pid);
300+
301+
let oldTask: TaskEntry | undefined
302+
queryClient.setQueryData(['instance', uuid, 'taskList'], (oldData: TaskEntry[] | undefined): TaskEntry[] | undefined => {
303+
if (oldData === undefined) {
304+
return undefined;
310305
}
311-
);
306+
return oldData.filter((task) => {
307+
const shouldKeep = task.pid !== macro_pid;
308+
if (!shouldKeep) {
309+
oldTask = task;
310+
}
311+
312+
return shouldKeep
313+
});
314+
})
315+
316+
queryClient.setQueryData(['instance', uuid, 'historyList'], (oldData: HistoryEntry[] | undefined): HistoryEntry[] | undefined => {
317+
if (oldTask === undefined) {
318+
return oldData;
319+
}
320+
const newHistory: HistoryEntry = {
321+
task: oldTask,
322+
exit_status,
323+
}
324+
325+
if (oldData === undefined) {
326+
return [newHistory];
327+
}
328+
329+
return [newHistory, ...oldData];
330+
})
312331
dispatch({
313332
title: `Macro ${macro_pid} stopped on ${uuid} with status ${exit_status.type}`,
314333
event,
@@ -331,21 +350,21 @@ export const useEventStream = () => {
331350
inner,
332351
otherwise(
333352
{
334-
ProgressionEnd: ({ inner, message }) => {
353+
ProgressionEnd: ({inner, message}) => {
335354
if (!inner) return;
336355
match(
337356
inner,
338357
otherwise(
339358
{
340359
InstanceCreation: (instance_info) =>
341360
addInstance(instance_info, queryClient),
342-
InstanceDelete: ({ instance_uuid: uuid }) =>
361+
InstanceDelete: ({instance_uuid: uuid}) =>
343362
deleteInstance(uuid, queryClient),
344363
FSOperationCompleted: ({
345-
instance_uuid,
346-
success,
347-
message,
348-
}) => {
364+
instance_uuid,
365+
success,
366+
message,
367+
}) => {
349368
if (success) {
350369
toast.success(message);
351370
} else {
@@ -359,11 +378,12 @@ export const useEventStream = () => {
359378
},
360379
},
361380
// eslint-disable-next-line @typescript-eslint/no-empty-function
362-
(_) => {}
381+
(_) => {
382+
}
363383
)
364384
);
365385
},
366-
ProgressionStart: ({ inner }) => {
386+
ProgressionStart: ({inner}) => {
367387
if (!inner) return;
368388
match(
369389
inner,
@@ -373,18 +393,20 @@ export const useEventStream = () => {
373393
// deleteInstance(uuid, queryClient),
374394
},
375395
// eslint-disable-next-line @typescript-eslint/no-empty-function
376-
(_) => {}
396+
(_) => {
397+
}
377398
)
378399
);
379400
},
380401
},
381402
// eslint-disable-next-line @typescript-eslint/no-empty-function
382-
(_) => {}
403+
(_) => {
404+
}
383405
)
384406
);
385407
}
386408
},
387-
FSEvent: ({ operation, target }) => {
409+
FSEvent: ({operation, target}) => {
388410
// console.log(`FS ${operation} on ${target.path}`);
389411
// match(target, {
390412
// File: ({ path }) => {

0 commit comments

Comments
 (0)