Skip to content

Instantly share code, notes, and snippets.

@xMartin
Created September 1, 2014 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xMartin/ba4e098cbbbe92e48fc7 to your computer and use it in GitHub Desktop.
Save xMartin/ba4e098cbbbe92e48fc7 to your computer and use it in GitHub Desktop.
JS Component Registry
function Registry() {
this._store = {};
}
Registry.prototype.get = function (id) {
return this._store[id];
};
Registry.prototype.require = function (id) {
var entry = this.get(id);
if (entry === undefined) {
throw new Error('Registry: "' + id + '" not found.');
}
return entry;
};
Registry.prototype.set = function (id, entry) {
var existingEntry = this.get(id);
if (existingEntry !== undefined) {
throw new Error('Registry: "' + id + '" already registered');
}
this.reset(id, entry);
};
Registry.prototype.reset = function (id, entry) {
this._store[id] = entry;
};
Registry.prototype.remove = function (id) {
delete this._store[id];
};
Registry.prototype.destroy = function (id) {
var entry = this.get(id);
if (entry !== undefined) {
if (entry.destroy) {
entry.destroy();
}
this.remove(id);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment