Skip to content

Commit

Permalink
Make identity metadata update synchronous
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
frederikrothenberger committed Oct 7, 2024
1 parent c14c6eb commit 4eda291
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 26 deletions.
6 changes: 2 additions & 4 deletions src/frontend/src/flows/recovery/recoveryWizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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,
});
}
Expand Down
4 changes: 1 addition & 3 deletions src/frontend/src/flows/register/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions src/frontend/src/repositories/identityMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ test("IdentityMetadataRepository changes partial data in memory", async () => {
});

const newRecoveryPageShownTimestampMillis = 9876543210;
await instance.updateMetadata({
instance.updateMetadata({
recoveryPageShownTimestampMillis: newRecoveryPageShownTimestampMillis,
});

Expand All @@ -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,
Expand Down Expand Up @@ -128,7 +128,7 @@ test("IdentityMetadataRepository sets data from partial data in memory", async (
});

const newDoNotShowRecoveryPageRequestTimestampMillis = 1234567890;
await instance.updateMetadata({
instance.updateMetadata({
doNotShowRecoveryPageRequestTimestampMillis:
newDoNotShowRecoveryPageRequestTimestampMillis,
});
Expand All @@ -148,7 +148,7 @@ test("IdentityMetadataRepository sets partial data in memory", async () => {
});

const newRecoveryPageShownTimestampMillis = 9876543210;
await instance.updateMetadata({
instance.updateMetadata({
recoveryPageShownTimestampMillis: newRecoveryPageShownTimestampMillis,
});

Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -275,7 +275,7 @@ test("IdentityMetadataRepository commits additional metadata to canister after u
});

const newRecoveryPageShownTimestampMillis = 9876543210;
await instance.updateMetadata({
instance.updateMetadata({
recoveryPageShownTimestampMillis: newRecoveryPageShownTimestampMillis,
});

Expand Down Expand Up @@ -309,7 +309,7 @@ test("IdentityMetadataRepository commits from initial partial data after adding
});

const newDoNotShowRecoveryPageRequestTimestampMillis = 1234567890;
await instance.updateMetadata({
instance.updateMetadata({
doNotShowRecoveryPageRequestTimestampMillis:
newDoNotShowRecoveryPageRequestTimestampMillis,
});
Expand Down
11 changes: 3 additions & 8 deletions src/frontend/src/repositories/identityMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IdentityMetadata>} partialMetadata
* @returns {Promise<void>} To indicate that the metadata has been set.
*/
updateMetadata = async (
partialMetadata: Partial<IdentityMetadata>
): Promise<void> => {
updateMetadata = (partialMetadata: Partial<IdentityMetadata>) => {
try {
this.metadata = Promise.resolve(
this.metadata = this.metadata.then((metadataMap) =>
updateMetadataMapV2({
metadataMap: await this.metadata,
metadataMap: metadataMap,
partialIdentityMetadata: partialMetadata,
})
);
Expand All @@ -163,7 +159,6 @@ export class IdentityMetadataRepository {
unknownToString(e, "unknown error")
);
}
// Do nothing if the metadata is not loaded.
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/utils/iiConnection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/utils/iiConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ export class AuthenticatedConnection extends Connection {

updateIdentityMetadata = (
partialMetadata: Partial<IdentityMetadata>
): Promise<void> => {
) => {
return this.metadataRepository.updateMetadata(partialMetadata);
};

Expand Down

0 comments on commit 4eda291

Please sign in to comment.