[bug] make MessageBus more reliable
This commit is contained in:
parent
b7f725bcfd
commit
97e86308ce
@ -12,9 +12,6 @@ function Iframe({ showIframe = false }: IframeProps) {
|
|||||||
if (iframeRef.current) {
|
if (iframeRef.current) {
|
||||||
IframeReference.setIframe(iframeRef.current);
|
IframeReference.setIframe(iframeRef.current);
|
||||||
}
|
}
|
||||||
return () => {
|
|
||||||
IframeReference.setIframe(null);
|
|
||||||
};
|
|
||||||
}, [iframeRef.current]);
|
}, [iframeRef.current]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -5,6 +5,11 @@ export default class IframeReference {
|
|||||||
private constructor() { }
|
private constructor() { }
|
||||||
|
|
||||||
public static setTargetOrigin(targetOrigin: string): void {
|
public static setTargetOrigin(targetOrigin: string): void {
|
||||||
|
if (this.targetOrigin) {
|
||||||
|
console.debug("targetOrigin is already set");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new URL(targetOrigin);
|
new URL(targetOrigin);
|
||||||
this.targetOrigin = targetOrigin;
|
this.targetOrigin = targetOrigin;
|
||||||
|
@ -759,17 +759,15 @@ export default class MessageBus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private sendMessage(message: any): void {
|
private sendMessage(message: any): void {
|
||||||
const targetOrigin = IframeReference.getTargetOrigin();
|
this.isReady().then(() => {
|
||||||
if (!targetOrigin) {
|
try {
|
||||||
console.error('[MessageBus] sendMessage: targetOrigin not found');
|
const targetOrigin = IframeReference.getTargetOrigin();
|
||||||
return;
|
const iframe = IframeReference.getIframe();
|
||||||
}
|
iframe.contentWindow?.postMessage(message, targetOrigin);
|
||||||
const iframe = IframeReference.getIframe();
|
} catch (error) {
|
||||||
if (!iframe) {
|
console.error('[MessageBus] sendMessage: error', error);
|
||||||
console.error('[MessageBus] sendMessage: iframe not found');
|
}
|
||||||
return;
|
}).catch(console.error);
|
||||||
}
|
|
||||||
iframe.contentWindow?.postMessage(message, targetOrigin);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleMessage(event: MessageEvent): void {
|
private handleMessage(event: MessageEvent): void {
|
||||||
@ -777,30 +775,18 @@ export default class MessageBus {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const iframe = IframeReference.getIframe();
|
try {
|
||||||
if (!iframe) {
|
const targetOrigin = IframeReference.getTargetOrigin();
|
||||||
console.error('[MessageBus] handleMessage: iframe not found');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event.source !== iframe.contentWindow) {
|
if (event.origin !== targetOrigin) {
|
||||||
console.error('[MessageBus] handleMessage: source not match');
|
throw new Error(`origin don't match: expected ${targetOrigin}, got ${event.origin} with type ${event.data.type}`);
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const targetOrigin = IframeReference.getTargetOrigin();
|
if (!event.data || typeof event.data !== 'object') {
|
||||||
if (!targetOrigin) {
|
throw new Error('data not found');
|
||||||
console.error('[MessageBus] handleMessage: targetOrigin not found');
|
}
|
||||||
return;
|
} catch (error) {
|
||||||
}
|
console.error('[MessageBus] handleMessage:', error);
|
||||||
|
|
||||||
if (event.origin !== targetOrigin) {
|
|
||||||
console.error('[MessageBus] handleMessage: origin not match');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!event.data || typeof event.data !== 'object') {
|
|
||||||
console.error('[MessageBus] handleMessage: data not found');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user