Skip to content

Commit

Permalink
fix(*): handle createChannel command timeout (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc authored May 22, 2022
1 parent d7ac929 commit cc3524a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/commands/create-channel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { PermissionDeniedError } from '@/errors';
import { CommandTimeout } from '@/config';
import { CommandTimedOutError, PermissionDeniedError } from '@/errors';
import { MumbleSocket } from '@/mumble-socket';
import { filterPacket } from '@/rxjs-operators/filter-packet';
import { ChannelState, PermissionDenied } from '@proto/Mumble';
import { filter, race, take } from 'rxjs';
import { filter, race, take, timer } from 'rxjs';

export const createChannel = async (
socket: MumbleSocket,
Expand All @@ -21,11 +22,14 @@ export const createChannel = async (
take(1),
),
socket.packet.pipe(filterPacket(PermissionDenied), take(1)),
timer(CommandTimeout),
).subscribe(packet => {
if (PermissionDenied.is(packet)) {
reject(new PermissionDeniedError(packet));
} else {
} else if (ChannelState.is(packet)) {
resolve(packet.channelId as number);
} else {
reject(new CommandTimedOutError('createChannel'));
}
});

Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const CommandTimeout = 5000;
5 changes: 5 additions & 0 deletions src/errors/command-timed-out.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class CommandTimedOutError extends Error {
constructor(commandName: string) {
super(`command ${commandName} has timed out`);
}
}
1 change: 1 addition & 0 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { CommandTimedOutError } from './command-timed-out.error';
export { ConnectionRejectedError } from './connection-rejected.error';
export { InsufficientPermissionsError } from './insufficient-permissions.error';
export { NoSuchChannelError } from './no-such-channel.error';
Expand Down

0 comments on commit cc3524a

Please sign in to comment.