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 loadDefaultRoomsSingle = () =>
allowOnlyOneAtATime(
'loadDefaultRoomsSingle',
async (): Promise<Array<OpenGroupV2InfoJoinable>> => {
const roomInfos = parseOpenGroupV2(defaultRoom);
if (roomInfos) {
try {
const roomsGot = await getAllRoomInfos(roomInfos);
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);
allowOnlyOneAtATime('loadDefaultRoomsSingle', async () => {
const roomInfos = parseOpenGroupV2(defaultRoom);
if (roomInfos) {
try {
const roomsGot = await getAllRoomInfos(roomInfos);
if (!roomsGot) {
return [];
}
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 [];
});
/**
* Load to the cache all the details of the room of the default opengroupv2 server

@ -45,7 +45,7 @@ export class OpenGroupManagerV2 {
serverUrl: string,
roomId: string,
publicKey: string
): Promise<ConversationModel> {
): Promise<ConversationModel | undefined> {
const oneAtaTimeStr = `oneAtaTimeOpenGroupV2Join:${serverUrl}${roomId}`;
return allowOnlyOneAtATime(oneAtaTimeStr, async () => {
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.
// 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(roomInfos, blinded)
)) as Uint8Array | null; // force the return type as allowOnlyOneAtATime does not keep it
);
if (!oneAtAtimeResult || !oneAtAtimeResult?.byteLength) {
window?.log?.warn('sogsV3FetchPreviewAndSaveIt failed for room: ', roomId);

@ -146,9 +146,7 @@ export interface SnodeFromSeed {
}
const getSnodeListFromSeednodeOneAtAtime = async (seedNodes: Array<string>) =>
allowOnlyOneAtATime('getSnodeListFromSeednode', () =>
getSnodeListFromSeednode(seedNodes)
) as Promise<Array<SnodeFromSeed>>;
allowOnlyOneAtATime('getSnodeListFromSeednode', () => getSnodeListFromSeednode(seedNodes));
/**
* 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
const oneAtaTimeRecord: Record<string, Promise<any>> = {};
export async function allowOnlyOneAtATime(
export async function allowOnlyOneAtATime<T>(
name: string,
process: () => Promise<any>,
process: () => Promise<T | undefined>,
timeoutMs?: number
) {
): Promise<T> {
// if currently not in progress
if (oneAtaTimeRecord[name] === undefined) {
// set lock
@ -37,7 +37,7 @@ export async function allowOnlyOneAtATime(
}, timeoutMs);
}
// do actual work
let innerRetVal;
let innerRetVal: T | undefined;
try {
innerRetVal = await process();
} catch (e) {

Loading…
Cancel
Save