- 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
Merged
t2t2 merged 3 commits into obs-websocket-community-projects:master from faultyserver:add-request-batch Sep 30, 2022
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -252,6 +252,10 @@ export interface IncomingMessageTypes { | |
| * obs-websocket is responding to a request coming from a client | ||
| */ | ||
| [WebSocketOpCode.RequestResponse]: ResponseMessage; | ||
| /** | ||
| * obs-websocket is responding to a batch request coming from a client | ||
| */ | ||
| [WebSocketOpCode.RequestBatchResponse]: ResponseBatchMessage; | ||
| } | ||
| | ||
| export interface OutgoingMessageTypes { | ||
| | @@ -285,6 +289,10 @@ export interface OutgoingMessageTypes { | |
| * Client is making a request to obs-websocket. Eg get current scene, create source. | ||
| */ | ||
| [WebSocketOpCode.Request]: RequestMessage; | ||
| /** | ||
| * Client is making a batch request to obs-websocket. | ||
| */ | ||
| [WebSocketOpCode.RequestBatch]: RequestBatchMessage; | ||
| } | ||
| | ||
| type EventMessage<T = keyof OBSEventTypes> = T extends keyof OBSEventTypes ? { | ||
| | @@ -302,13 +310,34 @@ export type RequestMessage<T = keyof OBSRequestTypes> = T extends keyof OBSReque | |
| requestData: OBSRequestTypes[T]; | ||
| } : never; | ||
| | ||
| 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; | ||
| Comment on lines +313 to +320 Contributor Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels really dirty, but without it, requests that have | ||
| | ||
| export type RequestBatchMessage = { | ||
| requestId: string; | ||
| haltOnFailure?: boolean; | ||
| executionType?: RequestBatchExecutionType; | ||
| requests: RequestBatchRequest[]; | ||
| }; | ||
| | ||
| export type ResponseMessage<T = keyof OBSResponseTypes> = T extends keyof OBSResponseTypes ? { | ||
| requestType: T; | ||
| requestId: string; | ||
| requestStatus: {result: true; code: number} | {result: false; code: number; comment: string}; | ||
| responseData: OBSResponseTypes[T]; | ||
| } : never; | ||
| | ||
| export type ResponseBatchMessage = { | ||
| requestId: string; | ||
| results: ResponseMessage[]; | ||
| }; | ||
| | ||
| // Events | ||
| export interface OBSEventTypes { | ||
| CurrentSceneCollectionChanging: { | ||
| | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import anyTest, {TestFn} from 'ava'; | ||
| | ||
| import {makeServer, MockServer} from './helpers/dev-server.js'; | ||
| import OBSWebSocket, {OBSRequestTypes, OBSResponseTypes, OBSWebSocketError} from '../src/json.js'; | ||
| | ||
| const test = anyTest as TestFn<{ | ||
| server: MockServer; | ||
| client: OBSWebSocket; | ||
| }>; | ||
| | ||
| test.beforeEach(async t => { | ||
| const server = await makeServer(); | ||
| const client = new OBSWebSocket(); | ||
| await client.connect(server.url); | ||
| | ||
| t.context = { | ||
| server, | ||
| client, | ||
| }; | ||
| }); | ||
| | ||
| test.afterEach(async t => { | ||
| await t.context.client.disconnect(); | ||
| await t.context.server.teardown(); | ||
| }); | ||
| | ||
| test('disconencted throws', async t => { | ||
| const {client} = t.context; | ||
| await client.disconnect(); | ||
| await t.throwsAsync(client.callBatch([{requestType: 'GetVersion'}]), { | ||
| instanceOf: Error, | ||
| message: 'Not connected', | ||
| }); | ||
| }); | ||
| | ||
| test('single request without parameters', async t => { | ||
| const {client} = t.context; | ||
| const [res] = await client.callBatch([{requestType: 'GetVersion'}]); | ||
| | ||
| t.is((res.responseData as OBSResponseTypes['GetVersion']).obsVersion, '5.0.0-mock.0'); | ||
| }); | ||
| | ||
| test('multiple requests with mixed parameters', async t => { | ||
| const {client} = t.context; | ||
| const [res1, res2, res3] = await client.callBatch([ | ||
| {requestType: 'GetVersion'}, | ||
| {requestType: 'BroadcastCustomEvent', requestData: {eventData: {}}}, | ||
| {requestType: 'GetVersion'}, | ||
| ]); | ||
| | ||
| t.is((res1.responseData as OBSResponseTypes['GetVersion']).obsVersion, '5.0.0-mock.0'); | ||
| t.is((res2.responseData as OBSResponseTypes['BroadcastCustomEvent']), undefined); | ||
| t.is((res3.responseData as OBSResponseTypes['GetVersion']).obsVersion, '5.0.0-mock.0'); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.