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' ;
1111import 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' ;
17- import { TaskEntry } from " ../bindings/TaskEntry" ;
18- import { getTasks } from " ../utils/apis" ;
19- import { HistoryEntry } from " ../bindings/HistoryEntry" ;
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' ;
2020
2121/**
2222 * does not return anything, call this for the side effect of subscribing to the event stream
2323 * information will be available in the query cache of the respective query cache
2424 */
2525export const useEventStream = ( ) => {
2626 const queryClient = useQueryClient ( ) ;
27- const { dispatch, ongoingDispatch} = useContext ( NotificationContext ) ;
27+ const { dispatch, ongoingDispatch } = useContext ( NotificationContext ) ;
2828 const selfUid = useUid ( ) ;
29- const { token, core, setCoreConnectionStatus, setToken} =
29+ const { token, core, setCoreConnectionStatus, setToken } =
3030 useContext ( LodestoneContext ) ;
3131 const socket = `${ core . address } :${ core . port } ` ;
3232 const wsRef = useRef < WebSocket | null > ( null ) ;
@@ -49,7 +49,7 @@ export const useEventStream = () => {
4949 const updateInstanceState = useCallback (
5050 ( uuid : string , state : InstanceState ) => {
5151 updateInstance ( uuid , queryClient , ( oldInfo ) => {
52- return { ...oldInfo , state} ;
52+ return { ...oldInfo , state } ;
5353 } ) ;
5454 } ,
5555 [ queryClient ]
@@ -94,16 +94,16 @@ export const useEventStream = () => {
9494
9595 const handleEvent = useCallback (
9696 ( event : ClientEvent , fresh : boolean ) => {
97- const { event_inner, snowflake} = event ;
97+ const { event_inner, snowflake } = event ;
9898
9999 match ( event_inner , {
100100 InstanceEvent : ( {
101- instance_event_inner : event_inner ,
102- instance_uuid : uuid ,
103- instance_name : name ,
104- } ) =>
101+ instance_event_inner : event_inner ,
102+ instance_uuid : uuid ,
103+ instance_name : name ,
104+ } ) =>
105105 match ( event_inner , {
106- StateTransition : ( { to } ) => {
106+ StateTransition : ( { to } ) => {
107107 if ( fresh ) updateInstanceState ( uuid , to ) ;
108108 dispatch ( {
109109 title : `Instance ${ name } ${
@@ -140,16 +140,16 @@ export const useEventStream = () => {
140140 fresh,
141141 } ) ;
142142 } ,
143- InstanceInput : ( { message} ) => {
143+ InstanceInput : ( { message } ) => {
144144 console . log ( `Got input on ${ name } : ${ message } ` ) ;
145145 } ,
146- InstanceOutput : ( { message} ) => {
146+ InstanceOutput : ( { message } ) => {
147147 console . log ( `Got output on ${ name } : ${ message } ` ) ;
148148 } ,
149- SystemMessage : ( { message} ) => {
149+ SystemMessage : ( { message } ) => {
150150 console . log ( `Got system message on ${ name } : ${ message } ` ) ;
151151 } ,
152- PlayerChange : ( { player_list, players_joined, players_left} ) => {
152+ PlayerChange : ( { player_list, players_joined, players_left } ) => {
153153 console . log ( `Got player change on ${ name } : ${ player_list } ` ) ;
154154 console . log ( `${ players_joined } joined ${ name } ` ) ;
155155 console . log ( `${ players_left } left ${ name } ` ) ;
@@ -181,7 +181,7 @@ export const useEventStream = () => {
181181 fresh,
182182 } ) ;
183183 } ,
184- PlayerMessage : ( { player, player_message} ) => {
184+ PlayerMessage : ( { player, player_message } ) => {
185185 console . log ( `${ player } said ${ player_message } on ${ name } ` ) ;
186186 dispatch ( {
187187 title : `${ player } said ${ player_message } on ${ name } ` ,
@@ -191,7 +191,7 @@ export const useEventStream = () => {
191191 } ) ;
192192 } ,
193193 } ) ,
194- UserEvent : ( { user_id : uid , user_event_inner : event_inner } ) =>
194+ UserEvent : ( { user_id : uid , user_event_inner : event_inner } ) =>
195195 match ( event_inner , {
196196 UserCreated : ( ) => {
197197 console . log ( `User ${ uid } created` ) ;
@@ -214,7 +214,7 @@ export const useEventStream = () => {
214214 [ 'user' , 'list' ] ,
215215 ( oldList : { [ uid : string ] : PublicUser } | undefined ) => {
216216 if ( ! oldList ) return oldList ;
217- const newList = { ...oldList } ;
217+ const newList = { ...oldList } ;
218218 delete newList [ uid ] ;
219219 return newList ;
220220 }
@@ -242,7 +242,7 @@ export const useEventStream = () => {
242242 // type: 'add',
243243 // });
244244 } ,
245- PermissionChanged : ( { new_permissions} ) => {
245+ PermissionChanged : ( { new_permissions } ) => {
246246 if ( fresh ) {
247247 if ( uid === selfUid ) {
248248 updatePermission ( new_permissions ) ;
@@ -255,7 +255,7 @@ export const useEventStream = () => {
255255 ...oldList [ uid ] ,
256256 permissions : new_permissions ,
257257 } ;
258- const newList = { ...oldList } ;
258+ const newList = { ...oldList } ;
259259 newList [ uid ] = newUser ;
260260 return newList ;
261261 }
@@ -269,14 +269,14 @@ export const useEventStream = () => {
269269 } ,
270270 } ) ,
271271 MacroEvent : ( {
272- instance_uuid : uuid ,
273- macro_pid,
274- macro_event_inner : event_inner
275- } ) =>
272+ instance_uuid : uuid ,
273+ macro_pid,
274+ macro_event_inner : event_inner ,
275+ } ) =>
276276 match ( event_inner , {
277277 Started : ( ) => {
278278 console . log ( `Macro ${ macro_pid } started on ${ uuid } ` ) ;
279- queryClient . invalidateQueries ( [ 'instance' , uuid , 'taskList' ] ) // need to invalidate the query to get the new task list
279+ queryClient . invalidateQueries ( [ 'instance' , uuid , 'taskList' ] ) ; // need to invalidate the query to get the new task list
280280 dispatch ( {
281281 title : `Macro ${ macro_pid } started on ${ uuid } ` ,
282282 event,
@@ -293,41 +293,49 @@ export const useEventStream = () => {
293293 fresh,
294294 } ) ;
295295 } ,
296- Stopped : ( { exit_status} ) => {
296+ Stopped : ( { exit_status } ) => {
297297 console . log (
298298 `Macro ${ macro_pid } stopped on ${ uuid } with status ${ exit_status . type } `
299299 ) ;
300300
301- let oldTask : TaskEntry | undefined
302- queryClient . setQueryData ( [ 'instance' , uuid , 'taskList' ] , ( oldData : TaskEntry [ ] | undefined ) : TaskEntry [ ] | undefined => {
303- if ( oldData === undefined ) {
304- return undefined ;
305- }
306- return oldData . filter ( ( task ) => {
307- const shouldKeep = task . pid !== macro_pid ;
308- if ( ! shouldKeep ) {
309- oldTask = task ;
301+ let oldTask : TaskEntry | undefined ;
302+ queryClient . setQueryData (
303+ [ 'instance' , uuid , 'taskList' ] ,
304+ ( oldData : TaskEntry [ ] | undefined ) : TaskEntry [ ] | undefined => {
305+ if ( oldData === undefined ) {
306+ return undefined ;
310307 }
308+ return oldData . filter ( ( task ) => {
309+ const shouldKeep = task . pid !== macro_pid ;
310+ if ( ! shouldKeep ) {
311+ oldTask = task ;
312+ }
311313
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,
314+ return shouldKeep ;
315+ } ) ;
323316 }
317+ ) ;
324318
325- if ( oldData === undefined ) {
326- return [ newHistory ] ;
327- }
319+ queryClient . setQueryData (
320+ [ 'instance' , uuid , 'historyList' ] ,
321+ (
322+ oldData : HistoryEntry [ ] | undefined
323+ ) : HistoryEntry [ ] | undefined => {
324+ if ( oldTask === undefined ) {
325+ return oldData ;
326+ }
327+ const newHistory : HistoryEntry = {
328+ task : oldTask ,
329+ exit_status,
330+ } ;
331+
332+ if ( oldData === undefined ) {
333+ return [ newHistory ] ;
334+ }
328335
329- return [ newHistory , ...oldData ] ;
330- } )
336+ return [ newHistory , ...oldData ] ;
337+ }
338+ ) ;
331339 dispatch ( {
332340 title : `Macro ${ macro_pid } stopped on ${ uuid } with status ${ exit_status . type } ` ,
333341 event,
@@ -350,21 +358,21 @@ export const useEventStream = () => {
350358 inner ,
351359 otherwise (
352360 {
353- ProgressionEnd : ( { inner, message} ) => {
361+ ProgressionEnd : ( { inner, message } ) => {
354362 if ( ! inner ) return ;
355363 match (
356364 inner ,
357365 otherwise (
358366 {
359367 InstanceCreation : ( instance_info ) =>
360368 addInstance ( instance_info , queryClient ) ,
361- InstanceDelete : ( { instance_uuid : uuid } ) =>
369+ InstanceDelete : ( { instance_uuid : uuid } ) =>
362370 deleteInstance ( uuid , queryClient ) ,
363371 FSOperationCompleted : ( {
364- instance_uuid,
365- success,
366- message,
367- } ) => {
372+ instance_uuid,
373+ success,
374+ message,
375+ } ) => {
368376 if ( success ) {
369377 toast . success ( message ) ;
370378 } else {
@@ -378,12 +386,11 @@ export const useEventStream = () => {
378386 } ,
379387 } ,
380388 // eslint-disable-next-line @typescript-eslint/no-empty-function
381- ( _ ) => {
382- }
389+ ( _ ) => { }
383390 )
384391 ) ;
385392 } ,
386- ProgressionStart : ( { inner} ) => {
393+ ProgressionStart : ( { inner } ) => {
387394 if ( ! inner ) return ;
388395 match (
389396 inner ,
@@ -393,20 +400,18 @@ export const useEventStream = () => {
393400 // deleteInstance(uuid, queryClient),
394401 } ,
395402 // eslint-disable-next-line @typescript-eslint/no-empty-function
396- ( _ ) => {
397- }
403+ ( _ ) => { }
398404 )
399405 ) ;
400406 } ,
401407 } ,
402408 // eslint-disable-next-line @typescript-eslint/no-empty-function
403- ( _ ) => {
404- }
409+ ( _ ) => { }
405410 )
406411 ) ;
407412 }
408413 } ,
409- FSEvent : ( { operation, target} ) => {
414+ FSEvent : ( { operation, target } ) => {
410415 // console.log(`FS ${operation} on ${target.path}`);
411416 // match(target, {
412417 // File: ({ path }) => {
0 commit comments