diff --git a/package.json b/package.json index ba5cf6f..3d30317 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bx24dev", - "version": "0.4.1", + "version": "0.4.2", "description": "Bitrix24 Dev extension", "author": "Andrey Neyman ", "engines": { diff --git a/src/extension/index.js b/src/extension/index.js index 61715cf..2c5df9c 100644 --- a/src/extension/index.js +++ b/src/extension/index.js @@ -110,4 +110,4 @@ const initBX24 = (authId, authData) => { } } }); -})(); \ No newline at end of file +})(); diff --git a/src/lib/Manager/ExtensionManager.js b/src/lib/Manager/ExtensionManager.js index 8c7df2e..32c9712 100644 --- a/src/lib/Manager/ExtensionManager.js +++ b/src/lib/Manager/ExtensionManager.js @@ -48,35 +48,28 @@ class ExtensionManager extends AbstractManager { const instanceId = payload.payload.authId; console.log('Manager.onMessageGetAuth()', instanceId, payload); - if (!this.instances[instanceId]) { - console.log('Instance isn\'t in memory, try to hydrate'); - // If there is no instance in memory, try to hydrate it from session data - let result = await this.getSessionInstanceData(); - const data = result.instanceData[instanceId]; + let instance = await this.getInstance(instanceId); - if (!data) { - console.error(`Authorization with ID ${instanceId} not found`); - return; - } - - this.hydrateInstance(data); + if (instance === null) { + return; } //await sleep(5000); - console.log('Data will be sent as response', this.instances[instanceId].getData()); - sendResponse(this.instances[instanceId].getData()); + console.log('Data will be sent as response', instance.getData()); + sendResponse(instance.getData()); } async onMessageRefreshAuth(payload, sender, sendResponse) { const authId = payload.payload.authId; console.log('Manager.onMessageRefreshAuth()', authId); - if (!this.instances[authId]) { - console.error(`Authorization with ID ${authId} not found`); + let instance = await this.getInstance(instanceId); + + if (instance === null) { return; } - let result = await this.instances[authId].refreshAuth(); + let result = await instance.refreshAuth(); console.log('Manager.onMessageRefreshAuth(), result', result); sendResponse(result); } @@ -286,6 +279,31 @@ class ExtensionManager extends AbstractManager { this.hydrateInstance(data); } } + + /** + * Returns instance from memory. If nothing found, + * try to find it in session data and hydrate object. + * + * @param instanceId + * @returns {Promise} + */ + async getInstance(instanceId) { + if (!this.instances[instanceId]) { + console.log('Instance isn\'t in memory, try to hydrate'); + // If there is no instance in memory, try to hydrate it from session data + let result = await this.getSessionInstanceData(); + const data = result.instanceData[instanceId]; + + if (!data) { + console.error(`Authorization with ID ${instanceId} not found`); + return null; + } + + this.hydrateInstance(data); + } + + return this.instances[instanceId]; + } } const manager = new ExtensionManager(messageListener);