Created
February 17, 2015 12:34
-
-
Save you-think-you-are-special/fdb05444d8a3c826d1f2 to your computer and use it in GitHub Desktop.
ES6 JS implementation of Registry pattern
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
/** | |
* 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