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 if (this.isRequest(messageEvent
.data
) === true) {
35 const [uuid
, responsePayload
] = this.validateMessageEvent(messageEvent
)
36 .data
as BroadcastChannelResponse
;
37 if (this.responses
.has(uuid
) === false) {
38 this.responses
.set(uuid
, {
39 responsesExpected
: this.uiService
.getBroadcastChannelExpectedResponses(uuid
),
41 responses
: [responsePayload
],
44 this.responses
.get(uuid
)?.responsesReceived
<= this.responses
.get(uuid
)?.responsesExpected
46 this.responses
.get(uuid
).responsesReceived
++;
47 this.responses
.get(uuid
).responses
.push(responsePayload
);
50 this.responses
.get(uuid
)?.responsesReceived
=== this.responses
.get(uuid
)?.responsesExpected
52 this.uiService
.sendResponse(uuid
, this.buildResponsePayload(uuid
));
53 this.responses
.delete(uuid
);
54 this.uiService
.deleteBroadcastChannelRequest(uuid
);
58 private buildResponsePayload(uuid
: string): ResponsePayload
{
59 const responsesStatus
= this.responses
61 ?.responses
.every((response
) => response
.status === ResponseStatus
.SUCCESS
)
62 ? ResponseStatus
.SUCCESS
63 : ResponseStatus
.FAILURE
;
65 status: responsesStatus
,
66 hashIdsSucceeded
: this.responses
68 ?.responses
.map(({ status, hashId
}) => {
69 if (status === ResponseStatus
.SUCCESS
) {
73 .filter((hashId
) => hashId
!== undefined),
74 ...(responsesStatus
=== ResponseStatus
.FAILURE
&& {
75 hashIdsFailed
: this.responses
77 ?.responses
.map(({ status, hashId
}) => {
78 if (status === ResponseStatus
.FAILURE
) {
82 .filter((hashId
) => hashId
!== undefined),
84 ...(responsesStatus
=== ResponseStatus
.FAILURE
&& {
85 responsesFailed
: this.responses
87 ?.responses
.map((response
) => {
88 if (response
.status === ResponseStatus
.FAILURE
) {
92 .filter((response
) => response
!== undefined),
97 private messageErrorHandler(messageEvent
: MessageEvent
): void {
99 `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`,