1 import { WorkerBroadcastChannel
} from
'./WorkerBroadcastChannel';
3 type BroadcastChannelResponse
,
4 type BroadcastChannelResponsePayload
,
9 import { isNullOrUndefined
, logger
} from
'../../utils';
10 import type { AbstractUIService
} from
'../ui-server/ui-services/AbstractUIService';
12 const moduleName
= 'UIServiceWorkerBroadcastChannel';
15 responsesExpected
: number;
16 responsesReceived
: number;
17 responses
: BroadcastChannelResponsePayload
[];
20 export class UIServiceWorkerBroadcastChannel
extends WorkerBroadcastChannel
{
21 private readonly uiService
: AbstractUIService
;
22 private readonly responses
: Map
<string, Responses
>;
24 constructor(uiService
: AbstractUIService
) {
26 this.uiService
= uiService
;
27 this.onmessage
= this.responseHandler
.bind(this) as (message
: unknown
) => void;
28 this.onmessageerror
= this.messageErrorHandler
.bind(this) as (message
: unknown
) => void;
29 this.responses
= new Map
<string, Responses
>();
32 private responseHandler(messageEvent
: MessageEvent
): void {
33 const validatedMessageEvent
= this.validateMessageEvent(messageEvent
);
34 if (validatedMessageEvent
=== false) {
37 if (this.isRequest(validatedMessageEvent
.data
) === true) {
40 const [uuid
, responsePayload
] = validatedMessageEvent
.data
as BroadcastChannelResponse
;
41 if (this.responses
.has(uuid
) === false) {
42 this.responses
.set(uuid
, {
43 responsesExpected
: this.uiService
.getBroadcastChannelExpectedResponses(uuid
),
45 responses
: [responsePayload
],
48 this.responses
.get(uuid
)!.responsesReceived
<= this.responses
.get(uuid
)!.responsesExpected
50 ++this.responses
.get(uuid
)!.responsesReceived
;
51 this.responses
.get(uuid
)?.responses
.push(responsePayload
);
54 this.responses
.get(uuid
)?.responsesReceived
=== this.responses
.get(uuid
)?.responsesExpected
56 this.uiService
.sendResponse(uuid
, this.buildResponsePayload(uuid
));
57 this.responses
.delete(uuid
);
58 this.uiService
.deleteBroadcastChannelRequest(uuid
);
62 private buildResponsePayload(uuid
: string): ResponsePayload
{
63 const responsesStatus
=
66 ?.responses
.every(({ status }) => status === ResponseStatus
.SUCCESS
) === true
67 ? ResponseStatus
.SUCCESS
68 : ResponseStatus
.FAILURE
;
70 status: responsesStatus
,
71 hashIdsSucceeded
: this.responses
73 ?.responses
.filter(({ hashId
}) => !isNullOrUndefined(hashId
))
74 .map(({ status, hashId
}) => {
75 if (status === ResponseStatus
.SUCCESS
) {
79 ...(responsesStatus
=== ResponseStatus
.FAILURE
&& {
80 hashIdsFailed
: this.responses
82 ?.responses
.filter(({ hashId
}) => !isNullOrUndefined(hashId
))
83 .map(({ status, hashId
}) => {
84 if (status === ResponseStatus
.FAILURE
) {
89 ...(responsesStatus
=== ResponseStatus
.FAILURE
&& {
90 responsesFailed
: this.responses
92 ?.responses
.filter((response
) => !isNullOrUndefined(response
))
94 if (response
.status === ResponseStatus
.FAILURE
) {
97 }) as BroadcastChannelResponsePayload
[],
102 private messageErrorHandler(messageEvent
: MessageEvent
): void {
104 `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`,