Skip to content

Commit

Permalink
fix(types): Minor type fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwalton committed Aug 25, 2021
1 parent 067be9a commit 6865613
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 64 deletions.
86 changes: 35 additions & 51 deletions src/AmqpConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,47 @@ export interface IAmqpConnectionManager {
heartbeatIntervalInSeconds: number;
reconnectTimeInSeconds: number;

addListener(event: string, listener: (...args: any[]) => void): this;
addListener(event: 'connect', listener: ConnectListener): this;
addListener(event: 'blocked', listener: (arg: { reason: string }) => void): this;
addListener(event: 'unblocked', listener: () => void): this;
addListener(event: 'disconnect', listener: (arg: { err: Error }) => void): this;

on(event: string, listener: (...args: any[]) => void): this;
on(event: 'connect', listener: ConnectListener): this;
on(event: 'blocked', listener: (arg: { reason: string }) => void): this;
on(event: 'unblocked', listener: () => void): this;
on(event: 'disconnect', listener: (arg: { err: Error }) => void): this;

once(event: string, listener: (...args: any[]) => void): this;
once(event: 'connect', listener: ConnectListener): this;
once(event: 'blocked', listener: (arg: { reason: string }) => void): this;
once(event: 'unblocked', listener: () => void): this;
once(event: 'disconnect', listener: (arg: { err: Error }) => void): this;

prependListener(event: string, listener: (...args: any[]) => void): this;
prependListener(event: 'connect', listener: ConnectListener): this;
prependListener(event: 'blocked', listener: (arg: { reason: string }) => void): this;
prependListener(event: 'unblocked', listener: () => void): this;
prependListener(event: 'disconnect', listener: (arg: { err: Error }) => void): this;

prependOnceListener(event: string, listener: (...args: any[]) => void): this;
prependOnceListener(event: 'connect', listener: ConnectListener): this;
prependOnceListener(event: 'blocked', listener: (arg: { reason: string }) => void): this;
prependOnceListener(event: 'unblocked', listener: () => void): this;
prependOnceListener(event: 'disconnect', listener: (arg: { err: Error }) => void): this;

removeListener(event: string, listener: (...args: any[]) => void): this;

createChannel(options?: CreateChannelOpts): ChannelWrapper;
close(): Promise<void>;
isConnected(): boolean;

/** The current connection. */
get connection(): Connection | undefined;

/** Returns the number of registered channels. */
get channelCount(): number;
}

//
Expand Down Expand Up @@ -122,69 +151,24 @@ export default class AmqpConnectionManager extends EventEmitter implements IAmqp
public heartbeatIntervalInSeconds: number;
public reconnectTimeInSeconds: number;

addListener(event: string, listener: (...args: any[]) => void): this;
addListener(event: 'connect', listener: ConnectListener): this;
addListener(event: 'blocked', listener: (arg: { reason: string }) => void): this;
addListener(event: 'unblocked', listener: () => void): this;
addListener(event: 'disconnect', listener: (arg: { err: Error }) => void): this;
addListener(event: string, listener: (...args: any[]) => void): this {
return super.addListener(event, listener);
}

on(event: string, listener: (...args: any[]) => void): this;
on(event: 'connect', listener: ConnectListener): this;
on(event: 'blocked', listener: (arg: { reason: string }) => void): this;
on(event: 'unblocked', listener: () => void): this;
on(event: 'disconnect', listener: (arg: { err: Error }) => void): this;
on(event: string, listener: (...args: any[]) => void): this {
return super.on(event, listener);
}

once(event: string, listener: (...args: any[]) => void): this;
once(event: 'connect', listener: ConnectListener): this;
once(event: 'blocked', listener: (arg: { reason: string }) => void): this;
once(event: 'unblocked', listener: () => void): this;
once(event: 'disconnect', listener: (arg: { err: Error }) => void): this;
once(event: string, listener: (...args: any[]) => void): this {
return super.once(event, listener);
}

prependListener(event: string, listener: (...args: any[]) => void): this;
prependListener(event: 'connect', listener: ConnectListener): this;
prependListener(event: 'blocked', listener: (arg: { reason: string }) => void): this;
prependListener(event: 'unblocked', listener: () => void): this;
prependListener(event: 'disconnect', listener: (arg: { err: Error }) => void): this;
prependListener(event: string, listener: (...args: any[]) => void): this {
return super.prependListener(event, listener);
}

prependOnceListener(event: string, listener: (...args: any[]) => void): this;
prependOnceListener(event: 'connect', listener: ConnectListener): this;
prependOnceListener(event: 'blocked', listener: (arg: { reason: string }) => void): this;
prependOnceListener(event: 'unblocked', listener: () => void): this;
prependOnceListener(event: 'disconnect', listener: (arg: { err: Error }) => void): this;
prependOnceListener(event: string, listener: (...args: any[]) => void): this {
return super.prependOnceListener(event, listener);
}

