Skip to content

Instantly share code, notes, and snippets.

@pa-ulander
Last active February 2, 2024 02:07
Show Gist options
  • Save pa-ulander/f96b529bc4f18703cd3688ffe78a1c29 to your computer and use it in GitHub Desktop.
Save pa-ulander/f96b529bc4f18703cd3688ffe78a1c29 to your computer and use it in GitHub Desktop.
/**
* Unfancy registry class to use for simple registry needs
* Just a silly wrapper around Map()
*/
export default class Registry {
constructor() {
if (!Registry.instance) {
this.registry = new Map();
Registry.instance = this;
}
return Registry.instance;
}
/**
* clear registry
*/
clear() {
this.registry.clear();
}
/**
* get registry
* @returns object - Map object
*/
get() {
return this.registry;
}
/**
*
* @param {*} key
* @returns
*/
getValue(key) {
return this.registry.get(key);
}
/**
* Chainable setter, hence the return
*
* @param {*} string - registry key to add or set
* @param {*} any - values to register
* @returns object - Map object
*/
setValue(key, val) {
this.registry.set(key, val);
return this;
}
/**
*
* @param {*} key
*/
deleteValue(key) {
this.registry.delete(key);
}
/**
*
* @returns
*/
getEntriesIterator() {
return this.registry.entries();
}
/**
*
* @returns
*/
getValuesIterator() {
return this.registry.values();
}
/**
* @returns Iterator - all registry key in insertion order
*/
getKeys() {
return this.registry.keys();
}
/**
*
* @param {*} key
* @returns
*/
hasValue(key) {
return this.registry.has(key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment