aklinker1 / webext-core

Collection of essential libraries and tools for building web extensions
https://webext-core.aklinker1.io
MIT License
96 stars 11 forks source link

Messaging supports removeListener API #58

Closed molvqingtai closed 3 months ago

molvqingtai commented 3 months ago

When using onMessage, need to support removing the listener but not all of it, especially when using react StrictMode.

useEffect(() => {
  onMessage('custom-event', () => {
    // do some thing...
  })

  return () => {
    // removeAllListeners()
    removeListener('custom-event'')
  }
})
aklinker1 commented 3 months ago

onMessage returns a function that, when called, removes the listener.

useEffect(() => {
  const removeListener = onMessage('custom-event', () => {
    // do some thing...
  })

  return () => {
    removeListener('custom-event'')
  }
})