In MV3, extension service workers can be shut down when Chrome feels like it, it is advised not to use global variables.
The documentation only has one small section about this and I have to make so many assumptions, so I want to test myself, to see what exactly would happen. Is there any reliable way to force shut down an extension the same way Chrome would?
I tried "terminate" in chrome://inspect/#service-workers but when the extension wakes up (due to some events?), the whole service worker script is re-run. Is that what's supposed to happen? I imagine that when the extension wakes up, only the event listeners (corresponding to the events that woke up the extension) will run, and the properties of the class will be undefined.
Below is how my worker script is written:
class X {
constructor() {
this.p = { a: 1, b: 2 };
}
init() {
chrome.webNavigation.onCompleted.addListener(this.onPageLoad.bind(this));
}
onPageLoad() {
console.log(this.p.a, this.p.b);
this.p.a += 1;
this.p.b += 2;
}
}
new X().init();
I want to test to know, after my extension is shut down and restarted:
- Will `init` rerun?
- What will happen to `this.p`? Will it be `undefined` when `onPageLoad` is run?
Is it possible to simulate the scenario? If not, what is the known effect in theory?
Any lead is appreciated. Thanks in advance!