Skip to content

Commit

Permalink
Use Promise.any instead of asyncSome
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros committed Nov 14, 2024
1 parent 5b1b10e commit 7ec5450
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/DeviceListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import SettingsStore, { CallbackFn } from "./settings/SettingsStore";
import { UIFeature } from "./settings/UIFeature";
import { isBulkUnverifiedDeviceReminderSnoozed } from "./utils/device/snoozeBulkUnverifiedDeviceReminder";
import { getUserDeviceIds } from "./utils/crypto/deviceInfo";
import { asyncSome } from "./utils/arrays.ts";

const KEY_BACKUP_POLL_INTERVAL = 5 * 60 * 1000;

Expand Down Expand Up @@ -241,7 +240,7 @@ export default class DeviceListener {
return this.keyBackupInfo;
}

private shouldShowSetupEncryptionToast(): Promise<boolean> | boolean {
private async shouldShowSetupEncryptionToast(): Promise<boolean> {
// If we're in the middle of a secret storage operation, we're likely
// modifying the state involved here, so don't add new toasts to setup.
if (isSecretStorageBeingAccessed()) return false;
Expand All @@ -250,7 +249,15 @@ export default class DeviceListener {
const cryptoApi = cli?.getCrypto();
if (!cli || !cryptoApi) return false;

return asyncSome(cli.getRooms(), ({ roomId }) => cryptoApi.isEncryptionEnabledInRoom(roomId));
return await Promise.any(
cli
.getRooms()
.map(({ roomId }) =>
cryptoApi
.isEncryptionEnabledInRoom(roomId)
.then((encrypted) => (encrypted ? Promise.resolve(true) : Promise.reject(false))),
),
);
}

private recheck(): void {
Expand Down
11 changes: 9 additions & 2 deletions src/utils/crypto/shouldSkipSetupEncryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Please see LICENSE files in the repository root for full details.
import { MatrixClient } from "matrix-js-sdk/src/matrix";

import { shouldForceDisableEncryption } from "./shouldForceDisableEncryption";
import { asyncSome } from "../arrays.ts";

/**
* If encryption is force disabled AND the user is not in any encrypted rooms
Expand All @@ -24,6 +23,14 @@ export const shouldSkipSetupEncryption = async (client: MatrixClient): Promise<b

return (
isEncryptionForceDisabled &&
!(await asyncSome(client.getRooms(), ({ roomId }) => crypto.isEncryptionEnabledInRoom(roomId)))
!(await Promise.any(
client
.getRooms()
.map(({ roomId }) =>
crypto
.isEncryptionEnabledInRoom(roomId)
.then((encrypted) => (encrypted ? Promise.resolve(true) : Promise.reject(false))),
),
))
);
};

0 comments on commit 7ec5450

Please sign in to comment.