Skip to content

Commit f6cd762

Browse files
authored
fix(*): fix fetchChannelPermissions command timeout (#42)
1 parent 61dde41 commit f6cd762

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed
Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
1+
import { CommandTimeout } from '@/config';
2+
import { CommandTimedOutError } from '@/errors';
13
import { MumbleSocket } from '@/mumble-socket';
24
import { filterPacket } from '@/rxjs-operators/filter-packet';
35
import { PermissionQuery } from '@tf2pickup-org/mumble-protocol';
4-
import { filter, take } from 'rxjs';
6+
import {
7+
concatMap,
8+
filter,
9+
lastValueFrom,
10+
race,
11+
take,
12+
throwError,
13+
timer,
14+
} from 'rxjs';
515

616
export const fetchChannelPermissions = async (
717
socket: MumbleSocket,
818
channelId: number,
919
): Promise<PermissionQuery> => {
10-
return new Promise(resolve => {
11-
socket.packet
12-
.pipe(
20+
const ret = lastValueFrom(
21+
race(
22+
socket.packet.pipe(
1323
filterPacket(PermissionQuery),
1424
filter(permissionQuery => permissionQuery.channelId === channelId),
1525
take(1),
16-
)
17-
.subscribe(resolve);
18-
socket.send(PermissionQuery, PermissionQuery.create({ channelId }));
19-
});
26+
),
27+
timer(CommandTimeout).pipe(
28+
concatMap(() =>
29+
throwError(() => new CommandTimedOutError('fetchChannelPermissions')),
30+
),
31+
),
32+
),
33+
);
34+
35+
// Send TWO PermissionQuery packets; if we send only one, the mumble server might not respond,
36+
// causing the command to time out.
37+
// I have no idea what is going on here, but I'm either dumb or mumble server is bugged as heck.
38+
[0, 1].forEach(() =>
39+
socket.send(PermissionQuery, PermissionQuery.create({ channelId })),
40+
);
41+
return ret;
2042
};

0 commit comments

Comments
 (0)