Skip to content

messaging: allow calling useServiceWorker() multiple times #2496

Open
@nuragic

Description

@nuragic

Quoting @gauntface in #114 (comment)

The error message does need changing. I hadn't intended that developers would call this method twice, out of interest what is the use case?

The use case is e.g. having a toggle to enable/disable push notifications.

The reason for enforcing a single call is to avoid any risk of the library being called with two different service workers.

Ok, good thing! However, I know what I'm doing in this case, so I'd like to have a mechanism to circumvent the registrationToUse != null check.

So would people prefer the ability to just allow any number of calls and block after getToken is called or would they prefer only being able to call it once and just have the error corrected to something along the lines of "useServiceWorker can only be called once and must be called before getToken()" ?

As I said, I need the possibility to call this method N times during the same app lifecycle...

Imagine this scenario:

  • the app loads from scratch, the SW is registered... the user previously enabled push notifications, so now we call 1) useServiceWorker() 2) getToken() .... etc.
  • user disable notifications from the app UI, we call deleteToken()... cool
  • user wants to enable notifications again: here we do the same thing as when the app initialise, but we get this error...

The fix could be either to remove the check and not throw, to expose a setter for registrationToUse in order to be reset, or passing a new flag to useServiceWorker() in order to enable multiple calls.

Thanks! 🙏

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions