Skip to content

Commit c595053

Browse files
wayneparrottMinggang Wang
authored andcommitted
Added typescript typings to support serialized pub-sub messages.
* index.js - converted `createMessageObject()` to constrained generic returning less ambiguous MessageType<T> instead of Message. * node.js - added isRaw to `_validateOption()` * node.d.ts - converted `createPublisher()` & `createSubscription()` & `deletePublisher()` to generic constrained to MessageTypeClassName. Improves type inferencing. Similarly modified SubscriptionCallback. * package.json - refactor dtslint test stage of the `test` script to `test-ts` script and upgraded dtslint to version 4.0.4 * publisher.d.ts - converted Publisher class to generic constrained to MessageTypeClassName. * test/type/index.d.ts - updated minimum typescript version for dtslint to 3.9 * test/type/main.ts - added tests
1 parent d53992b commit c595053

File tree

7 files changed

+61
-22
lines changed

7 files changed

+61
-22
lines changed

lib/node.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,11 @@ class Node {
371371
}
372372

373373
if (options === undefined) {
374-
options = { enableTypedArray: true, qos: QoS.profileDefault };
374+
options = {
375+
enableTypedArray: true,
376+
isRaw: false,
377+
qos: QoS.profileDefault,
378+
};
375379
return options;
376380
}
377381

