Skip to content

Commit

Permalink
Refactor dipatch method
Browse files Browse the repository at this point in the history
  • Loading branch information
twschiller committed Sep 21, 2024
1 parent c46fe8c commit 006640c
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions src/contentScript/stateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,25 @@ function mergeState(
}
}

/**
* Dispatch a state change event to the document.
* @see STATE_CHANGE_JS_EVENT_TYPE
*/
function dispatchStateChangeEvent(
// For now, leave off the state data because state controller in the content script uses JavaScript/DOM
// events, which is a public channel (the host site/other extensions can see the event).
detail: StateChangeEventDetail,
) {
console.debug("Dispatching statechange", detail);

document.dispatchEvent(
new CustomEvent(STATE_CHANGE_JS_EVENT_TYPE, {
detail,
bubbles: true,
}),
);
}

function dispatchStateChangeEventOnChange({
previous,
next,
Expand All @@ -167,22 +186,11 @@ function dispatchStateChangeEventOnChange({
}

if (!isEqual(previous, next)) {
// For now, leave off the event data because state controller in the content script uses JavaScript/DOM
// events, which is a public channel (the host site/other extensions can see the event).
const detail: StateChangeEventDetail = {
dispatchStateChangeEvent({
namespace,
extensionId: modComponentId,
blueprintId: modId,
};

console.debug("Dispatching statechange", detail);

document.dispatchEvent(
new CustomEvent(STATE_CHANGE_JS_EVENT_TYPE, {
detail,
bubbles: true,
}),
);
});
}
}

Expand Down Expand Up @@ -314,21 +322,10 @@ function onSessionStorageChange(
if (areaName === "session") {
for (const key of Object.keys(change)) {
if (key.startsWith(keyPrefix)) {
const modId = validateRegistryId(key.slice(keyPrefix.length));

const detail: StateChangeEventDetail = {
dispatchStateChangeEvent({
namespace: StateNamespaces.MOD,
blueprintId: modId,
};

console.debug("Dispatching statechange", detail);

document.dispatchEvent(
new CustomEvent(STATE_CHANGE_JS_EVENT_TYPE, {
detail,
bubbles: true,
}),
);
blueprintId: validateRegistryId(key.slice(keyPrefix.length)),
});
}
}
}
Expand Down

0 comments on commit 006640c

Please sign in to comment.