From 4eda291bcbf5d17fc3901e507bb37c1ed8a132eb Mon Sep 17 00:00:00 2001 From: Frederik Rothenberger Date: Mon, 7 Oct 2024 16:03:20 +0200 Subject: [PATCH] Make identity metadata update synchronous There is no need to `await` metadata if some value needs to be written. Instead we just update the internal promise of the metadata repository to update the value once it is loaded. --- .../src/flows/recovery/recoveryWizard.ts | 6 ++---- src/frontend/src/flows/register/index.ts | 4 +--- .../src/repositories/identityMetadata.test.ts | 18 +++++++++--------- .../src/repositories/identityMetadata.ts | 11 +++-------- src/frontend/src/utils/iiConnection.test.ts | 2 +- src/frontend/src/utils/iiConnection.ts | 2 +- 6 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/frontend/src/flows/recovery/recoveryWizard.ts b/src/frontend/src/flows/recovery/recoveryWizard.ts index 3af05989be..8e112e918d 100644 --- a/src/frontend/src/flows/recovery/recoveryWizard.ts +++ b/src/frontend/src/flows/recovery/recoveryWizard.ts @@ -237,8 +237,7 @@ export const recoveryWizard = async ( }); if (devicesStatus !== "no-warning") { - // `await` here doesn't add any waiting time beacause we already got the metadata earlier. - await connection.updateIdentityMetadata({ + connection.updateIdentityMetadata({ recoveryPageShownTimestampMillis: nowInMillis, }); const userChoice = await addDeviceWarning({ @@ -248,8 +247,7 @@ export const recoveryWizard = async ( await addDevice({ userNumber, connection }); } if (userChoice.action === "do-not-remind") { - // `await` here doesn't add any waiting time beacause we already got the metadata earlier. - await connection.updateIdentityMetadata({ + connection.updateIdentityMetadata({ doNotShowRecoveryPageRequestTimestampMillis: nowInMillis, }); } diff --git a/src/frontend/src/flows/register/index.ts b/src/frontend/src/flows/register/index.ts index ebba1b6472..3f490ddcb8 100644 --- a/src/frontend/src/flows/register/index.ts +++ b/src/frontend/src/flows/register/index.ts @@ -194,9 +194,7 @@ export const registerFlow = async ({ await finalizeIdentity?.(userNumber); // We don't want to nudge the user with the recovery phrase warning page // right after they've created their anchor. - // The metadata starts to fetch when the connection is created. - // But it might not have finished yet, so we `await` for `updateIdentityMetadata` to also wait for it. - await result.connection.updateIdentityMetadata({ + result.connection.updateIdentityMetadata({ recoveryPageShownTimestampMillis: Date.now(), }); await setAnchorUsed(userNumber); diff --git a/src/frontend/src/repositories/identityMetadata.test.ts b/src/frontend/src/repositories/identityMetadata.test.ts index 08675af092..aec091608f 100644 --- a/src/frontend/src/repositories/identityMetadata.test.ts +++ b/src/frontend/src/repositories/identityMetadata.test.ts @@ -79,7 +79,7 @@ test("IdentityMetadataRepository changes partial data in memory", async () => { }); const newRecoveryPageShownTimestampMillis = 9876543210; - await instance.updateMetadata({ + instance.updateMetadata({ recoveryPageShownTimestampMillis: newRecoveryPageShownTimestampMillis, }); @@ -98,7 +98,7 @@ test("IdentityMetadataRepository changes data in memory", async () => { const newRecoveryPageShownTimestampMillis = 9876543210; const newDoNotShowRecoveryPageRequestTimestampMillis = 1234567890; - await instance.updateMetadata({ + instance.updateMetadata({ recoveryPageShownTimestampMillis: newRecoveryPageShownTimestampMillis, doNotShowRecoveryPageRequestTimestampMillis: newDoNotShowRecoveryPageRequestTimestampMillis, @@ -128,7 +128,7 @@ test("IdentityMetadataRepository sets data from partial data in memory", async ( }); const newDoNotShowRecoveryPageRequestTimestampMillis = 1234567890; - await instance.updateMetadata({ + instance.updateMetadata({ doNotShowRecoveryPageRequestTimestampMillis: newDoNotShowRecoveryPageRequestTimestampMillis, }); @@ -148,7 +148,7 @@ test("IdentityMetadataRepository sets partial data in memory", async () => { }); const newRecoveryPageShownTimestampMillis = 9876543210; - await instance.updateMetadata({ + instance.updateMetadata({ recoveryPageShownTimestampMillis: newRecoveryPageShownTimestampMillis, }); @@ -166,7 +166,7 @@ test("IdentityMetadataRepository sets data in memory", async () => { const newRecoveryPageShownTimestampMillis = 9876543210; const newDoNotShowRecoveryPageRequestTimestampMillis = 1234567890; - await instance.updateMetadata({ + instance.updateMetadata({ recoveryPageShownTimestampMillis: newRecoveryPageShownTimestampMillis, doNotShowRecoveryPageRequestTimestampMillis: newDoNotShowRecoveryPageRequestTimestampMillis, @@ -187,7 +187,7 @@ test("IdentityMetadataRepository commits updated metadata to canister", async () const newRecoveryPageShownTimestampMillis = 9876543210; const newDoNotShowRecoveryPageRequestTimestampMillis = 1234567890; - await instance.updateMetadata({ + instance.updateMetadata({ recoveryPageShownTimestampMillis: newRecoveryPageShownTimestampMillis, doNotShowRecoveryPageRequestTimestampMillis: newDoNotShowRecoveryPageRequestTimestampMillis, @@ -234,7 +234,7 @@ test("IdentityMetadataRepository doesn't raise an error if committing fails", as doNotShowRecoveryPageRequestTimestampMillis: newDoNotShowRecoveryPageRequestTimestampMillis, }; - await instance.updateMetadata(newMetadata); + instance.updateMetadata(newMetadata); expect(setterMockError).not.toHaveBeenCalled(); const committed = await instance.commitMetadata(); @@ -275,7 +275,7 @@ test("IdentityMetadataRepository commits additional metadata to canister after u }); const newRecoveryPageShownTimestampMillis = 9876543210; - await instance.updateMetadata({ + instance.updateMetadata({ recoveryPageShownTimestampMillis: newRecoveryPageShownTimestampMillis, }); @@ -309,7 +309,7 @@ test("IdentityMetadataRepository commits from initial partial data after adding }); const newDoNotShowRecoveryPageRequestTimestampMillis = 1234567890; - await instance.updateMetadata({ + instance.updateMetadata({ doNotShowRecoveryPageRequestTimestampMillis: newDoNotShowRecoveryPageRequestTimestampMillis, }); diff --git a/src/frontend/src/repositories/identityMetadata.ts b/src/frontend/src/repositories/identityMetadata.ts index da33b0c728..3242039cc7 100644 --- a/src/frontend/src/repositories/identityMetadata.ts +++ b/src/frontend/src/repositories/identityMetadata.ts @@ -143,16 +143,12 @@ export class IdentityMetadataRepository { * We consider the metadata to not be crucial for the application. * Therefore, we don't want to disrupt user flows if there is an error with the metadata. * - * @param {Partial} partialMetadata - * @returns {Promise} To indicate that the metadata has been set. */ - updateMetadata = async ( - partialMetadata: Partial - ): Promise => { + updateMetadata = (partialMetadata: Partial) => { try { - this.metadata = Promise.resolve( + this.metadata = this.metadata.then((metadataMap) => updateMetadataMapV2({ - metadataMap: await this.metadata, + metadataMap: metadataMap, partialIdentityMetadata: partialMetadata, }) ); @@ -163,7 +159,6 @@ export class IdentityMetadataRepository { unknownToString(e, "unknown error") ); } - // Do nothing if the metadata is not loaded. }; /** diff --git a/src/frontend/src/utils/iiConnection.test.ts b/src/frontend/src/utils/iiConnection.test.ts index 4f08dfddd4..552e136640 100644 --- a/src/frontend/src/utils/iiConnection.test.ts +++ b/src/frontend/src/utils/iiConnection.test.ts @@ -73,7 +73,7 @@ test("commits changes on identity metadata", async () => { expect(await connection.getIdentityMetadata()).toEqual(mockIdentityMetadata); const newRecoveryPageShownTimestampMillis = 9876543210; - await connection.updateIdentityMetadata({ + connection.updateIdentityMetadata({ recoveryPageShownTimestampMillis: newRecoveryPageShownTimestampMillis, }); diff --git a/src/frontend/src/utils/iiConnection.ts b/src/frontend/src/utils/iiConnection.ts index cd069a78be..4b07d893fd 100644 --- a/src/frontend/src/utils/iiConnection.ts +++ b/src/frontend/src/utils/iiConnection.ts @@ -555,7 +555,7 @@ export class AuthenticatedConnection extends Connection { updateIdentityMetadata = ( partialMetadata: Partial - ): Promise => { + ) => { return this.metadataRepository.updateMetadata(partialMetadata); };