Skip to content
Merged
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
33 changes: 33 additions & 0 deletions src/core/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,37 @@ export default class Service extends EventEmitter {

this.ros.callOnConnection(call);
}

/**
* An alternate form of Service advertisement that supports a modern Promise-based interface for use with async/await.
* @param {(request: TRequest) => Promise<TResponse>} callback An asynchronous callback processing the request and returning a response.
*/
advertiseAsync(callback) {
if (this.isAdvertised) {
throw new Error('Cannot advertise the same Service twice!');
}
Comment on lines +135 to +137
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should move toward this behavior in all of the cases throughout the library where there's currently guard conditions that just early-return with no information. Users shouldn't be calling advertise multiple times on the same Service object, that's probably an issue with their code!

I don't want to break that in existing functions yet, but figured I'd do it here where we have a green field.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have not released any v2 version yet. So I think we can fix it everywhere.

this.ros.on(this.name, async (rosbridgeRequest) => {
/** @type {{op: string, service: string, values?: TResponse, result: boolean, id?: string}} */
let rosbridgeResponse = {
op: 'service_response',
service: this.name,
result: false
}
try {
rosbridgeResponse.values = await callback(rosbridgeRequest.args);
rosbridgeResponse.result = true;
} finally {
if (rosbridgeRequest.id) {
rosbridgeResponse.id = rosbridgeRequest.id;
}
this.ros.callOnConnection(rosbridgeResponse);
}
});
this.ros.callOnConnection({
op: 'advertise_service',
type: this.serviceType,
service: this.name
});
this.isAdvertised = true;
}
}
28 changes: 28 additions & 0 deletions test/service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { it, describe, expect } from 'vitest';
import { Service, Ros } from '../';

describe('Service', () => {
const ros = new Ros({
url: 'ws://localhost:9090'
});
it('Successfully advertises a service with an async return', async () => {
const server = new Service({
ros,
serviceType: 'std_srvs/Trigger',
name: '/test_service'
});
server.advertiseAsync(async () => {
return {
success: true,
message: 'foo'
}
});
const client = new Service({
ros,
serviceType: 'std_srvs/Trigger',
name: '/test_service'
})
const response = await new Promise((resolve, reject) => client.callService({}, resolve, reject));
expect(response).toEqual({success: true, message: 'foo'});
})
})