Skip to content

Instantly share code, notes, and snippets.

@you-think-you-are-special
Created February 17, 2015 12:34
Show Gist options
  • Save you-think-you-are-special/fdb05444d8a3c826d1f2 to your computer and use it in GitHub Desktop.
Save you-think-you-are-special/fdb05444d8a3c826d1f2 to your computer and use it in GitHub Desktop.
ES6 JS implementation of Registry pattern
/**
* Created by Alexander Litvinov
* Email: [email protected]
* May be freely distributed under the MIT license
*/
'use strict';
import _ from 'lodash'
export default
class Registry {
constructor() {
this._storage = {};
this._fabric = {};
}
add(key, value, options) {
if (arguments.length == 3) {
return this._fabric[key] = [value, options]
} else {
return this._storage[key] = value;
}
}
remove(key, isFabric) {
if (isFabric) {
delete this._fabric[key]
}
else {
delete this._storage[key];
}
}
acquire(key, options) {
if (arguments.length == 2) {
if (this._fabric[key]) {
var fabric = this._fabric[key],
create = fabric[0],
params = _.extend({}, fabric[1], options);
return create(params)
} else {
return this._storage[key] || options;
}
} else {
return this._storage[key]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment