diff options
author | Marius David Wieschollek <passwords.public@mdns.eu> | 2020-02-27 22:20:56 +0300 |
---|---|---|
committer | Marius David Wieschollek <passwords.public@mdns.eu> | 2020-02-27 22:20:56 +0300 |
commit | f914d88dd200d1c5dd5ed7106e28dd5f4236ce47 (patch) | |
tree | d1cd091e2e2ba9d9c493503d2a1cf44b755faa92 | |
parent | a0ac896fa0be99cdf6f866bdc67d47803acb3dc1 (diff) |
Allow replacement of instances
Signed-off-by: Marius David Wieschollek <passwords.public@mdns.eu>
-rw-r--r-- | src/Api/Api.js | 50 | ||||
-rw-r--r-- | src/Authorization/SessionAuthorization.js | 7 |
2 files changed, 46 insertions, 11 deletions
diff --git a/src/Api/Api.js b/src/Api/Api.js index 84d7892..c14c498 100644 --- a/src/Api/Api.js +++ b/src/Api/Api.js @@ -73,7 +73,7 @@ export default class Api { if(!(server instanceof this._classes.model.server)) { server = this.getInstance('model.server', server); } else { - this._instances.model = {server}; + this.setInstance('model.server', server); } this._server = server; @@ -244,22 +244,27 @@ export default class Api { * @param {String} name * @param {*} properties * @return {Object} + * @api */ getInstance(name, ...properties) { - let path = name.split('.'); - - if(this._instances.hasOwnProperty(path[0]) && this._instances[path[0]].hasOwnProperty(path[1])) { - return this._instances[path[0]][path[1]]; + if(!this._instances.hasOwnProperty(name) || !this._instances[name]) { + this._instances[name] = this.getClass(name, ...properties); } - if(!this._instances.hasOwnProperty(path[0])) { - this._instances[path[0]] = {}; - } + return this._instances[name]; + } - let instance = this.getClass(name, ...properties); - this._instances[path[0]][path[1]] = instance; + /** + * + * @param {String} name + * @param {Object} object + * @return {Api} + * @api + */ + setInstance(name, object) { + this._instances[name] = object; - return instance; + return this; } /** @@ -289,6 +294,29 @@ export default class Api { /** * + * @param {String} name + * @param {Object} constructor + * @return {Api} + * @api + */ + setClass(name, constructor) { + let path = name.split('.'); + + if(!this._classes.hasOwnProperty(path[0])) { + this._classes[path[0]] = {}; + } + + if(!this._classes[path[0]].hasOwnProperty(path[1])) { + this._classes[path[1]] = {}; + } + + this._classes[path[0]][path[1]] = constructor; + + return this; + } + + /** + * * @return {Object} * @private */ diff --git a/src/Authorization/SessionAuthorization.js b/src/Authorization/SessionAuthorization.js index 02a5aad..f8e6b0b 100644 --- a/src/Authorization/SessionAuthorization.js +++ b/src/Authorization/SessionAuthorization.js @@ -17,6 +17,13 @@ export default class SessionAuthorization { } /** + * @return {Boolean} + */ + isLoaded() { + return this._loaded; + } + + /** * * @return {Promise<void>} */ |