Skip to content

Commit 1465c3c

Browse files
ikwilnaarhuismanrickrick
authored
chore: make callService wrap callServiceAsync instead of around (#1173)
* chore: instead of wrapping callService for callServiceAsync, make callService wrap callServiceAsync. * chore: made the code more robust by adding rejection promises. --------- Co-authored-by: rick <rick@rick.rick> Co-authored-by: rick <rick@rick.nl>
1 parent 3e13969 commit 1465c3c

1 file changed

Lines changed: 32 additions & 37 deletions

File tree

packages/roslib/src/core/Service.ts

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ export default class Service<
5757
this.serviceType = serviceType;
5858
}
5959
/**
60-
* Call the service. Returns the service response in the
61-
* callback. Does nothing if this service is currently advertised.
60+
* Wrapper of callServiceAsync for synchronous use. Returns the service response in the
61+
* callback.
62+
* @see callService
6263
*
6364
* @param request - The service request to send.
6465
* @param [callback] - Function with the following params:
@@ -72,50 +73,44 @@ export default class Service<
7273
failedCallback: (error: string) => void = console.error,
7374
timeout?: number,
7475
): void {
75-
if (this.isAdvertised) {
76-
return;
77-
}
78-
79-
const serviceCallId = `call_service:${this.name}:${uuidv4()}`;
80-
81-
this.ros.once(serviceCallId, function (message) {
82-
if (isRosbridgeServiceResponseMessage<TResponse>(message)) {
83-
if (!message.result) {
84-
failedCallback(message.values ?? "");
85-
} else {
86-
callback?.(message.values);
87-
}
88-
}
89-
});
90-
91-
this.ros.callOnConnection({
92-
op: "call_service",
93-
id: serviceCallId,
94-
service: this.name,
95-
args: request,
96-
timeout: timeout,
97-
});
76+
this.callServiceAsync(request, timeout).then(callback, failedCallback);
9877
}
9978

10079
/**
101-
* Wrapper of callService that returns a modern Promise-based interface for use with async/await.
102-
* @see callService
80+
* Call the service. Returns the service response in the form of a Promise.
81+
* Returns a rejected promise if the service is currently advertised.
10382
* @param request - The service request to send.
10483
* @param [timeout] - Optional timeout, in seconds, for the service call. A non-positive value means no timeout.
10584
* If not provided, the rosbridge server will use its default value.
10685
*/
10786
callServiceAsync(request: TRequest, timeout?: number): Promise<TResponse> {
87+
if (this.isAdvertised) {
88+
return Promise.reject(new Error("Service is currently advertised"));
89+
}
90+
10891
return new Promise<TResponse>((resolve, reject) => {
109-
this.callService(
110-
request,
111-
(res) => {
112-
resolve(res);
113-
},
114-
(err) => {
115-
reject(new Error(err));
116-
},
117-
timeout,
118-
);
92+
const serviceCallId = `call_service:${this.name}:${uuidv4()}`;
93+
94+
this.ros.once(serviceCallId, (message) => {
95+
if (!isRosbridgeServiceResponseMessage<TResponse>(message)) {
96+
reject(new Error("Received an invalid service response message"));
97+
return;
98+
}
99+
100+
if (message.result) {
101+
resolve(message.values);
102+
} else {
103+
reject(new Error(message.values ?? ""));
104+
}
105+
});
106+
107+
this.ros.callOnConnection({
108+
op: "call_service",
109+
id: serviceCallId,
110+
service: this.name,
111+
args: request,
112+
timeout: timeout,
113+
});
119114
});
120115
}
121116

0 commit comments

Comments
 (0)