1 import { ResponsePayload
, ResponseStatus
} from
'../types/UIProtocol';
3 BroadcastChannelResponse
,
4 BroadcastChannelResponsePayload
,
6 } from
'../types/WorkerBroadcastChannel';
7 import logger from
'../utils/Logger';
8 import type AbstractUIService from
'./ui-server/ui-services/AbstractUIService';
9 import WorkerBroadcastChannel from
'./WorkerBroadcastChannel';
11 const moduleName
= 'UIServiceWorkerBroadcastChannel';
14 responsesExpected
: number;
15 responsesReceived
: number;
16 responses
: BroadcastChannelResponsePayload
[];
19 export default class UIServiceWorkerBroadcastChannel
extends WorkerBroadcastChannel
{
20 private readonly uiService
: AbstractUIService
;
21 private readonly responses
: Map
<string, Responses
>;
23 constructor(uiService
: AbstractUIService
) {
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;
28 this.responses
= new Map
<string, Responses
>();
31 private responseHandler(messageEvent
: MessageEvent
): void {
32 const validatedMessageEvent
= this.validateMessageEvent(messageEvent
);
33 if (validatedMessageEvent
=== false) {
36 if (this.isRequest(validatedMessageEvent
.data
) === true) {
39 const [uuid
, responsePayload
] = validatedMessageEvent
.data
as BroadcastChannelResponse
;
40 if (this.responses
.has(uuid
) === false) {
41 this.responses
.set(uuid
, {
42 responsesExpected
: this.uiService
.getBroadcastChannelExpectedResponses(uuid
),
44 responses
: [responsePayload
],
47 this.responses
.get(uuid
)?.responsesReceived
<= this.responses
.get(uuid
)?.responsesExpected
49 this.responses
.get(uuid
).responsesReceived
++;
50 this.responses
.get(uuid
).responses
.push(responsePayload
);
53 this.responses
.get(uuid
)?.responsesReceived
=== this.responses
.get(uuid
)?.responsesExpected
55 this.uiService
.sendResponse(uuid
, this.buildResponsePayload(uuid
));
56 this.responses
.delete(uuid
);
57 this.uiService
.deleteBroadcastChannelRequest(uuid
);
61 private buildResponsePayload(uuid
: string): ResponsePayload
{
62 const responsesStatus
= this.responses
64 ?.responses
.every(({ status }) => status === ResponseStatus
.SUCCESS
)
65 ? ResponseStatus
.SUCCESS
66 : ResponseStatus
.FAILURE
;
68 status: responsesStatus
,
69 hashIdsSucceeded
: this.responses
71 ?.responses
.map(({ status, hashId
}) => {
72 if (status === ResponseStatus
.SUCCESS
) {
76 .filter((hashId
) => hashId
!== undefined),
77 ...(responsesStatus
=== ResponseStatus
.FAILURE
&& {
78 hashIdsFailed
: this.responses
80 ?.responses
.map(({ status, hashId
}) => {
81 if (status === ResponseStatus
.FAILURE
) {
85 .filter((hashId
) => hashId
!== undefined),
87 ...(responsesStatus
=== ResponseStatus
.FAILURE
&& {
88 responsesFailed
: this.responses
90 ?.responses
.map((response
) => {
91 if (response
.status === ResponseStatus
.FAILURE
) {
95 .filter((response
) => response
!== undefined),
100 private messageErrorHandler(messageEvent
: MessageEvent
): void {
102 `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`,