serversideup / webext-bridge

💬 Messaging in Web Extensions made easy. Batteries included.
https://serversideup.net/open-source/webext-bridge
MIT License
553 stars 50 forks source link

Add timeout for sendMessage #53

Open noamloewenstern opened 1 year ago

noamloewenstern commented 1 year ago

I would find it useful to add an option for timeout waiting for response, when calling "sendMessage". Something like this:

const resp = await sendMessage('messageID', { msgData: 'data' }, `content-script@123`, { timeout: 3000 })
zikaari commented 1 year ago

That's a good idea. While I don't have the resources at this time to implement it in the library, this should work as quick-workaround:

âš  This code snippet has not been tested

import pTimeout from 'p-timeout'

try {
  const resp = await pTimeout(
    sendMessage('messageID', { msgData: 'data' }, `content-script@123`),
    { milliseconds: 3000 }
  )
} catch (err) {
  // [TimeoutError: Promise timed out after 3000 milliseconds]
}