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

Conversation

engina
Copy link
Contributor

@engina engina commented Jan 13, 2023

Attempts to fix #1555

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behaviour

New behaviour

Other information (e.g. related issues)

@engina
Copy link
Contributor Author

engina commented Jan 14, 2023

I noticed an issue engina#1

Also fixed with the help of systemfault from irc.libera.chat/typescript

darrachequesne pushed a commit that referenced this pull request Jan 16, 2023
When emitting with a timeout (added in version 4.4.0), the "err"
argument was not properly typed and would require to split the client
and server typings. It will now be automatically inferred as an Error
object.

Workaround for previous versions:

```ts
type WithTimeoutAck<isEmitter extends boolean, args extends any[]> = isEmitter extends true ? [Error, ...args] : args;

interface ClientToServerEvents<isEmitter extends boolean = false> {
    withAck: (data: { argName: boolean }, callback: (...args: WithTimeoutAck<isEmitter, [string]>) => void) => void;
}

interface ServerToClientEvents<isEmitter extends boolean = false> {

}

const io = new Server<ClientToServerEvents, ServerToClientEvents<true>>(3000);

io.on("connection", (socket) => {
    socket.on("withAck", (val, cb) => {
        cb("123");
    });
});

const socket: Socket<ServerToClientEvents, ClientToServerEvents<true>> = ioc("http://localhost:3000");

socket.timeout(100).emit("withAck", { argName: true }, (err, val) => {
  // ...
});
```

Related: #1555
@darrachequesne
Copy link
Member

Merged as 33e4172. Thanks a lot ❤️

@engina
Copy link
Contributor Author

engina commented Jan 16, 2023

Please see engina#1

Merged solution is suboptimal -- it loses argument labels. Perfect typing solution is proposed in the link and forked repo -- but it requires a newer tsc it seems. I tried to clean up a pull request for that too, but I got lost in build system intricacies and gave up.

@darrachequesne
Copy link
Member

@engina arf, it seems you are right:

Screenshot from 2023-01-17 09-03-42

I tried to fix the behavior with an untyped socket (which defaults to DefaultEventsMap), and lost the labels along the way...

Screenshot from 2023-01-17 09-06-12

If you have any pointer 😇

darrachequesne added a commit that referenced this pull request Jan 17, 2023
This follows [1], in order to keep the label of each argument.

[1]: 33e4172

Related:

- #1570 (comment)
- microsoft/TypeScript#39941
- microsoft/TypeScript#48049
@engina
Copy link
Contributor Author

engina commented Jan 18, 2023

@darrachequesne This gives perfect typings: https://github.com/engina/socket.io-client/blob/c8ff4b6351ec575ecf3dbcd4c487aa3c5283795e/lib/socket.ts

But your build system (tsd?) doesn't like it. So I started upgrading tsc (wrongfully i guess) but then things started to get hairy. And at this point I was exhausted :)

I would like to look at that again but very busy with work now.

My suggestion:

  1. You could try merging JUST the socket.ts I linked above.
  2. Verify typings work. (They worked perfectly in VS code for me)
  3. Make it build and pass the tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Emitting an event with acknowledgement and timeout breaks the Typescript client-to-server event definitions
2 participants