• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RobotWebTools / rclnodejs / 19690312781

26 Nov 2025 02:15AM UTC coverage: 82.843% (+1.1%) from 81.767%
19690312781

push

github

minggangw
Pump to 1.7.0 (#1329)

1074 of 1420 branches covered (75.63%)

Branch coverage included in aggregate %.

2446 of 2829 relevant lines covered (86.46%)

488.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.21
/lib/event_handler.js
1
// Copyright (c) 2025, The Robot Web Tools Contributors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
'use strict';
16

17
const rclnodejs = require('./native_loader.js');
26✔
18
const DistroUtils = require('./distro.js');
26✔
19
const { OperationError } = require('./errors.js');
26✔
20
const Entity = require('./entity.js');
26✔
21

22
/**
23
 * Enumeration for PublisherEventCallbacks event types.
24
 * @enum {number}
25
 */
26
const PublisherEventType = {
26✔
27
  /** @member {number} */
28
  PUBLISHER_OFFERED_DEADLINE_MISSED: 0,
29
  /** @member {number} */
30
  PUBLISHER_LIVELINESS_LOST: 1,
31
  /** @member {number} */
32
  PUBLISHER_OFFERED_INCOMPATIBLE_QOS: 2,
33
  /** @member {number} */
34
  PUBLISHER_INCOMPATIBLE_TYPE: 3,
35
  /** @member {number} */
36
  PUBLISHER_MATCHED: 4,
37
};
38

39
/**
40
 * Enumeration for SubscriptionEventCallbacks event types.
41
 * @enum {number}
42
 */
43
const SubscriptionEventType = {
26✔
44
  /** @member {number} */
45
  SUBSCRIPTION_REQUESTED_DEADLINE_MISSED: 0,
46
  /** @member {number} */
47
  SUBSCRIPTION_LIVELINESS_CHANGED: 1,
48
  /** @member {number} */
49
  SUBSCRIPTION_REQUESTED_INCOMPATIBLE_QOS: 2,
50
  /** @member {number} */
51
  SUBSCRIPTION_MESSAGE_LOST: 3,
52
  /** @member {number} */
53
  SUBSCRIPTION_INCOMPATIBLE_TYPE: 4,
54
  /** @member {number} */
55
  SUBSCRIPTION_MATCHED: 5,
56
};
57

58
class EventHandler extends Entity {
59
  constructor(handle, callback, eventType, eventTypeName) {
60
    super(handle, null, null);
11✔
61
    this._callback = callback;
11✔
62
    this._eventType = eventType;
11✔
63
    this._eventTypeName = eventTypeName;
11✔
64
  }
65

66
  takeData() {
67
    const data = rclnodejs.takeEvent(this._handle, {
10✔
68
      [this._eventTypeName]: this._eventType,
69
    });
70
    if (this._callback) {
10!
71
      this._callback(data);
10✔
72
    }
73
  }
74
}
75

76
/**
77
 * @class - Class representing a ROS 2 PublisherEventCallbacks
78
 * @hideconstructor
79
 */
80
class PublisherEventCallbacks {
81
  constructor() {
82
    if (DistroUtils.getDistroId() < DistroUtils.getDistroId('jazzy')) {
2!
83
      throw new OperationError(
×
84
        'PublisherEventCallbacks is only available in ROS 2 Jazzy and later',
85
        {
86
          code: 'UNSUPPORTED_ROS_VERSION',
87
          entityType: 'publisher event callbacks',
88
          details: {
89
            requiredVersion: 'jazzy',
90
            currentVersion: DistroUtils.getDistroId(),
91
          },
92
        }
93
      );
94
    }
95
    this._deadline = null;
2✔
96
    this._incompatible_qos = null;
2✔
97
    this._liveliness = null;
2✔
98
    this._incompatible_type = null;
2✔
99
    this._matched = null;
2✔
100
    this._eventHandlers = [];
2✔
101
  }
102

103
  /**
104
   * Set deadline missed callback.
105
   * @param {function} callback - The callback function to be called.
106
   */
107
  set deadline(callback) {
108
    this._deadline = callback;
1✔
109
  }
110

111
  /**
112
   * Get deadline missed callback.
113
   * @return {function} - The callback function.
114
   */
115
  get deadline() {
116
    return this._deadline;
×
117
  }
118

119
  /**
120
   * Set incompatible QoS callback.
121
   * @param {function} callback - The callback function to be called.
122
   */
123
  set incompatibleQos(callback) {
124
    this._incompatible_qos = callback;
1✔
125
  }
126

127
  /**
128
   * Get incompatible QoS callback.
129
   * @return {function} - The callback function.
130
   */
131
  get incompatibleQos() {
132
    return this._incompatible_qos;
×
133
  }
134

135
  /**
136
   * Set liveliness lost callback.
137
   * @param {function} callback - The callback function to be called.
138
   */
139
  set liveliness(callback) {
140
    this._liveliness = callback;
1✔
141
  }
142

143
  /**
144
   * Get liveliness lost callback.
145
   * @return {function} - The callback function.
146
   */
147
  get liveliness() {
148
    return this._liveliness;
×
149
  }
150

151
  /**
152
   * Set incompatible type callback.
153
   * @param {function} callback - The callback function to be called.
154
   */
155
  set incompatibleType(callback) {
156
    this._incompatible_type = callback;
×
157
  }
158

159
  /**
160
   * Get incompatible type callback.
161
   * @return {function} - The callback function.
162
   */
163
  get incompatibleType() {
164
    return this._incompatible_type;
×
165
  }
166

167
  /**
168
   * Set matched callback.
169
   * @param {function} callback - The callback function to be called.
170
   */
171
  set matched(callback) {
172
    this._matched = callback;
1✔
173
  }
174

175
  /**
176
   * Get matched callback.
177
   * @return {function} - The callback function.
178
   */
179
  get matched() {
180
    return this._matched;
×
181
  }
182

183
  createEventHandlers(publisherHandle) {
184
    if (this._deadline) {
2✔
185
      const deadlineHandle = rclnodejs.createPublisherEventHandle(
1✔
186
        publisherHandle,
187
        PublisherEventType.PUBLISHER_OFFERED_DEADLINE_MISSED
188
      );
189
      this._eventHandlers.push(
1✔
190
        new EventHandler(
191
          deadlineHandle,
192
          this._deadline,
193
          PublisherEventType.PUBLISHER_OFFERED_DEADLINE_MISSED,
194
          'publisher_event_type'
195
        )
196
      );
197
    }
198

199
    if (this._incompatible_qos) {
2✔
200
      const incompatibleQosHandle = rclnodejs.createPublisherEventHandle(
1✔
201
        publisherHandle,
202
        PublisherEventType.PUBLISHER_OFFERED_INCOMPATIBLE_QOS
203
      );
204
      this._eventHandlers.push(
1✔
205
        new EventHandler(
206
          incompatibleQosHandle,
207
          this._incompatible_qos,
208
          PublisherEventType.PUBLISHER_OFFERED_INCOMPATIBLE_QOS,
209
          'publisher_event_type'
210
        )
211
      );
212
    }
213

214
    if (this._liveliness) {
2✔
215
      const livelinessHandle = rclnodejs.createPublisherEventHandle(
1✔
216
        publisherHandle,
217
        PublisherEventType.PUBLISHER_LIVELINESS_LOST
218
      );
219
      this._eventHandlers.push(
1✔
220
        new EventHandler(
221
          livelinessHandle,
222
          this._liveliness,
223
          PublisherEventType.PUBLISHER_LIVELINESS_LOST,
224
          'publisher_event_type'
225
        )
226
      );
227
    }
228

229
    if (this._incompatible_type) {
2!
230
      const incompatibleTypeHandle = rclnodejs.createPublisherEventHandle(
×
231
        publisherHandle,
232
        PublisherEventType.PUBLISHER_INCOMPATIBLE_TYPE
233
      );
234
      this._eventHandlers.push(
×
235
        new EventHandler(
236
          incompatibleTypeHandle,
237
          this._incompatible_type,
238
          PublisherEventType.PUBLISHER_INCOMPATIBLE_TYPE,
239
          'publisher_event_type'
240
        )
241
      );
242
    }
243

244
    if (this._matched) {
2✔
245
      const matchedHandle = rclnodejs.createPublisherEventHandle(
1✔
246
        publisherHandle,
247
        PublisherEventType.PUBLISHER_MATCHED
248
      );
249
      this._eventHandlers.push(
1✔
250
        new EventHandler(
251
          matchedHandle,
252
          this._matched,
253
          PublisherEventType.PUBLISHER_MATCHED,
254
          'publisher_event_type'
255
        )
256
      );
257
    }
258

259
    return this._eventHandlers;
2✔
260
  }
261

262
  get eventHandlers() {
263
    return this._eventHandlers;
×
264
  }
265
}
266

267
/**
268
 * @class - Class representing a ROS 2 SubscriptionEventCallbacks
269
 * @hideconstructor
270
 */
271
class SubscriptionEventCallbacks {
272
  constructor() {
273
    if (DistroUtils.getDistroId() < DistroUtils.getDistroId('jazzy')) {
4!
274
      throw new OperationError(
×
275
        'SubscriptionEventCallbacks is only available in ROS 2 Jazzy and later',
276
        {
277
          code: 'UNSUPPORTED_ROS_VERSION',
278
          entityType: 'subscription event callbacks',
279
          details: {
280
            requiredVersion: 'jazzy',
281
            currentVersion: DistroUtils.getDistroId(),
282
          },
283
        }
284
      );
285
    }
286
    this._deadline = null;
4✔
287
    this._incompatible_qos = null;
4✔
288
    this._liveliness = null;
4✔
289
    this._message_lost = null;
4✔
290
    this._incompatible_type = null;
4✔
291
    this._matched = null;
4✔
292
    this._eventHandlers = [];
4✔
293
  }
294

295
  /**
296
   * Set the callback for deadline missed event.
297
   * @param {function} callback - The callback function to be called.
298
   */
299
  set deadline(callback) {
300
    this._deadline = callback;
3✔
301
  }
302

303
  /**
304
   * Get the callback for deadline missed event.
305
   * @return {function} - The callback function.
306
   */
307
  get deadline() {
308
    return this._deadline;
×
309
  }
310

311
  /**
312
   * Set the callback for incompatible QoS event.
313
   * @param {function} callback - The callback function to be called.
314
   */
315
  set incompatibleQos(callback) {
316
    this._incompatible_qos = callback;
1✔
317
  }
318

319
  /**
320
   * Get the callback for incompatible QoS event.
321
   * @return {function} - The callback function.
322
   */
323
  get incompatibleQos() {
324
    return this._incompatible_qos;
×
325
  }
326

327
  /**
328
   * Set the callback for liveliness changed event.
329
   * @param {function} callback - The callback function to be called.
330
   */
331
  set liveliness(callback) {
332
    this._liveliness = callback;
2✔
333
  }
334

335
  /**
336
   * Get the callback for liveliness changed event.
337
   * @return {function} - The callback function.
338
   */
339
  get liveliness() {
340
    return this._liveliness;
×
341
  }
342

343
  /**
344
   * Set the callback for message lost event.
345
   * @param {function} callback - The callback function to be called.
346
   */
347
  set messageLost(callback) {
348
    this._message_lost = callback;
×
349
  }
350

351
  /**
352
   * Get the callback for message lost event.
353
   * @return {function} - The callback function.
354
   */
355
  get messageLost() {
356
    return this._message_lost;
×
357
  }
358

359
  /**
360
   * Set the callback for incompatible type event.
361
   * @param {function} callback - The callback function to be called.
362
   */
363
  set incompatibleType(callback) {
364
    this._incompatible_type = callback;
×
365
  }
366

367
  /**
368
   * Get the callback for incompatible type event.
369
   * @return {function} - The callback function.
370
   */
371
  get incompatibleType() {
372
    return this._incompatible_type;
×
373
  }
374

375
  /**
376
   * Set the callback for matched event.
377
   * @param {function} callback - The callback function to be called.
378
   */
379
  set matched(callback) {
380
    this._matched = callback;
1✔
381
  }
382

383
  /**
384
   * Get the callback for matched event.
385
   * @return {function} - The callback function.
386
   */
387
  get matched() {
388
    return this._matched;
×
389
  }
390

391
  createEventHandlers(subscriptionHandle) {
392
    if (this._deadline) {
4✔
393
      const deadlineHandle = rclnodejs.createSubscriptionEventHandle(
3✔
394
        subscriptionHandle,
395
        SubscriptionEventType.SUBSCRIPTION_REQUESTED_DEADLINE_MISSED
396
      );
397
      this._eventHandlers.push(
3✔
398
        new EventHandler(
399
          deadlineHandle,
400
          this._deadline,
401
          SubscriptionEventType.SUBSCRIPTION_REQUESTED_DEADLINE_MISSED,
402
          'subscription_event_type'
403
        )
404
      );
405
    }
406

407
    if (this._incompatible_qos) {
4✔
408
      const incompatibleQosHandle = rclnodejs.createSubscriptionEventHandle(
1✔
409
        subscriptionHandle,
410
        SubscriptionEventType.SUBSCRIPTION_REQUESTED_INCOMPATIBLE_QOS
411
      );
412
      this._eventHandlers.push(
1✔
413
        new EventHandler(
414
          incompatibleQosHandle,
415
          this._incompatible_qos,
416
          SubscriptionEventType.SUBSCRIPTION_REQUESTED_INCOMPATIBLE_QOS,
417
          'subscription_event_type'
418
        )
419
      );
420
    }
421

422
    if (this._liveliness) {
4✔
423
      const livelinessHandle = rclnodejs.createSubscriptionEventHandle(
2✔
424
        subscriptionHandle,
425
        SubscriptionEventType.SUBSCRIPTION_LIVELINESS_CHANGED
426
      );
427
      this._eventHandlers.push(
2✔
428
        new EventHandler(
429
          livelinessHandle,
430
          this._liveliness,
431
          SubscriptionEventType.SUBSCRIPTION_LIVELINESS_CHANGED,
432
          'subscription_event_type'
433
        )
434
      );
435
    }
436

437
    if (this._message_lost) {
4!
438
      const messageLostHandle = rclnodejs.createSubscriptionEventHandle(
×
439
        subscriptionHandle,
440
        SubscriptionEventType.SUBSCRIPTION_MESSAGE_LOST
441
      );
442
      this._eventHandlers.push(
×
443
        new EventHandler(
444
          messageLostHandle,
445
          this._message_lost,
446
          SubscriptionEventType.SUBSCRIPTION_MESSAGE_LOST,
447
          'subscription_event_type'
448
        )
449
      );
450
    }
451

452
    if (this._incompatible_type) {
4!
453
      const incompatibleTypeHandle = rclnodejs.createSubscriptionEventHandle(
×
454
        subscriptionHandle,
455
        SubscriptionEventType.SUBSCRIPTION_INCOMPATIBLE_TYPE
456
      );
457
      this._eventHandlers.push(
×
458
        new EventHandler(
459
          incompatibleTypeHandle,
460
          this._incompatible_type,
461
          SubscriptionEventType.SUBSCRIPTION_INCOMPATIBLE_TYPE,
462
          'subscription_event_type'
463
        )
464
      );
465
    }
466

467
    if (this._matched) {
4✔
468
      const matchedHandle = rclnodejs.createSubscriptionEventHandle(
1✔
469
        subscriptionHandle,
470
        SubscriptionEventType.SUBSCRIPTION_MATCHED
471
      );
472
      this._eventHandlers.push(
1✔
473
        new EventHandler(
474
          matchedHandle,
475
          this._matched,
476
          SubscriptionEventType.SUBSCRIPTION_MATCHED,
477
          'subscription_event_type'
478
        )
479
      );
480
    }
481

482
    return this._eventHandlers;
4✔
483
  }
484
}
485

486
module.exports = {
26✔
487
  PublisherEventCallbacks,
488
  PublisherEventType,
489
  SubscriptionEventCallbacks,
490
  SubscriptionEventType,
491
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc