Skip to content

Commit 68f9e0d

Browse files
refactor: improve the types
1 parent c060d65 commit 68f9e0d

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

lib/socket.ts

+33-18
Original file line numberDiff line numberDiff line change
@@ -231,22 +231,26 @@ interface SocketReservedEvents {
231231
handshake: (data: HandshakeData) => void;
232232
packet: (packet: Packet) => void;
233233
packetCreate: (packet: Packet) => void;
234-
data: (data) => void;
235-
message: (data) => void;
234+
data: (data: RawData) => void;
235+
message: (data: RawData) => void;
236236
drain: () => void;
237237
flush: () => void;
238238
heartbeat: () => void;
239239
ping: () => void;
240240
pong: () => void;
241241
error: (err: string | Error) => void;
242-
upgrading: (transport) => void;
243-
upgrade: (transport) => void;
242+
upgrading: (transport: Transport) => void;
243+
upgrade: (transport: Transport) => void;
244244
upgradeError: (err: Error) => void;
245245
close: (reason: string, description?: CloseDetails | Error) => void;
246246
}
247247

248248
type SocketState = "opening" | "open" | "closing" | "closed";
249249

250+
interface WriteOptions {
251+
compress?: boolean;
252+
}
253+
250254
export class Socket extends Emitter<
251255
Record<never, never>,
252256
Record<never, never>,
@@ -407,7 +411,7 @@ export class Socket extends Emitter<
407411
* @return {Transport}
408412
* @private
409413
*/
410-
private createTransport(name) {
414+
private createTransport(name: string) {
411415
debug('creating transport "%s"', name);
412416
const query: any = Object.assign({}, this.opts.query);
413417

@@ -481,7 +485,7 @@ export class Socket extends Emitter<
481485
*
482486
* @private
483487
*/
484-
private setTransport(transport) {
488+
private setTransport(transport: Transport) {
485489
debug("setting transport %s", transport.name);
486490

487491
if (this.transport) {
@@ -506,7 +510,7 @@ export class Socket extends Emitter<
506510
* @param {String} name - transport name
507511
* @private
508512
*/
509-
private probe(name) {
513+
private probe(name: string) {
510514
debug('probing transport "%s"', name);
511515
let transport = this.createTransport(name);
512516
let failed = false;
@@ -654,7 +658,7 @@ export class Socket extends Emitter<
654658
*
655659
* @private
656660
*/
657-
private onPacket(packet) {
661+
private onPacket(packet: Packet) {
658662
if (
659663
"opening" === this.readyState ||
660664
"open" === this.readyState ||
@@ -808,15 +812,23 @@ export class Socket extends Emitter<
808812
*
809813
* @param {String} msg - message.
810814
* @param {Object} options.
811-
* @param {Function} callback function.
815+
* @param {Function} fn - callback function.
812816
* @return {Socket} for chaining.
813817
*/
814-
public write(msg: RawData, options?, fn?) {
818+
public write(msg: RawData, options?: WriteOptions, fn?: () => void) {
815819
this.sendPacket("message", msg, options, fn);
816820
return this;
817821
}
818822

819-
public send(msg: RawData, options?, fn?) {
823+
/**
824+
* Sends a message. Alias of {@link Socket#write}.
825+
*
826+
* @param {String} msg - message.
827+
* @param {Object} options.
828+
* @param {Function} fn - callback function.
829+
* @return {Socket} for chaining.
830+
*/
831+
public send(msg: RawData, options?: WriteOptions, fn?: () => void) {
820832
this.sendPacket("message", msg, options, fn);
821833
return this;
822834
}
@@ -830,7 +842,12 @@ export class Socket extends Emitter<
830842
* @param {Function} fn - callback function.
831843
* @private
832844
*/
833-
private sendPacket(type: PacketType, data?: RawData, options?, fn?) {
845+
private sendPacket(
846+
type: PacketType,
847+
data?: RawData,
848+
options?: WriteOptions,
849+
fn?: () => void
850+
) {
834851
if ("function" === typeof data) {
835852
fn = data;
836853
data = undefined;
@@ -851,7 +868,7 @@ export class Socket extends Emitter<
851868
const packet = {
852869
type: type,
853870
data: data,
854-
options: options,
871+
options: options as { compress: boolean },
855872
};
856873
this.emitReserved("packetCreate", packet);
857874
this.writeBuffer.push(packet);
@@ -907,7 +924,7 @@ export class Socket extends Emitter<
907924
*
908925
* @private
909926
*/
910-
private onError(err) {
927+
private onError(err: Error) {
911928
debug("socket error %j", err);
912929
Socket.priorWebsocketSuccess = false;
913930
this.emitReserved("error", err);
@@ -970,11 +987,9 @@ export class Socket extends Emitter<
970987
* @param {Array} upgrades - server upgrades
971988
* @private
972989
*/
973-
private filterUpgrades(upgrades) {
990+
private filterUpgrades(upgrades: string[]) {
974991
const filteredUpgrades = [];
975-
let i = 0;
976-
const j = upgrades.length;
977-
for (; i < j; i++) {
992+
for (let i = 0; i < upgrades.length; i++) {
978993
if (~this.transports.indexOf(upgrades[i]))
979994
filteredUpgrades.push(upgrades[i]);
980995
}

0 commit comments

Comments
 (0)