fix: make allowOneAtATime take a generic

pull/2571/head
Audric Ackermann 3 years ago
parent ae51b0cd96
commit 37cedaf24a

@ -142,36 +142,33 @@ const defaultServerPublicKey = 'a03c383cf63c3c4efe67acc52112a6dd734b3a946b9545f4
const defaultRoom = `${defaultServer}/main?public_key=${defaultServerPublicKey}`; const defaultRoom = `${defaultServer}/main?public_key=${defaultServerPublicKey}`;
const loadDefaultRoomsSingle = () => const loadDefaultRoomsSingle = () =>
allowOnlyOneAtATime( allowOnlyOneAtATime('loadDefaultRoomsSingle', async () => {
'loadDefaultRoomsSingle', const roomInfos = parseOpenGroupV2(defaultRoom);
async (): Promise<Array<OpenGroupV2InfoJoinable>> => { if (roomInfos) {
const roomInfos = parseOpenGroupV2(defaultRoom); try {
if (roomInfos) { const roomsGot = await getAllRoomInfos(roomInfos);
try {
const roomsGot = await getAllRoomInfos(roomInfos); if (!roomsGot) {
return [];
if (!roomsGot) {
return [];
}
return roomsGot.map(room => {
return {
...room,
completeUrl: getCompleteUrlFromRoom({
serverUrl: roomInfos.serverUrl,
serverPublicKey: roomInfos.serverPublicKey,
roomId: room.id,
}),
};
});
} catch (e) {
window?.log?.warn('loadDefaultRoomloadDefaultRoomssIfNeeded failed', e);
} }
return [];
return roomsGot.map(room => {
return {
...room,
completeUrl: getCompleteUrlFromRoom({
serverUrl: roomInfos.serverUrl,
serverPublicKey: roomInfos.serverPublicKey,
roomId: room.id,
}),
};
});
} catch (e) {
window?.log?.warn('loadDefaultRoomloadDefaultRoomssIfNeeded failed', e);
} }
return []; return [];
} }
); return [];
});
/** /**
* Load to the cache all the details of the room of the default opengroupv2 server * Load to the cache all the details of the room of the default opengroupv2 server

@ -45,7 +45,7 @@ export class OpenGroupManagerV2 {
serverUrl: string, serverUrl: string,
roomId: string, roomId: string,
publicKey: string publicKey: string
): Promise<ConversationModel> { ): Promise<ConversationModel | undefined> {
const oneAtaTimeStr = `oneAtaTimeOpenGroupV2Join:${serverUrl}${roomId}`; const oneAtaTimeStr = `oneAtaTimeOpenGroupV2Join:${serverUrl}${roomId}`;
return allowOnlyOneAtATime(oneAtaTimeStr, async () => { return allowOnlyOneAtATime(oneAtaTimeStr, async () => {
return this.attemptConnectionV2(serverUrl, roomId, publicKey); return this.attemptConnectionV2(serverUrl, roomId, publicKey);

@ -103,10 +103,10 @@ export async function sogsV3FetchPreviewAndSaveIt(roomInfos: OpenGroupV2RoomWith
// make sure this runs only once for each rooms. // make sure this runs only once for each rooms.
// we don't want to trigger one of those on each setPollInfo results as it happens on each batch poll. // we don't want to trigger one of those on each setPollInfo results as it happens on each batch poll.
const oneAtAtimeResult = (await allowOnlyOneAtATime( const oneAtAtimeResult = await allowOnlyOneAtATime(
`sogsV3FetchPreview-${serverUrl}-${roomId}`, `sogsV3FetchPreview-${serverUrl}-${roomId}`,
() => sogsV3FetchPreview(roomInfos, blinded) () => sogsV3FetchPreview(roomInfos, blinded)
)) as Uint8Array | null; // force the return type as allowOnlyOneAtATime does not keep it );
if (!oneAtAtimeResult || !oneAtAtimeResult?.byteLength) { if (!oneAtAtimeResult || !oneAtAtimeResult?.byteLength) {
window?.log?.warn('sogsV3FetchPreviewAndSaveIt failed for room: ', roomId); window?.log?.warn('sogsV3FetchPreviewAndSaveIt failed for room: ', roomId);

@ -146,9 +146,7 @@ export interface SnodeFromSeed {
} }
const getSnodeListFromSeednodeOneAtAtime = async (seedNodes: Array<string>) => const getSnodeListFromSeednodeOneAtAtime = async (seedNodes: Array<string>) =>
allowOnlyOneAtATime('getSnodeListFromSeednode', () => allowOnlyOneAtATime('getSnodeListFromSeednode', () => getSnodeListFromSeednode(seedNodes));
getSnodeListFromSeednode(seedNodes)
) as Promise<Array<SnodeFromSeed>>;
/** /**
* This call will try 4 times to contact a seed nodes (random) and get the snode list from it. * This call will try 4 times to contact a seed nodes (random) and get the snode list from it.

@ -17,11 +17,11 @@ export class TaskTimedOutError extends Error {
// one action resolves all // one action resolves all
const oneAtaTimeRecord: Record<string, Promise<any>> = {}; const oneAtaTimeRecord: Record<string, Promise<any>> = {};
export async function allowOnlyOneAtATime( export async function allowOnlyOneAtATime<T>(
name: string, name: string,
process: () => Promise<any>, process: () => Promise<T | undefined>,
timeoutMs?: number timeoutMs?: number
) { ): Promise<T> {
// if currently not in progress // if currently not in progress
if (oneAtaTimeRecord[name] === undefined) { if (oneAtaTimeRecord[name] === undefined) {
// set lock // set lock
@ -37,7 +37,7 @@ export async function allowOnlyOneAtATime(
}, timeoutMs); }, timeoutMs);
} }
// do actual work // do actual work
let innerRetVal; let innerRetVal: T | undefined;
try { try {
innerRetVal = await process(); innerRetVal = await process();
} catch (e) { } catch (e) {

Loading…
Cancel
Save