Skip to content

Commit fc0705b

Browse files
ros2jsguyMinggang Wang
authored andcommitted
Updated for PR improvement feedback.
index.js changes: * removed unnneeded debug cruft * improved property names * minor code cleanup
1 parent 26983d1 commit fc0705b

File tree

1 file changed

+41
-53
lines changed

1 file changed

+41
-53
lines changed

index.js

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const fs = require('fs');
2424
const generator = require('./rosidl_gen/index.js');
2525
const loader = require('./lib/interface_loader.js');
2626
const logging = require('./lib/logging.js');
27-
// const Node = require('./lib/node.js');
2827
const NodeOptions = require('./lib/node_options.js');
2928
const {
3029
FloatingPointRange,
@@ -58,8 +57,6 @@ function inherits(target, source) {
5857
});
5958
}
6059

61-
// inherits(rclnodejs.ShadowNode, Node);
62-
6360
function getCurrentGeneratorVersion() {
6461
let jsonFilePath = path.join(generator.generatedRoot, 'generator.json');
6562

@@ -90,11 +87,10 @@ function getCurrentGeneratorVersion() {
9087
*/
9188
let rcl = {
9289

93-
// flag identifies when module has been init'ed
94-
initialized: false,
95-
90+
_rosVersionChecked: false,
91+
9692
// Map<Context,Array<Node>
97-
_nodes: new Map(),
93+
_contextToNodeArrayMap: new Map(),
9894

9995
/** {@link Clock} class */
10096
Clock: Clock,
@@ -195,7 +191,7 @@ let rcl = {
195191
throw new TypeError('Invalid argument.');
196192
}
197193

198-
if (!this._nodes.has(context)) {
194+
if (!this._contextToNodeArrayMap.has(context)) {
199195
throw new Error('Invalid context. Must call rclnodejs(context) before using the context');
200196
}
201197

@@ -210,7 +206,7 @@ let rcl = {
210206
namespace
211207
);
212208

213-
this._nodes.get(context).push(node);
209+
this._contextToNodeArrayMap.get(context).push(node);
214210
return node;
215211
},
216212

@@ -224,53 +220,51 @@ let rcl = {
224220

225221
return new Promise((resolve, reject) => {
226222
// check if context has already been initialized
227-
if (this._nodes.has(context)) {
228-
throw new Error('The module rclnodejs has been initialized.');
223+
if (this._contextToNodeArrayMap.has(context)) {
224+
throw new Error('The context has already been initialized.');
229225
};
230226

231227
// check argv for correct value and state
232228
if (!Array.isArray(argv)) {
233229
throw new TypeError('argv must be an array.');
234230
}
235-
if (argv.reduce((hasNull, arg) => typeof arg !== 'string', false)) {
231+
if (argv.reduce((hasNull, arg) => hasNull || typeof arg !== 'string', false)) {
236232
throw new TypeError('argv elements must not be null');
237233
}
238234

239-
// setup internal state for context outside promise.
235+
// initialize context
240236
rclnodejs.init(context.handle(), argv);
241-
this._nodes.set(context, []);
242-
243-
let that = this;
244-
if (!this._initialized) {
245-
getCurrentGeneratorVersion()
246-
.then(version => {
247-
let forced =
248-
version === null ||
249-
compareVersions(version, generator.version()) === -1
250-
? true
251-
: false;
252-
if (forced) {
253-
debug(
254-
'The generator will begin to create JavaScript code from ROS IDL files...'
255-
);
256-
}
257-
258-
generator
259-
.generateAll(forced)
260-
.then(() => {
261-
this._initialized = true;
262-
resolve();
263-
})
264-
.catch(e => {
265-
reject(e);
266-
});
267-
})
268-
.catch(e => {
269-
reject(e);
270-
});
271-
} else {
237+
this._contextToNodeArrayMap.set(context, []);
238+
239+
if (this._rosVersionChecked) {
240+
// no further processing required
272241
resolve();
273242
}
243+
244+
getCurrentGeneratorVersion()
245+
.then(version => {
246+
let forced =
247+
version === null ||
248+
compareVersions(version, generator.version()) === -1;
249+
if (forced) {
250+
debug(
251+
'The generator will begin to create JavaScript code from ROS IDL files...'
252+
);
253+
}
254+
255+
generator
256+
.generateAll(forced)
257+
.then(() => {
258+
this._rosVersionChecked = true;
259+
resolve();
260+
})
261+
.catch(e => {
262+
reject(e);
263+
});
264+
})
265+
.catch(e => {
266+
reject(e);
267+
});
274268
});
275269
},
276270

@@ -316,18 +310,12 @@ let rcl = {
316310
return;
317311
}
318312

319-
// check for non-existant or deleted context
320-
// if (context === Context.defaultContext()) {
321-
// console.log('default ctx', this._nodes.has(Context.defaultContext()));
322-
// }
323-
if (!this._nodes.has(context)) {console.log('xxx'); return;}
324-
325313
// shutdown and remove all nodes assigned to context
326-
this._nodes.get(context).forEach(node => {
314+
this._contextToNodeArrayMap.get(context).forEach(node => {
327315
node.stopSpinning();
328316
node.destroy();
329317
});
330-
this._nodes.delete(context);
318+
this._contextToNodeArrayMap.delete(context);
331319

332320
// shutdown context
333321
if (context === Context.defaultContext()) {
@@ -343,7 +331,7 @@ let rcl = {
343331
* @return {boolean} Return true if the module is shut down, otherwise return false.
344332
*/
345333
isShutdown(context = Context.defaultContext()) {
346-
return !this._nodes.has(context);
334+
return !this._contextToNodeArrayMap.has(context);
347335
},
348336

349337
/**

0 commit comments

Comments
 (0)