Skip to content

Commit

Permalink
refactor: improve event types
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasgloning committed May 13, 2022
1 parent 97a724b commit ce34b57
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
15 changes: 7 additions & 8 deletions lib/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ export enum ConnectionType {
Media = "media",
}

export enum PeerEventType {
Open = "open",
Close = "close",
Connection = "connection",
Call = "call",
Disconnected = "disconnected",
Error = "error",
}
export type PeerEventType =
| "open"
| "close"
| "connection"
| "call"
| "disconnected"
| "error";

export enum PeerErrorType {
BrowserIncompatible = "browser-incompatible",
Expand Down
29 changes: 19 additions & 10 deletions lib/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { DataConnection } from "./dataconnection";
import {
ConnectionType,
PeerErrorType,
PeerEventType,
SocketEventType,
ServerMessageType,
} from "./enums";
Expand All @@ -33,10 +32,18 @@ class PeerOptions implements PeerJSOption {
logFunction?: (logLevel: LogLevel, ...rest: any[]) => void;
}

type PeerEvents = {
open: (id: string) => void;
connection: (dataConnection: DataConnection) => void;
call: (mediaConnection: MediaConnection) => void;
close: () => void;
disconnected: (currentId: string) => void;
error: (error: Error) => void;
};
/**
* A peer who can initiate connections with other peers.
*/
export class Peer extends EventEmitter {
export class Peer extends EventEmitter<PeerEvents> {
private static readonly DEFAULT_KEY = "peerjs";

private readonly _options: PeerOptions;
Expand Down Expand Up @@ -232,7 +239,7 @@ export class Peer extends EventEmitter {
case ServerMessageType.Open: // The connection to the server is open.
this._lastServerId = this.id;
this._open = true;
this.emit(PeerEventType.Open, this.id);
this.emit("open", this.id);
break;
case ServerMessageType.Error: // Server error.
this._abort(PeerErrorType.ServerError, payload.msg);
Expand Down Expand Up @@ -271,24 +278,26 @@ export class Peer extends EventEmitter {

// Create a new connection.
if (payload.type === ConnectionType.Media) {
connection = new MediaConnection(peerId, this, {
const mediaConnection = new MediaConnection(peerId, this, {
connectionId: connectionId,
_payload: payload,
metadata: payload.metadata,
});
connection = mediaConnection;
this._addConnection(peerId, connection);
this.emit(PeerEventType.Call, connection);
this.emit("call", mediaConnection);
} else if (payload.type === ConnectionType.Data) {
connection = new DataConnection(peerId, this, {
const dataConnection = new DataConnection(peerId, this, {
connectionId: connectionId,
_payload: payload,
metadata: payload.metadata,
label: payload.label,
serialization: payload.serialization,
reliable: payload.reliable,
});
connection = dataConnection;
this._addConnection(peerId, connection);
this.emit(PeerEventType.Connection, connection);
this.emit("connection", dataConnection);
} else {
logger.warn(`Received malformed connection type:${payload.type}`);
return;
Expand Down Expand Up @@ -491,7 +500,7 @@ export class Peer extends EventEmitter {

error.type = type;

this.emit(PeerEventType.Error, error);
this.emit("error", error);
}

/**
Expand All @@ -512,7 +521,7 @@ export class Peer extends EventEmitter {

this._destroyed = true;

this.emit(PeerEventType.Close);
this.emit("close");
}

/** Disconnects every connection on this peer. */
Expand Down Expand Up @@ -559,7 +568,7 @@ export class Peer extends EventEmitter {
this._lastServerId = currentId;
this._id = null;

this.emit(PeerEventType.Disconnected, currentId);
this.emit("disconnected", currentId);
}

/** Attempts to reconnect with the same ID. */
Expand Down

0 comments on commit ce34b57

Please sign in to comment.