Created
September 1, 2014 16:20
-
-
Save xMartin/ba4e098cbbbe92e48fc7 to your computer and use it in GitHub Desktop.
JS Component Registry
This file contains hidden or 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
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