-
Notifications
You must be signed in to change notification settings - Fork 98
Implement Request Batches with callBatch
#306
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
Implement Request Batches with callBatch
#306
Conversation
export type RequestBatchRequest<T = keyof OBSRequestTypes> = T extends keyof OBSRequestTypes ? OBSRequestTypes[T] extends never ? { | ||
requestType: T; | ||
requestId?: string; | ||
} : { | ||
requestType: T; | ||
requestId?: string; | ||
requestData: OBSRequestTypes[T]; | ||
} : never; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels really dirty, but without it, requests that have never
as the requestData
value still want a requestData
parameter, which is an impossible constraint to satisfy. So this is the compromise.
I published [`@faulty/obs-websocket-js`](https://www.npmjs.com/package/@faulty/obs-websocket-js) with the changes made to implement request batches, and also [opened a PR](obs-websocket-community-projects/obs-websocket-js#306) to get the feature merged into an official release. But with the first step done, we no longer need a vendored copy of the library and can skip a lot of the build time it was taking up as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also update readme.md, especially include the typing caveat
Related Issue (if applicable):
#292
Description:
This PR implements a
callBatch
function that allows consumers to use theRequestBatch
functionality from obs-websocket.RequestBatch
was already part of the generated types (and in the protocol), so this doesn't change anything other than adding the function and the related types.Because of the granularity of obs-websocket-v5's protocol design, getting the entire state of an input often requires multiple requests, and then multiple of those groups of requests for each input that you want to know about (example, just getting some audio-related state for all relevant inputs). In the context of any meaningfully-sized OBS setup, that could mean hundreds of requests in succession every time you want to refetch the state of OBS. Especially when the OBS host and the websocket client are not on the same computer (or are potentially even on different local networks), the latency of sending individual requests also adds up quickly.
Request batches are a really useful feature to avoid all of these issues and instead group requests into much more manageable and relevant bundles. Batches can contain large numbers of requests that obs-websocket will run when possible and then return all of the results together, saving on back and forth round trip times and encoding overhead.
I needed this feature for an event I was running a few months ago under exactly those circumstances and ended up implementing it myself using a vendored copy of obs-websocket-js, so this PR is largely just bringing those changes back here so that I can ditch the vendored copy and come back to using an official release.
Limitations
I tried for a while to get some smart typing for
callBatch
, like knowing what specificOBSRequestTypes
would be in each slot of the returned array ofresults
. It feels like there's some way to use mapped tuple types, but I couldn't figure it out with the way theOBSRequestTypes
andOBSResponseTypes
are currently structured as a single interface.So, instead, consumers will currently need to cast each result to the type they are expecting, like: