How To Prevent/detect Race Condition Between Processing And Restoring Store Data When Waking Up An Event Page
I am using sendMessage and onMessage listener (both in background and content pages), and I am seeing some messages are getting lost. I have a few 'global' variables that I store e
Solution 1:
Well, you can't do anything about async nature of Event page processing and Chrome Storage API. And there's no "delaying until" in async JS.
Therefore, you'll need to make do with callbacks. This should work:
var globalsReady = false;
chrome.foo.onBar.addListener(handler);
functionhandler(a, b, c) {
restoreGlobals(function() {
/* Do actual handling using a, b, c */
});
// Special note for onMessage: if you are sending a reply asynchronously,// you'll need to return true; here
}
functionrestoreGlobals(callback) {
if(!globalsReady) {
chrome.storage.local.get(/*...*/, function(data) {
/* restore globals here */
globalsReady = true;
if(typeof callback == "function") callback();
});
} else {
// Already done restoringif(typeof callback == "function") callback();
}
}
restoreGlobals();
Post a Comment for "How To Prevent/detect Race Condition Between Processing And Restoring Store Data When Waking Up An Event Page"