/**
* Create a new AmqplibConnectionManager.
*
* @param {(string|Object)[]} urls - An array of brokers to connect to.
* @param urls - An array of brokers to connect to.
* Takes url strings or objects {url: string, connectionOptions?: object}
* If present, a broker's [connectionOptions] will be used instead
* of [options.connectionOptions] when passed to the amqplib connect method.
* AmqplibConnectionManager will round-robin between them whenever it
* needs to create a new connection.
* @param {Object} [options={}] -
* @param {number} [options.heartbeatIntervalInSeconds=5] - The interval,
* @param [options={}] -
* @param [options.heartbeatIntervalInSeconds=5] - The interval,
* in seconds, to send heartbeats.
* @param {number} [options.reconnectTimeInSeconds] - The time to wait
* @param [options.reconnectTimeInSeconds] - The time to wait
* before trying to reconnect. If not specified, defaults to
* `heartbeatIntervalInSeconds`.
* @param {Object} [options.connectionOptions] - Passed to the amqplib
* @param [options.connectionOptions] - Passed to the amqplib
* connect method.
* @param {function} [options.findServers] - A `fn(callback)` or a `fn()`
* @param [options.findServers] - A `fn(callback)` or a `fn()`
* which returns a Promise. This should resolve to one or more servers
* to connect to, either a single URL or an array of URLs. This is handy
* when you're using a service discovery mechanism such as Consul or etcd.
Expand Down
20 changes: 10 additions & 10 deletions src/ChannelWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ export default class ChannelWrapper extends EventEmitter {
* Setup functions should, ideally, not throw errors, but if they do then the ChannelWrapper will emit an 'error'
* event.
*
* @param {function} setup - setup function.
* @param {function} [done] - callback.
* @returns {void | Promise} - Resolves when complete.
* @param setup - setup function.
* @param [done] - callback.
* @returns - Resolves when complete.
*/
addSetup(setup: SetupFunc, done?: pb.Callback<void>): Promise<void> {
return pb.addCallback(
Expand Down Expand Up @@ -214,8 +214,8 @@ export default class ChannelWrapper extends EventEmitter {
* Returns a Promise which resolves when this channel next connects.
* (Mainly here for unit testing...)
*
* @param {function} [done] - Optional callback.
* @returns {void | Promise} - Resolves when connected.
* @param [done] - Optional callback.
* @returns - Resolves when connected.
*/
waitForConnect(done?: pb.Callback<void>): Promise<void> {
return pb.addCallback(
Expand Down Expand Up @@ -295,13 +295,13 @@ export default class ChannelWrapper extends EventEmitter {
/**
* Create a new ChannelWrapper.
*
* @param {AmqpConnectionManager} connectionManager - connection manager which
* @param connectionManager - connection manager which
* created this channel.
* @param {Object} [options] -
* @param {string} [options.name] - A name for this channel. Handy for debugging.
* @param {function} [options.setup] - A default setup function to call. See
* @param [options] -
* @param [options.name] - A name for this channel. Handy for debugging.
* @param [options.setup] - A default setup function to call. See
* `addSetup` for details.
* @param {boolean} [options.json] - if true, then ChannelWrapper assumes all
* @param [options.json] - if true, then ChannelWrapper assumes all
* messages passed to `publish()` and `sendToQueue()` are plain JSON objects.
* These will be encoded automatically before being sent.
*
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import AmqpConnectionManager, { AmqpConnectionManagerOptions } from './AmqpConnectionManager.js';
import AmqpConnectionManager, {
AmqpConnectionManagerOptions,
IAmqpConnectionManager,
} from './AmqpConnectionManager.js';

export type {
AmqpConnectionManagerOptions,
default as AmqpConnectionManager,
IAmqpConnectionManager as AmqpConnectionManager,
} from './AmqpConnectionManager.js';

export type { SetupFunc, CreateChannelOpts, default as ChannelWrapper } from './ChannelWrapper.js';

export function connect(
urls: string[],
options: AmqpConnectionManagerOptions
): AmqpConnectionManager {
): IAmqpConnectionManager {
return new AmqpConnectionManager(urls, options);
}

Expand Down
4 changes: 4 additions & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ export class FakeAmqpConnectionManager extends EventEmitter implements IAmqpConn
return this._connection as any as Connection | undefined;
}

get channelCount(): number {
return 0;
}

isConnected() {
return this.connected;
}
Expand Down

0 comments on commit 6865613

Please sign in to comment.