ResourceManager
A resource coordinator for registering and connecting scripts, stylesheets and data assets.
import { ResourceManager } from "@schukai/monster/source/dom/resourcemanager.mjs";Introduction
ResourceManager coordinates scripts, stylesheets and data resources as one managed bundle. Use it when a feature needs several assets and should register, connect and verify them through one contract.
Core Responsibilities
| Surface | What it solves |
|---|---|
addScript() | Registers a script resource with the manager. |
addStylesheet() | Registers a stylesheet resource with the manager. |
addData() | Registers a data resource that fetches and injects text content. |
connect() | Creates and appends all registered resources to the target document. |
available() | Waits until the registered resources report their availability. |
Why It Matters
Instead of scattering resource injection across unrelated modules, the manager makes the full dependency set visible and testable in one place.
Register And Connect Multiple Resources
import { ResourceManager } from "@schukai/monster/source/dom/resourcemanager.mjs";
const output = document.getElementById("manager-output");
document.getElementById("manager-run").addEventListener("click", () => {
const manager = new ResourceManager({ document });
manager
.addScript("/assets/app.js", { type: "module" })
.addStylesheet("/assets/app.css", { rel: "stylesheet" })
.addData("/assets/config.json", { type: "application/json" });
const resources = manager.getInternal("resources");
output.textContent = JSON.stringify(
{
scripts: resources.scripts.length,
stylesheets: resources.stylesheets.length,
data: resources.data.length,
},
null,
2,
);
});Connect Script Stylesheet And Data Resources From Inline Urls
import { ResourceManager } from "@schukai/monster/source/dom/resourcemanager.mjs";
const output = document.getElementById("resource-manager-bundle-output");
document.getElementById("resource-manager-bundle-run").addEventListener("click", async () => {
const manager = new ResourceManager({ document });
manager
.addStylesheet("data:text/css,body%7B--resource-manager-demo:1;%7D")
.addScript("data:text/javascript,window.__resourceManagerScriptLoaded%20%3D%20true%3B")
.addData("data:application/json,%7B%22status%22%3A%22ready%22%7D");
manager.connect();
await manager.available();
const resources = manager.getInternal("resources");
output.textContent = JSON.stringify({
stylesheets: resources.stylesheets.length,
scripts: resources.scripts.length,
data: resources.data.length,
scriptLoaded: window.__resourceManagerScriptLoaded === true,
}, null, 2);
});waiting
Register Resources In Separate Steps And Inspect The Internal Groups
import { ResourceManager } from "@schukai/monster/source/dom/resourcemanager.mjs";
document.getElementById("resource-manager-separate-run").addEventListener("click", () => {
const manager = new ResourceManager({ document });
manager.addScript("/assets/app.js", { type: "module" });
manager.addStylesheet("/assets/app.css", { rel: "stylesheet" });
manager.addData("/assets/config.json", { type: "application/json" });
const resources = manager.getInternal("resources");
document.getElementById("resource-manager-separate-output").textContent = JSON.stringify({
scriptSources: resources.scripts.map((entry) => entry.getOption("src")),
stylesheetTargets: resources.stylesheets.map((entry) => entry.getOption("href")),
dataSources: resources.data.map((entry) => entry.getOption("src")),
}, null, 2);
});waiting
Wait For Registered Resources To Report Availability
import { ResourceManager } from "@schukai/monster/source/dom/resourcemanager.mjs";
document.getElementById("resource-manager-availability-run").addEventListener("click", async () => {
const manager = new ResourceManager({ document });
manager
.addStylesheet("data:text/css,body%7B--resource-manager-availability:1;%7D")
.addScript("data:text/javascript,window.__resourceManagerAvailability%20%3D%20'ok'%3B");
manager.connect();
const result = await manager.available();
document.getElementById("resource-manager-availability-output").textContent = JSON.stringify({
resolvedEntries: result.length,
marker: window.__resourceManagerAvailability ?? "missing",
}, null, 2);
});waiting
Exported
ResourceManagerDerived from
BaseOptions
The Options listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the Base.
Properties
The Properties listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Properties from these parent classes. If you cannot find a specific Properties in this list, we recommend consulting the documentation of the Base.
Methods
The methods listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the Base.
Constructor
constructor(options)options{object}: options
Behavioral methods
addData(url,options)url{string|url}: urloptions{[object|undefined}: options
- {Monster.DOM.ResourceManager}
addScript(url,options)url{string|url}: urloptions{[object|undefined}: options
- {Monster.DOM.ResourceManager}
addStylesheet(url,options)url{string|url}: urloptions{[object|undefined}: options
- {Monster.DOM.ResourceManager}
Structural methods
getBaseURL()getOption(key)key
Other methods
available()- {Promise}
{Error}unsupported resource definition
connect()- {Monster.DOM.ResourceManager}
{Error}unsupported resource definition
internalDefaults()Events
This component does not fire any public events. It may fire events that are inherited from its parent classes.