Skip to content

Commit

Permalink
chore: bump @socket.io/component-emitter to version 3.0.0
Browse files Browse the repository at this point in the history
The typed events have been moved from [1] to [2] in order to remove
the intermediary class and reduce the bundle size.

Diff: socketio/emitter@2.0.0...3.0.0

[1]: https://github.com/socketio/socket.io-client
[2]: https://github.com/socketio/emitter/
  • Loading branch information
darrachequesne committed Oct 14, 2021
1 parent 1524413 commit b3bb73a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 28 deletions.
59 changes: 39 additions & 20 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { installTimerFunctions } from "./util.js";
import parseqs from "parseqs";
import parseuri from "parseuri";
import debugModule from "debug"; // debug()
import Emitter from "@socket.io/component-emitter";
import { Emitter } from "@socket.io/component-emitter";
import { protocol } from "engine.io-parser";

const debug = debugModule("engine.io-client:socket"); // debug()
Expand Down Expand Up @@ -214,7 +214,26 @@ export interface SocketOptions {
protocols: string | string[];
}

export class Socket extends Emitter {
interface SocketReservedEvents {
open: () => void;
handshake: (data) => void;
packet: (packet) => void;
packetCreate: (packet) => void;
data: (data) => void;
message: (data) => void;
drain: () => void;
flush: () => void;
heartbeat: () => void;
ping: () => void;
pong: () => void;
error: (err: string | Error) => void;
upgrading: (transport) => void;
upgrade: (transport) => void;
upgradeError: (err: Error) => void;
close: (reason: string, desc?: Error) => void;
}

export class Socket extends Emitter<{}, {}, SocketReservedEvents> {
public id: string;
public transport: any;
public binaryType: string;
Expand Down Expand Up @@ -408,7 +427,7 @@ export class Socket extends Emitter {
} else if (0 === this.transports.length) {
// Emit error on next tick so it can be listened to
this.setTimeoutFn(() => {
this.emit("error", "No transports available");
this.emitReserved("error", "No transports available");
}, 0);
return;
} else {
Expand Down Expand Up @@ -479,7 +498,7 @@ export class Socket extends Emitter {
if ("pong" === msg.type && "probe" === msg.data) {
debug('probe transport "%s" pong', name);
this.upgrading = true;
this.emit("upgrading", transport);
this.emitReserved("upgrading", transport);
if (!transport) return;
Socket.priorWebsocketSuccess = "websocket" === transport.name;

Expand All @@ -493,7 +512,7 @@ export class Socket extends Emitter {

this.setTransport(transport);
transport.send([{ type: "upgrade" }]);
this.emit("upgrade", transport);
this.emitReserved("upgrade", transport);
transport = null;
this.upgrading = false;
this.flush();
Expand All @@ -503,7 +522,7 @@ export class Socket extends Emitter {
const err = new Error("probe error");
// @ts-ignore
err.transport = transport.name;
this.emit("upgradeError", err);
this.emitReserved("upgradeError", err);
}
});
};
Expand All @@ -530,7 +549,7 @@ export class Socket extends Emitter {

debug('probe transport "%s" failed because of error: %s', name, err);

this.emit("upgradeError", error);
this.emitReserved("upgradeError", error);
};

function onTransportClose() {
Expand Down Expand Up @@ -578,7 +597,7 @@ export class Socket extends Emitter {
debug("socket open");
this.readyState = "open";
Socket.priorWebsocketSuccess = "websocket" === this.transport.name;
this.emit("open");
this.emitReserved("open");
this.flush();

// we check for `readyState` in case an `open`
Expand Down Expand Up @@ -610,10 +629,10 @@ export class Socket extends Emitter {
) {
debug('socket receive: type "%s", data "%s"', packet.type, packet.data);

this.emit("packet", packet);
this.emitReserved("packet", packet);

// Socket is live - any packet counts
this.emit("heartbeat");
this.emitReserved("heartbeat");

switch (packet.type) {
case "open":
Expand All @@ -623,8 +642,8 @@ export class Socket extends Emitter {
case "ping":
this.resetPingTimeout();
this.sendPacket("pong");
this.emit("ping");
this.emit("pong");
this.emitReserved("ping");
this.emitReserved("pong");
break;

case "error":
Expand All @@ -635,8 +654,8 @@ export class Socket extends Emitter {
break;

case "message":
this.emit("data", packet.data);
this.emit("message", packet.data);
this.emitReserved("data", packet.data);
this.emitReserved("message", packet.data);
break;
}
} else {
Expand All @@ -651,7 +670,7 @@ export class Socket extends Emitter {
* @api private
*/
private onHandshake(data) {
this.emit("handshake", data);
this.emitReserved("handshake", data);
this.id = data.sid;
this.transport.query.sid = data.sid;
this.upgrades = this.filterUpgrades(data.upgrades);
Expand Down Expand Up @@ -692,7 +711,7 @@ export class Socket extends Emitter {
this.prevBufferLen = 0;

if (0 === this.writeBuffer.length) {
this.emit("drain");
this.emitReserved("drain");
} else {
this.flush();
}
Expand All @@ -715,7 +734,7 @@ export class Socket extends Emitter {
// keep track of current length of writeBuffer
// splice writeBuffer and callbackBuffer on `drain`
this.prevBufferLen = this.writeBuffer.length;
this.emit("flush");
this.emitReserved("flush");
}
}

Expand Down Expand Up @@ -770,7 +789,7 @@ export class Socket extends Emitter {
data: data,
options: options
};
this.emit("packetCreate", packet);
this.emitReserved("packetCreate", packet);
this.writeBuffer.push(packet);
if (fn) this.once("flush", fn);
this.flush();
Expand Down Expand Up @@ -829,7 +848,7 @@ export class Socket extends Emitter {
private onError(err) {
debug("socket error %j", err);
Socket.priorWebsocketSuccess = false;
this.emit("error", err);
this.emitReserved("error", err);
this.onClose("transport error", err);
}

Expand Down Expand Up @@ -869,7 +888,7 @@ export class Socket extends Emitter {
this.id = null;

// emit close event
this.emit("close", reason, desc);
this.emitReserved("close", reason, desc);

// clean buffers after, so users can still
// grab the buffers on `close` event
Expand Down
7 changes: 5 additions & 2 deletions lib/transport.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { decodePacket } from "engine.io-parser";
import Emitter from "@socket.io/component-emitter";
import { DefaultEventsMap, Emitter } from "@socket.io/component-emitter";
import { installTimerFunctions } from "./util.js";
import debugModule from "debug"; // debug()
import { SocketOptions } from "./socket.js";

const debug = debugModule("engine.io-client:transport"); // debug()

export abstract class Transport extends Emitter {
export abstract class Transport extends Emitter<
DefaultEventsMap,
DefaultEventsMap
> {
protected opts: SocketOptions;
protected supportsBinary: boolean;
protected query: object;
Expand Down
4 changes: 2 additions & 2 deletions lib/transports/polling-xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as XMLHttpRequestModule from "xmlhttprequest-ssl";
import debugModule from "debug"; // debug()
import globalThis from "../globalThis.js";
import { installTimerFunctions, pick } from "../util.js";
import Emitter from "@socket.io/component-emitter";
import { DefaultEventsMap, Emitter } from "@socket.io/component-emitter";
import { Polling } from "./polling.js";
import { SocketOptions } from "../socket.js";

Expand Down Expand Up @@ -106,7 +106,7 @@ export class XHR extends Polling {
}
}

export class Request extends Emitter {
export class Request extends Emitter<DefaultEventsMap, DefaultEventsMap> {
private readonly opts: { xd; xs } & SocketOptions;
private readonly method: string;
private readonly uri: string;
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
}
],
"dependencies": {
"@socket.io/component-emitter": "~2.0.0",
"@socket.io/component-emitter": "~3.0.0",
"debug": "~4.3.1",
"engine.io-parser": "~5.0.0",
"has-cors": "1.1.0",
Expand Down

0 comments on commit b3bb73a

Please sign in to comment.