From 0cffb09e249832291be73be039dc1b9bb38115f9 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Tue, 30 Apr 2019 21:50:46 +0900 Subject: Types on src/share --- src/shared/messages.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src/shared/messages.ts') diff --git a/src/shared/messages.ts b/src/shared/messages.ts index ddf3368..2bc12d8 100644 --- a/src/shared/messages.ts +++ b/src/shared/messages.ts @@ -1,5 +1,8 @@ -const onWebMessage = (listener) => { - window.addEventListener('message', (event) => { +type WebMessageSender = Window | MessagePort | ServiceWorker | null; +type WebMessageListener = (msg: any, sender: WebMessageSender | null) => void; + +const onWebMessage = (listener: WebMessageListener) => { + window.addEventListener('message', (event: MessageEvent) => { let sender = event.source; let message = null; try { @@ -12,11 +15,15 @@ const onWebMessage = (listener) => { }); }; -const onBackgroundMessage = (listener) => { +const onBackgroundMessage = ( + listener: (msg: any, sender: browser.runtime.MessageSender, +) => void) => { browser.runtime.onMessage.addListener(listener); }; -const onMessage = (listener) => { +const onMessage = ( + listener: (msg: any, sender: WebMessageSender | browser.runtime.MessageSender, +) => void) => { onWebMessage(listener); onBackgroundMessage(listener); }; -- cgit v1.2.3