32 lines
630 B
TypeScript
32 lines
630 B
TypeScript
export class ExtensionRegistry {
|
|
#values = new Map<string, unknown[]>();
|
|
|
|
add<T>(name: string, value: T): void {
|
|
const values = this.#values.get(name);
|
|
|
|
if (values) {
|
|
values.push(value);
|
|
} else {
|
|
this.#values.set(name, [value]);
|
|
}
|
|
}
|
|
|
|
get<T>(name: string): T {
|
|
const values = this.all<T>(name);
|
|
|
|
if (values.length !== 1) {
|
|
throw new Error(`"${name}" needs exactly one value, found ${values.length}.`);
|
|
}
|
|
|
|
return values[0] as T;
|
|
}
|
|
|
|
all<T>(name: string): T[] {
|
|
return [...(this.#values.get(name) ?? [])] as T[];
|
|
}
|
|
|
|
clear(): void {
|
|
this.#values.clear();
|
|
}
|
|
}
|