Browse Source

refactor: fix event types for library consumers

This requires some `// @ts-expect-error` until we find out how to tell
the typescript compiler what events are available
Jonas Gloning 1 year ago
parent
commit
b49ef4a9b2

+ 8 - 11
lib/baseconnection.ts

@@ -3,7 +3,6 @@ import type { ServerMessage } from "./servermessage";
 import type { ConnectionType } from "./enums";
 import { BaseConnectionErrorType } from "./enums";
 import { PeerError, type PromiseEvents } from "./peerError";
-import type { ValidEventTypes } from "eventemitter3";
 import EventEmitter from "eventemitter3";
 import { EventEmitterWithPromise } from "./eventEmitterWithPromise";
 
@@ -28,13 +27,11 @@ export interface BaseConnectionEvents<
 }
 
 export interface IBaseConnection<
-	SubClassEvents extends ValidEventTypes,
+	SubClassEvents extends BaseConnectionEvents<
+		BaseConnectionErrorType | ErrorType
+	>,
 	ErrorType extends string = never,
-> extends EventEmitter<
-		| (SubClassEvents &
-				BaseConnectionEvents<BaseConnectionErrorType | ErrorType>)
-		| BaseConnectionEvents<BaseConnectionErrorType | ErrorType>
-	> {
+> extends EventEmitter<SubClassEvents> {
 	readonly metadata: any;
 	readonly connectionId: string;
 	get type(): ConnectionType;
@@ -51,17 +48,17 @@ export interface IBaseConnection<
 }
 
 export abstract class BaseConnection<
-		AwaitType extends EventEmitter<
-			SubClassEvents & BaseConnectionEvents<BaseConnectionErrorType | ErrorType>
+		AwaitType extends EventEmitter<SubClassEvents>,
+		SubClassEvents extends BaseConnectionEvents<
+			BaseConnectionErrorType | ErrorType
 		>,
-		SubClassEvents extends ValidEventTypes,
 		ErrorType extends string = never,
 	>
 	extends EventEmitterWithPromise<
 		AwaitType,
 		never,
 		ErrorType | BaseConnectionErrorType,
-		SubClassEvents & BaseConnectionEvents<BaseConnectionErrorType | ErrorType>
+		SubClassEvents
 	>
 	implements IBaseConnection<SubClassEvents, ErrorType>
 {

+ 4 - 7
lib/dataconnection/DataConnection.ts

@@ -13,15 +13,12 @@ import {
 	IBaseConnection,
 } from "../baseconnection";
 import type { ServerMessage } from "../servermessage";
-import type { PromiseEvents } from "../peerError";
 import { randomToken } from "../utils/randomToken";
 
 export interface DataConnectionEvents
-	extends PromiseEvents<
-			never,
-			DataConnectionErrorType | BaseConnectionErrorType
-		>,
-		BaseConnectionEvents<DataConnectionErrorType | BaseConnectionErrorType> {
+	extends BaseConnectionEvents<
+		DataConnectionErrorType | BaseConnectionErrorType
+	> {
 	/**
 	 * Emitted when data is received from the remote peer.
 	 */
@@ -60,7 +57,7 @@ export abstract class DataConnection extends BaseConnection<
 	abstract readonly serialization: string;
 	readonly reliable: boolean;
 
-	public get type() {
+	public get type(): ConnectionType.Data {
 		return ConnectionType.Data;
 	}
 

+ 8 - 2
lib/eventEmitterWithPromise.ts

@@ -8,7 +8,7 @@ export class EventEmitterWithPromise<
 		ErrorType extends string,
 		Events extends PromiseEvents<OpenType, ErrorType>,
 	>
-	extends EventEmitter<Events | PromiseEvents<OpenType, ErrorType>, never>
+	extends EventEmitter<Events, never>
 	implements Promise<AwaitType>
 {
 	protected _open = false;
@@ -39,12 +39,14 @@ export class EventEmitterWithPromise<
 	): Promise<TResult1 | TResult2> {
 		const p = new Promise((resolve, reject) => {
 			const onOpen = () => {
+				// @ts-expect-error
 				this.off("error", onError);
 				// Remove 'then' to prevent potential recursion issues
 				// `await` will wait for a Promise-like to resolve recursively
 				resolve?.(proxyWithoutThen(this));
 			};
 			const onError = (err: PeerError<`${ErrorType}`>) => {
+				// @ts-expect-error
 				this.off("open", onOpen);
 				reject(err);
 			};
@@ -52,7 +54,10 @@ export class EventEmitterWithPromise<
 				onOpen();
 				return;
 			}
+
+			// @ts-expect-error
 			this.once("open", onOpen);
+			// @ts-expect-error
 			this.once("error", onError);
 		});
 		return p.then(onfulfilled, onrejected);
@@ -66,11 +71,12 @@ export class EventEmitterWithPromise<
 	emitError(type: ErrorType, err: string | Error): void {
 		logger.error("Error:", err);
 
+		// @ts-expect-error
 		this.emit("error", new PeerError<`${ErrorType}`>(`${type}`, err));
 	}
 }
 
-function proxyWithoutThen<T extends object>(obj: T) {
+function proxyWithoutThen<T extends object>(obj: T): Omit<T, "then"> {
 	return new Proxy(obj, {
 		get(target, p, receiver) {
 			if (p === "then") {

+ 8 - 4
lib/mediaconnection.ts

@@ -3,11 +3,15 @@ import logger from "./logger";
 import { Negotiator } from "./negotiator";
 import { ConnectionType, ServerMessageType } from "./enums";
 import type { Peer } from "./peer";
-import { BaseConnection, type BaseConnectionEvents } from "./baseconnection";
+import {
+	BaseConnection,
+	type BaseConnectionEvents,
+	IBaseConnection,
+} from "./baseconnection";
 import type { ServerMessage } from "./servermessage";
 import type { AnswerOption } from "./optionInterfaces";
 
-export interface MediaConnectionEvents extends BaseConnectionEvents<never> {
+export interface MediaConnectionEvents extends BaseConnectionEvents {
 	/**
 	 * Emitted when a connection to the PeerServer is established.
 	 *
@@ -25,7 +29,7 @@ export interface MediaConnectionEvents extends BaseConnectionEvents<never> {
 }
 
 export interface IMediaConnection
-	extends BaseConnection<IMediaConnection, MediaConnectionEvents> {
+	extends IBaseConnection<MediaConnectionEvents> {
 	get type(): ConnectionType.Media;
 	get localStream(): MediaStream;
 	get remoteStream(): MediaStream;
@@ -63,7 +67,7 @@ export class MediaConnection extends BaseConnection<
 	/**
 	 * For media connections, this is always 'media'.
 	 */
-	get type() {
+	get type(): ConnectionType.Media {
 		return ConnectionType.Media;
 	}
 

+ 7 - 3
lib/peer.ts

@@ -21,7 +21,7 @@ import { BinaryPack } from "./dataconnection/BufferedConnection/BinaryPack";
 import { Raw } from "./dataconnection/BufferedConnection/Raw";
 import { Json } from "./dataconnection/BufferedConnection/Json";
 
-import { PeerError } from "./peerError";
+import { PeerError, PromiseEvents } from "./peerError";
 import { EventEmitterWithPromise } from "./eventEmitterWithPromise";
 import EventEmitter from "eventemitter3";
 
@@ -80,7 +80,7 @@ export interface SerializerMapping {
 	) => DataConnection;
 }
 
-export interface PeerEvents {
+export interface PeerEvents extends PromiseEvents<string, PeerErrorType> {
 	/**
 	 * Emitted when a connection to the PeerServer is established.
 	 *
@@ -146,7 +146,11 @@ export interface IPeer extends EventEmitter<PeerEvents> {
 	 * @param stream The caller's media stream
 	 * @param options Metadata associated with the connection, passed in by whoever initiated the connection.
 	 */
-	call(peer: string, stream: MediaStream, options: CallOption): MediaConnection;
+	call(
+		peer: string,
+		stream: MediaStream,
+		options?: CallOption,
+	): MediaConnection;
 	/** Retrieve a data/media connection for this peer. */
 	getConnection(
 		peerId: string,