nathanboktae / robust-websocket

A robust reconnecting WebSocket client for the browser
ISC License
153 stars 24 forks source link

Add TypeScript definition; allow url argument to be an async function #9

Closed finom closed 2 years ago

finom commented 2 years ago

The PR consists two changes:

  const stream = new RobustWebSocket(async function getUrl(): Promise<string> {
      try {
        const { listenKey } = await api.futuresUserDataStream('POST');
        return `${options.accountStreamURL}${listenKey}`;
      } catch (e) {
        console.error(e);
        await delay(5_000);

        return getUrl();
      }
    });

I have published the change on NPM to use the library while this PR is waiting to be approved: https://www.npmjs.com/package/altamoon-robust-websocket.

Thank you for your work!

nathanboktae commented 2 years ago

Thanks for the PR. Happy to take the TypeScript definition file.

However, for the async changes, it a) forces the function to be async always, and not allow sync; this is a breaking change. b) requires ESNext, as currently this library is vanilla ES5.

You can fix both by allowing getUrl to return string | Promise<string> and use standard Promise APIs. If you do that, please update the documentation, add a test, and I'll be glad to take the PR.

finom commented 2 years ago

@nathanboktae thank you for quick response. Unfortunately that's a lot of work and I we don't have time allocated on that, sorry. We're going to use the fork 😬 But feel free to borrow our changes 😅