Files
owl/src/registry.ts
T

29 lines
692 B
TypeScript
Raw Normal View History

2019-03-04 22:47:20 +01:00
/**
* The registry is basically a simple hashmap. It is only a little safer and
* more structured than a simple object.
*/
2019-02-02 16:51:24 +01:00
export class Registry<T> {
private map: { [key: string]: T } = {};
2019-03-04 22:47:20 +01:00
/**
* Add an element to the registry. Note that the add method returns the
* registry, to it can be chained.
*/
2019-02-02 16:51:24 +01:00
add(key: string, item: T): Registry<T> {
if (key in this.map) {
throw new Error(`Key ${key} already exists!`);
}
this.map[key] = item;
return this;
}
2019-03-04 22:47:20 +01:00
/**
* Returns the element corresponding to the key
*
* Nothing is done to check that the key actually exists.
*/
get(key: string): T | undefined {
2019-02-02 16:51:24 +01:00
return this.map[key];
}
}