[bug] make MessageBus more reliable

This commit is contained in:
Sosthene 2025-07-15 21:18:44 +02:00
parent b7f725bcfd
commit bd4c9205d9

View File

@ -759,17 +759,15 @@ export default class MessageBus {
}
private sendMessage(message: any): void {
this.isReady().then(() => {
try {
const targetOrigin = IframeReference.getTargetOrigin();
if (!targetOrigin) {
console.error('[MessageBus] sendMessage: targetOrigin not found');
return;
}
const iframe = IframeReference.getIframe();
if (!iframe) {
console.error('[MessageBus] sendMessage: iframe not found');
return;
}
iframe.contentWindow?.postMessage(message, targetOrigin);
} catch (error) {
console.error('[MessageBus] sendMessage: error', error);
}
}).catch(console.error);
}
private handleMessage(event: MessageEvent): void {
@ -777,11 +775,8 @@ export default class MessageBus {
return;
}
try {
const iframe = IframeReference.getIframe();
if (!iframe) {
console.error('[MessageBus] handleMessage: iframe not found');
return;
}
if (event.source !== iframe.contentWindow) {
console.error('[MessageBus] handleMessage: source not match');
@ -789,18 +784,16 @@ export default class MessageBus {
}
const targetOrigin = IframeReference.getTargetOrigin();
if (!targetOrigin) {
console.error('[MessageBus] handleMessage: targetOrigin not found');
return;
}
if (event.origin !== targetOrigin) {
console.error('[MessageBus] handleMessage: origin not match');
return;
throw new Error('origin not match');
}
if (!event.data || typeof event.data !== 'object') {
console.error('[MessageBus] handleMessage: data not found');
throw new Error('data not found');
}
} catch (error) {
console.error('[MessageBus] handleMessage:', error);
return;
}