refs #14615-placeholder-message-displayed-instead-of-inputs-link
[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 { loadContainers } from '~/store/processes/processes-actions';
13 import { LogEventType } from '~/models/log';
14 import { addProcessLogsPanelItem } from '../store/process-logs-panel/process-logs-panel-actions';
15 import { FilterBuilder } from "~/services/api/filter-builder";
16
17 export const initWebSocket = (config: Config, authService: AuthService, store: RootStore) => {
18     const webSocketService = new WebSocketService(config.websocketUrl, authService);
19     webSocketService.setMessageListener(messageListener(store));
20     webSocketService.connect();
21 };
22
23 const messageListener = (store: RootStore) => (message: ResourceEventMessage) => {
24     if (message.eventType === LogEventType.CREATE || message.eventType === LogEventType.UPDATE) {
25         switch (message.objectKind) {
26             case ResourceKind.CONTAINER_REQUEST:
27                 return store.dispatch(loadProcess(message.objectUuid));
28             case ResourceKind.CONTAINER:
29                 return store.dispatch(loadContainers(
30                     new FilterBuilder().addIn('uuid', [message.objectUuid]).getFilters()
31                 ));
32             default:
33                 return;
34         }
35     } else {
36         return store.dispatch(addProcessLogsPanelItem(message as ResourceEventMessage<{text: string}>));
37     }
38 };