Skip to content

Commit

Permalink
scriptio_toolkit.register, scriptio_toolkit.wait
Browse files Browse the repository at this point in the history
  • Loading branch information
PRO-2684 committed Mar 25, 2024
1 parent bcdc6b6 commit 1f3634f
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const scriptDataAttr = "data-scriptio-script";
const configDataAttr = "data-scriptio-config";
const switchDataAttr = "data-scriptio-switch";
const eventName = "scriptio-toggle";
const toolkitEventName = "scriptio-toolkit";
const $ = document.querySelector.bind(document);
// Normalized plugin path
const pluginPath = LiteLoader.plugins.scriptio.path.plugin.replace(":\\", "://").replaceAll("\\", "/");
Expand All @@ -17,6 +18,37 @@ const scriptio_toolkit = {
toggleFunc(true);
}
},
register: (tool, value) => { // Register a tool
if (tool in scriptio_toolkit) {
return false;
}
scriptio_toolkit[tool] = value;
window.dispatchEvent(new CustomEvent(toolkitEventName, { detail: tool }));
return true;
},
wait: (tool, timeout = 5000) => { // Wait for a tool to be registered
return new Promise((resolve, reject) => {
if (tool in scriptio_toolkit) {
return resolve(scriptio_toolkit[tool]);
}
const timer = setTimeout(() => {
window.removeEventListener(toolkitEventName, listener);
if (tool in scriptio_toolkit) {
resolve(scriptio_toolkit[tool]);
} else {
reject(new Error("Timeout waiting for:", tool));
}
}, timeout);
function listener(event) {
if (event.detail === tool) {
clearTimeout(timer);
log("Toolkit event received:", tool);
resolve(scriptio_toolkit[tool]);
}
}
window.addEventListener(toolkitEventName, listener, { once: true });
});
}
};
Object.defineProperty(window, "scriptio_toolkit", {
value: scriptio_toolkit,
Expand Down

0 comments on commit 1f3634f

Please sign in to comment.