Skip to content

Commit 37cedaf

Browse files
committed
fix: make allowOneAtATime take a generic
1 parent ae51b0c commit 37cedaf

File tree

5 files changed

+31
-36
lines changed

5 files changed

+31
-36
lines changed

ts/session/apis/open_group_api/opengroupV2/ApiUtil.ts

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -142,36 +142,33 @@ const defaultServerPublicKey = 'a03c383cf63c3c4efe67acc52112a6dd734b3a946b9545f4
142142
const defaultRoom = `${defaultServer}/main?public_key=${defaultServerPublicKey}`;
143143

144144
const loadDefaultRoomsSingle = () =>
145-
allowOnlyOneAtATime(
146-
'loadDefaultRoomsSingle',
147-
async (): Promise<Array<OpenGroupV2InfoJoinable>> => {
148-
const roomInfos = parseOpenGroupV2(defaultRoom);
149-
if (roomInfos) {
150-
try {
151-
const roomsGot = await getAllRoomInfos(roomInfos);
152-
153-
if (!roomsGot) {
154-
return [];
155-
}
156-
157-
return roomsGot.map(room => {
158-
return {
159-
...room,
160-
completeUrl: getCompleteUrlFromRoom({
161-
serverUrl: roomInfos.serverUrl,
162-
serverPublicKey: roomInfos.serverPublicKey,
163-
roomId: room.id,
164-
}),
165-
};
166-
});
167-
} catch (e) {
168-
window?.log?.warn('loadDefaultRoomloadDefaultRoomssIfNeeded failed', e);
145+
allowOnlyOneAtATime('loadDefaultRoomsSingle', async () => {
146+
const roomInfos = parseOpenGroupV2(defaultRoom);
147+
if (roomInfos) {
148+
try {
149+
const roomsGot = await getAllRoomInfos(roomInfos);
150+
151+
if (!roomsGot) {
152+
return [];
169153
}
170-
return [];
154+
155+
return roomsGot.map(room => {
156+
return {
157+
...room,
158+
completeUrl: getCompleteUrlFromRoom({
159+
serverUrl: roomInfos.serverUrl,
160+
serverPublicKey: roomInfos.serverPublicKey,
161+
roomId: room.id,
162+
}),
163+
};
164+
});
165+
} catch (e) {
166+
window?.log?.warn('loadDefaultRoomloadDefaultRoomssIfNeeded failed', e);
171167
}
172168
return [];
173169
}
174-
);
170+
return [];
171+
});
175172

176173
/**
177174
* Load to the cache all the details of the room of the default opengroupv2 server

ts/session/apis/open_group_api/opengroupV2/OpenGroupManagerV2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class OpenGroupManagerV2 {
4545
serverUrl: string,
4646
roomId: string,
4747
publicKey: string
48-
): Promise<ConversationModel> {
48+
): Promise<ConversationModel | undefined> {
4949
const oneAtaTimeStr = `oneAtaTimeOpenGroupV2Join:${serverUrl}${roomId}`;
5050
return allowOnlyOneAtATime(oneAtaTimeStr, async () => {
5151
return this.attemptConnectionV2(serverUrl, roomId, publicKey);

ts/session/apis/open_group_api/sogsv3/sogsV3FetchFile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ export async function sogsV3FetchPreviewAndSaveIt(roomInfos: OpenGroupV2RoomWith
103103

104104
// make sure this runs only once for each rooms.
105105
// we don't want to trigger one of those on each setPollInfo results as it happens on each batch poll.
106-
const oneAtAtimeResult = (await allowOnlyOneAtATime(
106+
const oneAtAtimeResult = await allowOnlyOneAtATime(
107107
`sogsV3FetchPreview-${serverUrl}-${roomId}`,
108108
() => sogsV3FetchPreview(roomInfos, blinded)
109-
)) as Uint8Array | null; // force the return type as allowOnlyOneAtATime does not keep it
109+
);
110110

111111
if (!oneAtAtimeResult || !oneAtAtimeResult?.byteLength) {
112112
window?.log?.warn('sogsV3FetchPreviewAndSaveIt failed for room: ', roomId);

ts/session/apis/seed_node_api/SeedNodeAPI.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,7 @@ export interface SnodeFromSeed {
146146
}
147147

148148
const getSnodeListFromSeednodeOneAtAtime = async (seedNodes: Array<string>) =>
149-
allowOnlyOneAtATime('getSnodeListFromSeednode', () =>
150-
getSnodeListFromSeednode(seedNodes)
151-
) as Promise<Array<SnodeFromSeed>>;
149+
allowOnlyOneAtATime('getSnodeListFromSeednode', () => getSnodeListFromSeednode(seedNodes));
152150

153151
/**
154152
* This call will try 4 times to contact a seed nodes (random) and get the snode list from it.

ts/session/utils/Promise.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ export class TaskTimedOutError extends Error {
1717
// one action resolves all
1818
const oneAtaTimeRecord: Record<string, Promise<any>> = {};
1919

20-
export async function allowOnlyOneAtATime(
20+
export async function allowOnlyOneAtATime<T>(
2121
name: string,
22-
process: () => Promise<any>,
22+
process: () => Promise<T | undefined>,
2323
timeoutMs?: number
24-
) {
24+
): Promise<T> {
2525
// if currently not in progress
2626
if (oneAtaTimeRecord[name] === undefined) {
2727
// set lock
@@ -37,7 +37,7 @@ export async function allowOnlyOneAtATime(
3737
}, timeoutMs);
3838
}
3939
// do actual work
40-
let innerRetVal;
40+
let innerRetVal: T | undefined;
4141
try {
4242
innerRetVal = await process();
4343
} catch (e) {

0 commit comments

Comments
 (0)