MitsuhaKitsune / vuex-webextensions

A Vuex plugin to share store through webextensions components
MIT License
83 stars 30 forks source link

Race condition in contentScript.js #8

Closed KBoyarchuk closed 6 years ago

KBoyarchuk commented 6 years ago

In contentScripts.js file there may be a problem. When we first open the extension popup, function sendMutation called before onMessage listener. In this case, when the data is written to the state, the onMessage handler is called with type STORE_INITIAL_STATE and the replaceState method overwrites the data that was previous written to the state.

As example:

extension

Stormsher commented 6 years ago

Confirm that, I have similar issue with content script...

MitsuhaKitsune commented 6 years ago

This is caused because the messaging api, it have a bit of delay, for now the workarround its delay a bit your initialization to let the plugin sync the state with the background store first and then commit your mutations.

To avoid this, I think on a "pending queue", before STORE_INITIAL_STATE queue all mutations and when the state are synced with the backgrond apply all of them.

How you see this method? Any ideas to improve it?

MitsuhaKitsune commented 6 years ago

Should be fixed with https://github.com/MitsuhaKitsune/vuex-webextensions/commit/4387b5d02e394b721e1e3726fcfbe70d0df36dee, available on npm as v1.2.3, try it and give me feedback please

KBoyarchuk commented 6 years ago

Create PR. Check it please.

MitsuhaKitsune commented 6 years ago

Finally fixed the initialization on content scripts? :)

KBoyarchuk commented 6 years ago

Yeap. At the moment everything works stably.