From 13c7f55a793974b05a2b343547330e873a89d75a Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 4 Nov 2020 16:01:25 +0100 Subject: [PATCH] split up setKey and setKeyAndQueue as dehydrating in the background prevents use-cases where you want to await the creation of the dehydrated device --- src/client.js | 2 +- src/crypto/dehydration.ts | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/client.js b/src/client.js index 3f2ac2c66..837122c35 100644 --- a/src/client.js +++ b/src/client.js @@ -577,7 +577,7 @@ MatrixClient.prototype.setDehydrationKey = async function( logger.warn('not dehydrating device if crypto is not enabled'); return; } - return await this._crypto._dehydrationManager.setDehydrationKey( + return await this._crypto._dehydrationManager.setKeyAndQueue( key, keyInfo, deviceDisplayName, ); }; diff --git a/src/crypto/dehydration.ts b/src/crypto/dehydration.ts index f9996c583..e276c140c 100644 --- a/src/crypto/dehydration.ts +++ b/src/crypto/dehydration.ts @@ -77,10 +77,23 @@ export class DehydrationManager { }, ); } - async setDehydrationKey( + + /** set the key, and queue periodic dehydration to the server in the background */ + async setKeyAndQueueDehydration( key: Uint8Array, keyInfo: {[props: string]: any} = {}, deviceDisplayName: string = undefined, ): Promise { + const matches = await this.setKey(key, keyInfo, deviceDisplayName); + if (!matches) { + // start dehydration in the background + this.dehydrateDevice(); + } + } + + async setKey( + key: Uint8Array, keyInfo: {[props: string]: any} = {}, + deviceDisplayName: string = undefined, + ): Promise { if (!key) { // unsetting the key -- cancel any pending dehydration task if (this.timeoutId) { @@ -116,9 +129,8 @@ export class DehydrationManager { this.key = key; this.keyInfo = keyInfo; this.deviceDisplayName = deviceDisplayName; - // start dehydration in the background - this.dehydrateDevice(); } + return matches; } private async dehydrateDevice(): Promise { if (this.inProgress) {