Add events to MessageQueueInterface.

Added strict typings for events.
pull/1160/head
Mikunj 5 years ago
parent 21586f8e14
commit 0f6053ce08

@ -1,13 +1,19 @@
import { MessageQueueInterface } from './MessageQueueInterface';
import { EventEmitter } from 'events';
import {
MessageQueueInterface,
MessageQueueInterfaceEvents,
} from './MessageQueueInterface';
import { OpenGroupMessage, OutgoingContentMessage } from '../messages/outgoing';
import { JobQueue } from '../utils/JobQueue';
import { PendingMessageCache } from './PendingMessageCache';
import { JobQueue, TypedEventEmitter } from '../utils';
export class MessageQueue implements MessageQueueInterface {
public readonly events: TypedEventEmitter<MessageQueueInterfaceEvents>;
private readonly jobQueues: Map<string, JobQueue> = new Map();
private readonly cache: PendingMessageCache;
constructor() {
this.events = new EventEmitter();
this.cache = new PendingMessageCache();
this.processAllPending();
}

@ -1,13 +1,19 @@
import { OpenGroupMessage, OutgoingContentMessage } from '../messages/outgoing';
import { RawMessage } from '../types/RawMessage';
import { TypedEventEmitter } from '../utils';
// TODO: add all group messages here, replace OutgoingContentMessage with them
type GroupMessageType = OpenGroupMessage | OutgoingContentMessage;
export interface MessageQueueInterfaceEvents {
success: (message: RawMessage) => void;
fail: (message: RawMessage, error: Error) => void;
}
export interface MessageQueueInterface {
events: TypedEventEmitter<MessageQueueInterfaceEvents>;
sendUsingMultiDevice(user: string, message: OutgoingContentMessage): void;
send(device: string, message: OutgoingContentMessage): void;
sendToGroup(message: GroupMessageType): void;
sendSyncMessage(message: OutgoingContentMessage): void;
// TODO: Find a good way to handle events in this
// E.g if we do queue.onMessageSent() we want to also be able to stop listening to the event
// TODO: implement events here
}

@ -1,5 +1,6 @@
// TS 3.8 supports export * as X from 'Y'
import * as MessageSender from './MessageSender';
import { MessageQueue } from './MessageQueue';
import { MessageQueueInterface } from './MessageQueueInterface';
export { MessageSender };
export { MessageSender, MessageQueue, MessageQueueInterface };
export * from './MessageQueue';
export * from './MessageQueueInterface';

@ -0,0 +1,53 @@
// Code from https://github.com/andywer/typed-emitter
type Arguments<T> = [T] extends [(...args: infer U) => any]
? U
: [T] extends [void] ? [] : [T];
/**
* Type-safe event emitter.
*
* Use it like this:
*
* interface MyEvents {
* error: (error: Error) => void
* message: (from: string, content: string) => void
* }
*
* const myEmitter = new EventEmitter() as TypedEmitter<MyEvents>
*
* myEmitter.on("message", (from, content) => {
* // ...
* })
*
* myEmitter.emit("error", "x") // <- Will catch this type error
*
* or
*
* class MyEmitter extends EventEmitter implements TypedEventEmitter<MyEvents>
*/
export interface TypedEventEmitter<Events> {
addListener<E extends keyof Events>(event: E, listener: Events[E]): this;
on<E extends keyof Events>(event: E, listener: Events[E]): this;
once<E extends keyof Events>(event: E, listener: Events[E]): this;
prependListener<E extends keyof Events>(event: E, listener: Events[E]): this;
prependOnceListener<E extends keyof Events>(
event: E,
listener: Events[E]
): this;
off<E extends keyof Events>(event: E, listener: Events[E]): this;
removeAllListeners<E extends keyof Events>(event?: E): this;
removeListener<E extends keyof Events>(event: E, listener: Events[E]): this;
emit<E extends keyof Events>(
event: E,
...args: Arguments<Events[E]>
): boolean;
eventNames(): Array<keyof Events | string | symbol>;
listeners<E extends keyof Events>(event: E): Array<Function>;
listenerCount<E extends keyof Events>(event: E): number;
getMaxListeners(): number;
setMaxListeners(maxListeners: number): this;
}

@ -0,0 +1,2 @@
export * from './TypedEmitter';
export * from './JobQueue';
Loading…
Cancel
Save