@@ -382,6 +386,11 @@ class Node {
382386
if (options.qos === undefined) {
383387
options = Object.assign(options, { qos: QoS.profileDefault });
384388
}
389+
390+
if (options.isRaw === undefined) {
391+
options = Object.assign(options, { isRaw: false });
392+
}
393+
385394
return options;
386395
}
387396

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"scripts": {
1616
"install": "node-gyp rebuild",
1717
"docs": "cd docs && make",
18-
"test": "node ./scripts/compile_tests.js && node --expose-gc ./scripts/run_test.js && node ./scripts/generate_tsd.js && dtslint test/types",
18+
"test": "node ./scripts/compile_tests.js && node --expose-gc ./scripts/run_test.js && npm run dtslint",
19+
"dtslint": "node ./scripts/generate_tsd.js && dtslint test/types",
1920
"lint": "eslint --max-warnings=0 index.js types/*.d.ts scripts lib example rosidl_gen rosidl_parser test benchmark/rclnodejs && node ./scripts/cpplint.js",
2021
"postinstall": "node scripts/generate_messages.js",
2122
"format": "clang-format -i -style=file ./src/*.cpp ./src/*.hpp && prettier --trailing-comma es5 --single-quote --write \"{.,{lib,rosidl_gen,rostsd_gen,rosidl_parser,types,example,test}/**}/*.{js,md,ts}\""
@@ -37,7 +38,7 @@
3738
"clang-format": "^1.4.0",
3839
"commander": "^6.0.0",
3940
"deep-equal": "^1.1.1",
40-
"dtslint": "^3.6.13",
41+
"dtslint": "^4.0.4",
4142
"eslint": "^7.5.0",
4243
"eslint-config-prettier": "^6.11.0",
4344
"husky": "^4.2.5",

test/types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
// TypeScript Version: 3.7
1+
// Minimum TypeScript Version: 3.9
2+

test/types/main.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import * as rclnodejs from 'rclnodejs';
44
const NODE_NAME = 'test_node';
55
const TYPE_CLASS = 'std_msgs/msg/String';
66
const TOPIC = 'topic';
7+
const MSG = rclnodejs.createMessageObject(TYPE_CLASS);
8+
MSG.data = '';
79

810
// ---- rclnodejs -----
911
// $ExpectType Promise<void>
@@ -85,7 +87,7 @@ node.countPublishers(TOPIC);
8587
node.countSubscribers(TOPIC);
8688

8789
// ---- Publisher ----
88-
// $ExpectType Publisher
90+
// $ExpectType Publisher<"std_msgs/msg/String">
8991
const publisher = node.createPublisher(TYPE_CLASS, TOPIC);
9092

9193
// $ExpectType object
@@ -104,15 +106,22 @@ publisher.typeClass;
104106
publisher.typedArrayEnabled;
105107

106108
// $ExpectType void
107-
publisher.publish('');
109+
publisher.publish(MSG);
110+
111+
// $ExpectType void
112+
publisher.publish(Buffer.from('Hello ROS World'));
113+
114+
// $ExpectType void
115+
node.destroyPublisher(publisher);
116+
108117

109118
// ---- Subscription ----
110119
// $ExpectType Subscription
111120
const subscription = node.createSubscription(
112121
TYPE_CLASS,
113122
TOPIC,
114123
{},
115-
(msg: rclnodejs.Message) => {}
124+
(msg) => {}
116125
);
117126

118127
// $ExpectType string

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ declare module 'rclnodejs' {
113113
* or {package: 'std_msgs', type: 'msg', name: 'String'}
114114
* @returns A Message object or undefined if type is not recognized.
115115
*/
116-
function createMessageObject(type: TypeClass): Message;
116+
function createMessageObject<T extends TypeClass<MessageTypeClassName>>(type: T): MessageType<T>;
117117

118118
/**
119119
* Get a list of action names and types for action clients associated with a node.

types/node.d.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable camelcase */
22

3-
import { Clock } from 'rclnodejs';
3+
import { Clock, TypeClassName, MessageTypeClassName } from 'rclnodejs';
44
import { Logging } from 'rclnodejs';
55
import { Parameter, ParameterDescriptor, ParameterType } from 'rclnodejs';
66
import { QoS } from 'rclnodejs';
@@ -27,7 +27,23 @@ declare module 'rclnodejs' {
2727
* See {@link DEFAULT_OPTIONS}
2828
*/
2929
interface Options<T = QoS | QoS.ProfileRef> {
30-
enableTypedArray?: boolean;
30+
31+
/**
32+
* A messages will use TypedArray if necessary, default: true.
33+
*/
34+
enableTypedArray?: boolean;
35+
36+
/**
37+
* Indicates messages are serialized, default: false.
38+
*
39+
* @remarks
40+
* See {@link Node#createSubscription | Node.createSubscription}
41+
*/
42+
isRaw?: boolean;
43+
44+
/**
45+
* ROS Middleware "quality of service" setting, default: QoS.profileDefault.
46+
*/
3147
qos?: T;
3248
}
3349

@@ -37,7 +53,8 @@ declare module 'rclnodejs' {
3753
* ```ts
3854
* {
3955
* enableTypedArray: true,
40-
* qos: QoS.profileDefault
56+
* qos: QoS.profileDefault,
57+
* isRaw: false
4158
* }
4259
* ```
4360
*/
@@ -84,9 +101,9 @@ declare module 'rclnodejs' {
84101
* See {@link Publisher}
85102
* See {@link Subscription}
86103
*/
87-
type SubscriptionCallback =
104+
type SubscriptionCallback<T extends TypeClass<MessageTypeClassName>> =
88105
// * @param message - The published message
89-
(message: Message) => void;
106+
(message: MessageType<T> | Buffer) => void;
90107

91108
/**
92109
* Callback for receiving service requests from a client.
@@ -246,11 +263,11 @@ declare module 'rclnodejs' {
246263
* @param options - Configuration options, see DEFAULT_OPTIONS
247264
* @returns New instance of Publisher.
248265
*/
249-
createPublisher(
250-
typeClass: TypeClass,
266+
createPublisher<T extends TypeClass<MessageTypeClassName>>(
267+
typeClass: T,
251268
topic: string,
252269
options?: Options
253-
): Publisher;
270+
): Publisher<T>;
254271

255272
/**
256273
* Create a Subscription.
@@ -261,11 +278,11 @@ declare module 'rclnodejs' {
261278
* @param callback - Called when a new message is received.
262279
* @returns New instance of Subscription.
263280
*/
264-
createSubscription(
265-
typeClass: TypeClass,
281+
createSubscription<T extends TypeClass<MessageTypeClassName>>(
282+
typeClass: T,
266283
topic: string,
267284
options: Options,
268-
callback: SubscriptionCallback
285+
callback: SubscriptionCallback<T>
269286
): Subscription;
270287

271288
/**
@@ -318,7 +335,7 @@ declare module 'rclnodejs' {
318335
*
319336
* @param publisher - Publisher to be destroyed.
320337
*/
321-
destroyPublisher(publisher: Publisher): void;
338+
destroyPublisher<T extends MessageTypeClassName>(publisher: Publisher<T>): void;
322339

323340
/**
324341
* Destroy a Subscription.

types/publisher.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { MessageTypeClassName } from 'rclnodejs';
2+
13
declare module 'rclnodejs' {
24
/**
35
* A ROS Publisher that publishes messages on a topic.
46
*/
5-
class Publisher extends Entity {
7+
class Publisher<T extends TypeClass<MessageTypeClassName>> extends Entity {
68
/**
79
* Topic on which messages are published.
810
*/
@@ -13,6 +15,6 @@ declare module 'rclnodejs' {
1315
*
1416
* @param message - The message to be sent.
1517
*/
16-
publish(message: Message): void;
18+
publish(message: MessageType<T> | Buffer): void;
1719
}
1820
}

0 commit comments

Comments
 (0)