-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the generic registry to a separate file
- Loading branch information
1 parent
1f36300
commit f2a5d82
Showing
3 changed files
with
37 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +0,0 @@ | ||
type Configuration = Record<string, any>; | ||
type Builder<T extends Configuration = Configuration> = (config: T) => () => any; | ||
|
||
export class Registry<K extends string> { | ||
private registry: Map<K, Builder> = new Map(); | ||
|
||
register(key: K, builder: Builder): void { | ||
if (this.registry.has(key)) { | ||
throw new Error(`Builder with key '${key}' is already registered`); | ||
} | ||
this.registry.set(key, builder); | ||
} | ||
|
||
has(key: K): boolean { | ||
return this.registry.has(key); | ||
} | ||
|
||
get(key: K): Builder { | ||
if (!this.has(key)) { | ||
throw new Error(`There is no builder with key '${key}' registered`); | ||
} | ||
|
||
return this.registry.get(key)!; | ||
} | ||
|
||
remove(key: K): boolean { | ||
if (!this.registry.has(key)) { | ||
throw new Error(`Builder with key '${key}' does not exist`); | ||
} | ||
return this.registry.delete(key); | ||
} | ||
|
||
allNames(): K[] { | ||
return Array.from(this.registry.keys()); | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
type Configuration = Record<string, any>; | ||
type Builder<T extends Configuration = Configuration> = (config: T) => () => any; | ||
|
||
export class Registry<K extends string> { | ||
private registry: Map<K, Builder> = new Map(); | ||
|
||
register(key: K, builder: Builder): void { | ||
if (this.registry.has(key)) { | ||
throw new Error(`Builder with key '${key}' is already registered`); | ||
} | ||
this.registry.set(key, builder); | ||
} | ||
|
||
has(key: K): boolean { | ||
return this.registry.has(key); | ||
} | ||
|
||
get(key: K): Builder { | ||
if (!this.has(key)) { | ||
throw new Error(`There is no builder with key '${key}' registered`); | ||
} | ||
|
||
return this.registry.get(key)!; | ||
} | ||
|
||
remove(key: K): boolean { | ||
if (!this.registry.has(key)) { | ||
throw new Error(`Builder with key '${key}' does not exist`); | ||
} | ||
return this.registry.delete(key); | ||
} | ||
|
||
allNames(): K[] { | ||
return Array.from(this.registry.keys()); | ||
} | ||
} |