Timothy J. Baek 10 ヶ月 前
コミット
a82154de3f
1 ファイル変更7 行追加2 行削除
  1. 7 2
      src/lib/components/chat/Chat.svelte

+ 7 - 2
src/lib/components/chat/Chat.svelte

@@ -127,7 +127,7 @@
 	}
 
 	onMount(async () => {
-		window.addEventListener('message', async (event) => {
+		const onMessageHandler = async (event) => {
 			if (event.origin === window.origin) {
 				// Replace with your iframe's origin
 				console.log('Message received from iframe:', event.data);
@@ -160,7 +160,8 @@
 					}
 				}
 			}
-		});
+		};
+		window.addEventListener('message', onMessageHandler);
 
 		if (!$chatId) {
 			chatId.subscribe(async (value) => {
@@ -173,6 +174,10 @@
 				await goto('/');
 			}
 		}
+
+		return () => {
+			window.removeEventListener('message', onMessageHandler);
+		};
 	});
 
 	//////////////////////////