2 type BroadcastChannelResponse
,
3 type BroadcastChannelResponsePayload
,
7 } from
'../../types/index.js'
8 import { logger
} from
'../../utils/index.js'
9 import type { AbstractUIService
} from
'../ui-server/ui-services/AbstractUIService.js'
10 import { WorkerBroadcastChannel
} from
'./WorkerBroadcastChannel.js'
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
)) {
40 const [uuid
, responsePayload
] = validatedMessageEvent
.data
as BroadcastChannelResponse
41 if (!this.responses
.has(uuid
)) {
42 this.responses
.set(uuid
, {
43 responsesExpected
: this.uiService
.getBroadcastChannelExpectedResponses(uuid
),
45 responses
: [responsePayload
]
48 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
49 this.responses
.get(uuid
)!.responsesReceived
<= this.responses
.get(uuid
)!.responsesExpected
51 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
52 ++this.responses
.get(uuid
)!.responsesReceived
53 this.responses
.get(uuid
)?.responses
.push(responsePayload
)
56 this.responses
.get(uuid
)?.responsesReceived
=== this.responses
.get(uuid
)?.responsesExpected
58 this.uiService
.sendResponse(uuid
, this.buildResponsePayload(uuid
))
59 this.responses
.delete(uuid
)
60 this.uiService
.deleteBroadcastChannelRequest(uuid
)
64 private buildResponsePayload (uuid
: string): ResponsePayload
{
65 const responsesStatus
=
68 ?.responses
.every(({ status }) => status === ResponseStatus
.SUCCESS
) === true
69 ? ResponseStatus
.SUCCESS
70 : ResponseStatus
.FAILURE
72 status: responsesStatus
,
73 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain
74 hashIdsSucceeded
: this.responses
76 ?.responses
.map(({ status, hashId
}) => {
77 if (hashId
!= null && status === ResponseStatus
.SUCCESS
) {
82 .filter(hashId
=> hashId
!= null)!,
83 ...(responsesStatus
=== ResponseStatus
.FAILURE
&& {
84 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain
85 hashIdsFailed
: this.responses
87 ?.responses
.map(({ status, hashId
}) => {
88 if (hashId
!= null && status === ResponseStatus
.FAILURE
) {
93 .filter(hashId
=> hashId
!= null)!
95 ...(responsesStatus
=== ResponseStatus
.FAILURE
&& {
96 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain
97 responsesFailed
: this.responses
99 ?.responses
.map(response
=> {
100 if (response
.status === ResponseStatus
.FAILURE
) {
105 .filter(response
=> response
!= null)!
110 private messageErrorHandler (messageEvent
: MessageEvent
): void {
112 `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`,