Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When emitting from timeout() prepend timeout error #1570

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dist/socket.io.esm.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/socket.io.esm.min.js.map

Large diffs are not rendered by default.

720 changes: 713 additions & 7 deletions dist/socket.io.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/socket.io.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/socket.io.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/socket.io.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/socket.io.msgpack.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/socket.io.msgpack.min.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { url } from "./url.js";
import { Manager, ManagerOptions } from "./manager.js";
import { Socket, SocketOptions } from "./socket.js";
import { url } from "./url";
import { Manager, ManagerOptions } from "./manager";
import { Socket, SocketOptions } from "./socket";
import debugModule from "debug"; // debug()

const debug = debugModule("socket.io-client"); // debug()
Expand Down
6 changes: 3 additions & 3 deletions lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
installTimerFunctions,
nextTick,
} from "engine.io-client";
import { Socket, SocketOptions, DisconnectDescription } from "./socket.js";
import { Socket, SocketOptions, DisconnectDescription } from "./socket";
import * as parser from "socket.io-parser";
import { Decoder, Encoder, Packet } from "socket.io-parser";
import { on } from "./on.js";
import { Backoff } from "./contrib/backo2.js";
import { on } from "./on";
import { Backoff } from "./contrib/backo2";
import {
DefaultEventsMap,
EventsMap,
Expand Down
48 changes: 45 additions & 3 deletions lib/socket.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Packet, PacketType } from "socket.io-parser";
import { on } from "./on.js";
import { Manager } from "./manager.js";
import { on } from "./on";
import { Manager } from "./manager";
import {
DefaultEventsMap,
EventNames,
Expand All @@ -12,6 +12,46 @@ import debugModule from "debug"; // debug()

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

/**
* Utulity type to extract the last element of a tuple.
*/
type Last<T extends any[]> = T["length"] extends 1
? T
: T extends [infer H, ...infer Rest]
? Last<Rest>
: [];
/**
* Utility type to extract all elements of a tuple except the last one.
*/
type AllButLast<T extends any[]> = T extends [...infer I, infer L] ? I : never;

/**
* Utility type to prepend the timeout error to a parameter list.
*/
type PrependTimeoutError<T extends any[]> = {
[K in keyof T]: T[K] extends (...args: infer Params) => infer Result
? (timeoutErr: Error, ...args: Params) => Result
: T[K];
};

/**
* Utility type to decorate callbacks of each method in an interface with a timeout error.
*/
type DecorateCallbacksWithTimeoutError<T> = {
[K in keyof T]: T[K] extends (...args: infer Params) => infer Result
? (
...args: [
...AllButLast<Params>,
...(PrependTimeoutError<
Last<Params>
> extends infer Z extends readonly unknown[]
? Z
: never)
]
) => Result
: T[K];
};

export interface SocketOptions {
/**
* the authentication payload sent when connecting to the Namespace
Expand Down Expand Up @@ -677,7 +717,9 @@ export class Socket<
*
* @returns self
*/
public timeout(timeout: number): this {
public timeout(
timeout: number
): Socket<ListenEvents, DecorateCallbacksWithTimeoutError<EmitEvents>> {
this.flags.timeout = timeout;
return this;
}
Expand Down
118 changes: 14 additions & 104 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@rollup/plugin-node-resolve": "^13.0.5",
"@sinonjs/fake-timers": "^7.1.2",
"@types/mocha": "^9.0.0",
"@types/node": "^16.7.6",
"@types/node": "^18.11.18",
"@types/sinonjs__fake-timers": "^6.0.3",
"@wdio/cli": "^7.26.0",
"@wdio/local-runner": "^7.26.0",
Expand All @@ -72,7 +72,7 @@
"ts-loader": "^8.3.0",
"ts-node": "^10.2.1",
"tsd": "^0.17.0",
"typescript": "^4.4.2"
"typescript": "^4.9.4"
},
"scripts": {
"compile": "rimraf ./build && tsc && tsc -p tsconfig.esm.json && ./postcompile.sh",
Expand Down
Loading