tshaddix / webext-redux

A set of utilities for building Redux applications in Web Extensions.
MIT License
1.22k stars 179 forks source link

Reconnect to the port if not ready upon safety message #281

Open tuttleb opened 2 years ago

tuttleb commented 2 years ago

Overview

In situations where wrapStore is happening after the Store is created we do not currently call the onConnect listener which syncs initial state. The safety handler, however, will mark the store as ready and resolve the ready promise. This means that the content script will start using the store even though it has never synced with the background. In my use case this means that the content script is using the default Store state of {}.

To resolve this, the safety handler (if the store is not ready) now triggers the Store to disconnect and then reconnect to the port. This will trigger the onConnect handler in the wrapped store and sync state as expected.

Alternatives

Reproduction Steps

These are effectively the same as https://github.com/tshaddix/webext-redux/issues/106

I'm wrapping my wrapStore in a timeout, then reloading the extension + a page that sets up a Store in a content script. Basically this guarantees that the Store is set up before wrapStore happens.

setTimeout(() => {
  wrapStore(store, {
    portName: REDUX_PORT_NAME,
  });
}, 10000);