just so much groundwork
This commit is contained in:
38
lib/resources.ts
Normal file
38
lib/resources.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
export class ResourceManager {
|
||||
private resources: Map<string, unknown> = new Map();
|
||||
private statuses: Map<string, Promise<boolean>> = new Map();
|
||||
|
||||
get<T>(name: string): T {
|
||||
if (!this.resources.has(name)) {
|
||||
throw new Error(`Resource ${name} not found`);
|
||||
}
|
||||
return this.resources.get(name) as T;
|
||||
}
|
||||
|
||||
set(name: string, value: unknown) {
|
||||
if (typeof (value as EventSource).addEventListener === "function") {
|
||||
this.statuses.set(
|
||||
name,
|
||||
new Promise((resolve) => {
|
||||
const onload = () => {
|
||||
this.resources.set(name, value);
|
||||
resolve(true);
|
||||
(value as EventSource).removeEventListener("load", onload);
|
||||
};
|
||||
(value as EventSource).addEventListener("load", onload);
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
console.warn("Resource added was not a loadable resource");
|
||||
}
|
||||
this.resources.set(name, value);
|
||||
}
|
||||
|
||||
delete(name: string) {
|
||||
this.resources.delete(name);
|
||||
}
|
||||
|
||||
ready() {
|
||||
return Promise.all(Array.from(this.statuses.values()));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user