This commit is contained in:
Younes Aassila 2024-03-06 19:54:15 +01:00
parent 812a5d2d3b
commit b57c5e96eb
3 changed files with 10 additions and 3 deletions

View File

@ -93,6 +93,9 @@ window.Worker = class Worker extends window.Worker {
new Blob([wrapperScript], { type: "text/javascript" })
);
super(wrapperScriptURL, options);
URL.revokeObjectURL(newScriptURL);
URL.revokeObjectURL(wrapperScriptURL);
pageState.twitchWorker = this;
this.addEventListener("message", event => {
if (
event.data?.type === MessageType.ContentScriptMessage ||
@ -101,7 +104,6 @@ window.Worker = class Worker extends window.Worker {
window.postMessage(event.data);
}
});
pageState.twitchWorker = this;
}
};

View File

@ -40,6 +40,7 @@ async function sendMessageAndWaitForResponse(
const message = event.data?.message;
if (!message) return;
if (message.type === responseMessageType) {
self.removeEventListener("message", listener);
resolve(message);
}
};

View File

@ -10,7 +10,10 @@ class Store<T extends Record<string | symbol, any>> {
private readonly _areaName: StorageAreaName;
private readonly _getDefaultState: () => T;
private _state: T; // Raw state
private _listenersByEvent: Record<string, Function[]> = {};
private _listenersByEvent: Record<EventType, Function[]> = {
load: [],
change: [],
};
readyState: ReadyState = "loading";
state: T; // Proxy state
@ -18,12 +21,13 @@ class Store<T extends Record<string | symbol, any>> {
constructor(areaName: StorageAreaName, getDefaultState: () => T) {
this._areaName = areaName;
this._getDefaultState = getDefaultState;
// Temporary state until init() is called to satisfy TypeScript.
// Temporary state to satisfy TypeScript until init() is called.
this._state = this._getDefaultState();
this.state = this._state;
this._init().then(() => {
this.readyState = "complete";
this.dispatchEvent("load");
this._listenersByEvent["load"] = []; // Remove all load listeners.
});
browser.storage.onChanged.addListener((changes, area) => {
if (area !== this._areaName) return;