Merge branch '19295-reused' refs #19295
[arvados-workbench2.git] / src / websocket / websocket.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { RootStore } from 'store/store';
6 import { AuthService } from 'services/auth-service/auth-service';
7 import { Config } from 'common/config';
8 import { WebSocketService } from './websocket-service';
9 import { ResourceEventMessage } from './resource-event-message';
10 import { ResourceKind } from 'models/resource';
11 import { loadProcess } from 'store/processes/processes-actions';
12 import { LogEventType } from 'models/log';
13 import { addProcessLogsPanelItem } from 'store/process-logs-panel/process-logs-panel-actions';
14 import { subprocessPanelActions } from "store/subprocess-panel/subprocess-panel-actions";
15 import { projectPanelActions } from "store/project-panel/project-panel-action";
16 import { getProjectPanelCurrentUuid } from 'store/project-panel/project-panel-action';
17 import { allProcessesPanelActions } from 'store/all-processes-panel/all-processes-panel-action';
18 import { loadCollection } from 'store/workbench/workbench-actions';
19
20 export const initWebSocket = (config: Config, authService: AuthService, store: RootStore) => {
21     if (config.websocketUrl) {
22         const webSocketService = new WebSocketService(config.websocketUrl, authService);
23         webSocketService.setMessageListener(messageListener(store));
24         webSocketService.connect();
25     } else {
26         console.warn("WARNING: Websocket ExternalURL is not set on the cluster config.");
27     }
28 };
29
30 const messageListener = (store: RootStore) => (message: ResourceEventMessage) => {
31     if (message.eventType === LogEventType.CREATE || message.eventType === LogEventType.UPDATE) {
32         switch (message.objectKind) {
33             case ResourceKind.COLLECTION:
34                 const currentCollection = store.getState().collectionPanel.item;
35                 if (currentCollection && currentCollection.uuid === message.objectUuid) {
36                     store.dispatch(loadCollection(message.objectUuid));
37                 }
38                 return;
39             case ResourceKind.CONTAINER_REQUEST:
40                 if (store.getState().processPanel.containerRequestUuid === message.objectUuid) {
41                     store.dispatch(loadProcess(message.objectUuid));
42                 }
43             // fall through, this will happen for container requests as well.
44             case ResourceKind.CONTAINER:
45                 store.dispatch(subprocessPanelActions.REQUEST_ITEMS());
46                 store.dispatch(allProcessesPanelActions.REQUEST_ITEMS());
47                 if (message.objectOwnerUuid === getProjectPanelCurrentUuid(store.getState())) {
48                     store.dispatch(projectPanelActions.REQUEST_ITEMS());
49                 }
50                 return;
51             default:
52                 return;
53         }
54     } else {
55         return store.dispatch(addProcessLogsPanelItem(message as ResourceEventMessage<{ text: string }>));
56     }
57 };