Hi wOxxOm, thank you for your answer.
I see your code is similar\same to what you had given as an answer in this
thread on SO I don't think I understand very well the purpose of that code or perhaps the correlation with my problem.
If I understood well, your code allows, against the preventive import of all the scripts that we expect to use in the future inside the
self.oninstall event handler,
to be able to import the necessary scripts at a later time through a simple exchange of messages.
When the SW will go to sleep after installing or upgrading, all scripts "declared" inside the self.oninstall event handler will be unloaded
and, when the right message will land to the chrome.runtime.onMessage handler will be imported only the scripts needed in that precise context.
This message can only come from a script external to the SW, such as an extension page.
Did I understand it right so far?I added to your code a chrome.action.onClicked handler which opens a tab which sends a message back to the SW and I was able to verify that this mechanism works fine.
I saved that SO thread in my favorites because I guess it will come in handy in the future (you're a genius).
I only changed "(toRun.length)" to "(toRun.size)" since "length" returns undefined with Set
Now let's get back to my problem.
Maybe I forgot to make a premise (but I think you've already guessed it).
My goal is to be able to use a handy object variable inside the scripts I've imported with "importScripts"
and I need to use this object in the
main thread (so it must be available immediately).
An alternative could be to move the self-invoking function to the main thread of the script I'm importing (/script/main.js).
I fixed things a bit like this, but I'm not very satisfied with it:
/*----------------------------------------------------------*/
/*worker_wrappwer*/
var mainProm, mainPromBusy;
try {
mainProm = new Promise((ok, ko) => {
var arrProm = [];
arrProm.push(chrome.storage.session.get());
arrProm.push(chrome.storage.local.get());
arrProm.push(chrome.storage.sync.get());
mainPromBusy = Promise.all(arrProm)
.then(v => {
ok({...v[0], ...v[1], ...v[2] /*, 'fooBar': 123*/ });
mainPromBusy = null
})
.catch(e => ko(e))
});
importScripts('script/main.js')
} catch (e) {
console.log(e)
}
/*script/main.js*/
(async _ => {
if (mainPromBusy != null)
mergedStores = await mainProm;
console.log(mergedStores)
})()
/*----------------------------------------------------------*/
Lastly, could you please elaborate on your explanation why my first attempt worked while the others didn't?
I don't think I understood correctly.
Thanks you and...
best wishes for the new year