ThemeRegistry
class exported by @mwillbanks/tuil-theme.
class
Public class exported by @mwillbanks/tuil-theme.
export class ThemeRegistry {
readonly #entries = new Map<string, ThemeRegistryEntry>();
#defaultId?: string;
register(
entry: ThemeRegistryEntry,
options: { readonly default?: boolean } = {},
): Disposable {
if (this.#entries.has(entry.theme.id)) {
throw new Error(`Theme "${entry.theme.id}" is already registered`);
}
const registered = Object.freeze({
...entry,
tags: Object.freeze([...new Set(entry.tags ?? [])]),
});
this.#entries.set(entry.theme.id, registered);
if (options.default || this.#defaultId === undefined) {
this.#defaultId = entry.theme.id;
}
return toDisposable(() => {
if (this.#entries.get(entry.theme.id) !== registered) return;
this.#entries.delete(entry.theme.id);
if (this.#defaultId === entry.theme.id) {
this.#defaultId = this.list()[0]?.theme.id;
}
});
}
get(id: string): ThemeRegistryEntry | undefined {
return this.#entries.get(id);
}
resolve(id?: string): Theme {
const selected = id ?? this.#defaultId;
const theme = selected ? this.#entries.get(selected)?.theme : undefined;
if (!theme) {
throw new Error(
selected
? `Theme "${selected}" is not registered`
: "No theme is registered",
);
}
return theme;
}
list(options: { readonly tag?: string } = {}): readonly ThemeRegistryEntry[] {
return [...this.#entries.values()]
.filter((entry) => !options.tag || entry.tags?.includes(options.tag))
.sort((left, right) => left.theme.id.localeCompare(right.theme.id));
}
}Members
| Member | Type | Required | Description | Related types |
|---|---|---|---|---|
#entries | Map<string, ThemeRegistryEntry> | Yes | The #entries member uses the Map<string, ThemeRegistryEntry> contract. | ThemeRegistryEntry |
#defaultId | string | undefined | No | The #defaultId member uses the string | undefined contract. | — |
register | (entry: ThemeRegistryEntry, options?: { readonly default?: boolean; }) => Disposable | Yes | The register member uses the (entry: ThemeRegistryEntry, options?: { readonly default?: boolean; }) => Disposable contract. | Disposable, ThemeRegistryEntry |
get | (id: string) => ThemeRegistryEntry | undefined | Yes | The get member uses the (id: string) => ThemeRegistryEntry | undefined contract. | ThemeRegistryEntry |
resolve | (id?: string) => Theme | Yes | The resolve member uses the (id?: string) => Theme contract. | Theme |
list | (options?: { readonly tag?: string; }) => readonly ThemeRegistryEntry[] | Yes | The list member uses the (options?: { readonly tag?: string; }) => readonly ThemeRegistryEntry[] contract. | ThemeRegistryEntry |
Parameters
This declaration has no public members.
Returns
This declaration does not return a value.
Throws
No thrown errors are documented for this declaration.
Related types
Source
View the secondary source reference