Commit | Line | Data |
---|---|---|
78202038 JB |
1 | import type AbstractUIService from './ui-server/ui-services/AbstractUIService'; |
2 | import WorkerBroadcastChannel from './WorkerBroadcastChannel'; | |
8bfbc743 | 3 | import { type ResponsePayload, ResponseStatus } from '../types/UIProtocol'; |
0d2cec76 JB |
4 | import type { |
5 | BroadcastChannelResponse, | |
6 | BroadcastChannelResponsePayload, | |
7 | MessageEvent, | |
8 | } from '../types/WorkerBroadcastChannel'; | |
6c8f5d90 | 9 | import logger from '../utils/Logger'; |
6c8f5d90 JB |
10 | |
11 | const moduleName = 'UIServiceWorkerBroadcastChannel'; | |
12 | ||
0d2cec76 JB |
13 | type Responses = { |
14 | responsesExpected: number; | |
15 | responsesReceived: number; | |
16 | responses: BroadcastChannelResponsePayload[]; | |
17 | }; | |
18 | ||
6c8f5d90 | 19 | export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel { |
0d2cec76 | 20 | private readonly uiService: AbstractUIService; |
53870cff | 21 | private readonly responses: Map<string, Responses>; |
6c8f5d90 JB |
22 | |
23 | constructor(uiService: AbstractUIService) { | |
24 | super(); | |
25 | this.uiService = uiService; | |
26 | this.onmessage = this.responseHandler.bind(this) as (message: MessageEvent) => void; | |
27 | this.onmessageerror = this.messageErrorHandler.bind(this) as (message: MessageEvent) => void; | |
0d2cec76 | 28 | this.responses = new Map<string, Responses>(); |
6c8f5d90 JB |
29 | } |
30 | ||
31 | private responseHandler(messageEvent: MessageEvent): void { | |
5dea4c94 JB |
32 | const validatedMessageEvent = this.validateMessageEvent(messageEvent); |
33 | if (validatedMessageEvent === false) { | |
6c8f5d90 JB |
34 | return; |
35 | } | |
5dea4c94 JB |
36 | if (this.isRequest(validatedMessageEvent.data) === true) { |
37 | return; | |
38 | } | |
39 | const [uuid, responsePayload] = validatedMessageEvent.data as BroadcastChannelResponse; | |
0d2cec76 JB |
40 | if (this.responses.has(uuid) === false) { |
41 | this.responses.set(uuid, { | |
42 | responsesExpected: this.uiService.getBroadcastChannelExpectedResponses(uuid), | |
43 | responsesReceived: 1, | |
44 | responses: [responsePayload], | |
45 | }); | |
46 | } else if ( | |
6d9876e7 | 47 | this.responses.get(uuid)?.responsesReceived <= this.responses.get(uuid)?.responsesExpected |
0d2cec76 JB |
48 | ) { |
49 | this.responses.get(uuid).responsesReceived++; | |
50 | this.responses.get(uuid).responses.push(responsePayload); | |
6d9876e7 JB |
51 | } |
52 | if ( | |
53 | this.responses.get(uuid)?.responsesReceived === this.responses.get(uuid)?.responsesExpected | |
0d2cec76 | 54 | ) { |
0d2cec76 JB |
55 | this.uiService.sendResponse(uuid, this.buildResponsePayload(uuid)); |
56 | this.responses.delete(uuid); | |
57 | this.uiService.deleteBroadcastChannelRequest(uuid); | |
58 | } | |
59 | } | |
6c8f5d90 | 60 | |
0d2cec76 | 61 | private buildResponsePayload(uuid: string): ResponsePayload { |
c56450e3 JB |
62 | const responsesStatus = |
63 | this.responses | |
64 | .get(uuid) | |
65 | ?.responses.every(({ status }) => status === ResponseStatus.SUCCESS) === true | |
66 | ? ResponseStatus.SUCCESS | |
67 | : ResponseStatus.FAILURE; | |
0d2cec76 JB |
68 | return { |
69 | status: responsesStatus, | |
70 | hashIdsSucceeded: this.responses | |
71 | .get(uuid) | |
72 | ?.responses.map(({ status, hashId }) => { | |
73 | if (status === ResponseStatus.SUCCESS) { | |
74 | return hashId; | |
75 | } | |
76 | }) | |
8475b222 | 77 | .filter(hashId => hashId !== undefined), |
0d2cec76 JB |
78 | ...(responsesStatus === ResponseStatus.FAILURE && { |
79 | hashIdsFailed: this.responses | |
80 | .get(uuid) | |
81 | ?.responses.map(({ status, hashId }) => { | |
82 | if (status === ResponseStatus.FAILURE) { | |
83 | return hashId; | |
84 | } | |
85 | }) | |
8475b222 | 86 | .filter(hashId => hashId !== undefined), |
0d2cec76 | 87 | }), |
f53c88d0 JB |
88 | ...(responsesStatus === ResponseStatus.FAILURE && { |
89 | responsesFailed: this.responses | |
90 | .get(uuid) | |
8475b222 | 91 | ?.responses.map(response => { |
f53c88d0 JB |
92 | if (response.status === ResponseStatus.FAILURE) { |
93 | return response; | |
94 | } | |
95 | }) | |
8475b222 | 96 | .filter(response => response !== undefined), |
f53c88d0 | 97 | }), |
0d2cec76 | 98 | }; |
6c8f5d90 JB |
99 | } |
100 | ||
101 | private messageErrorHandler(messageEvent: MessageEvent): void { | |
102 | logger.error( | |
103 | `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`, | |
5083d31a | 104 | messageEvent |
6c8f5d90 JB |
105 | ); |
106 | } | |
107 | } |