Skip to content

Commit 5616664

Browse files
authored
Change isOpen() to a propterty (#899)
1 parent 3aafa77 commit 5616664

File tree

6 files changed

+86
-84
lines changed

6 files changed

+86
-84
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ install:
8282
script:
8383
# linting no longer works on node 0.10
8484
- node bin/at-least-node-4.0.js && npm run lint || true
85-
- npm run docs-diff
85+
- npm run docs:diff
8686
- node ./
8787
- npm test
8888

README.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ For getting started with node-serialport, we recommend you begin with the follow
5252
* _instance_
5353
* [`.open([callback])`](#module_serialport--SerialPort+open)
5454
* [`.update([options], [callback])`](#module_serialport--SerialPort+update)
55-
* [`.isOpen()`](#module_serialport--SerialPort+isOpen) ⇒ <code>Boolean</code>
5655
* [`.write(data, [callback])`](#module_serialport--SerialPort+write)
5756
* [`.pause()`](#module_serialport--SerialPort+pause)
5857
* [`.resume()`](#module_serialport--SerialPort+resume)
@@ -299,6 +298,7 @@ Enjoy and do cool things with this code.
299298
| Name | Type | Description |
300299
| --- | --- | --- |
301300
| path | <code>string</code> | The system path or name of the serial port. Read Only. |
301+
| isOpen | <code>boolean</code> | true if the port is open, false otherwise. Read Only. |
302302

303303

304304
-
@@ -351,15 +351,6 @@ Changes the baud rate for an open port. Throws if you provide a bad argument. Em
351351
| [callback] | <code>[errorCallback](#module_serialport--SerialPort..errorCallback)</code> | Called once the port's baud rate has been changed. If `.update` is called without an callback and there is an error, an error event will be emitted. |
352352

353353

354-
-
355-
356-
<a name="module_serialport--SerialPort+isOpen"></a>
357-
358-
#### `serialPort.isOpen()` ⇒ <code>Boolean</code>
359-
Returns `true` if the port is open.
360-
361-
**Kind**: instance method of <code>[SerialPort](#exp_module_serialport--SerialPort)</code>
362-
363354
-
364355

365356
<a name="module_serialport--SerialPort+write"></a>

lib/serialport.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ var defaultSetFlags = {
9090
* @param {module:serialport~errorCallback=} openCallback - Called when a connection has been opened. If this is not provided and an error occurs, it will be emitted on the ports `error` event. The callback will NOT be called if autoOpen is set to false in the openOptions as the open will not be performed.
9191
* @throws {TypeError} When given invalid arguments a TypeError will be thrown.
9292
* @property {string} path The system path or name of the serial port. Read Only.
93+
* @property {boolean} isOpen true if the port is open, false otherwise. Read Only.
9394
* @emits module:serialport#open
9495
* @emits module:serialport#data
9596
* @emits module:serialport#close
@@ -121,6 +122,12 @@ function SerialPort(path, options, callback) {
121122
value: path
122123
});
123124

125+
Object.defineProperty(this, 'isOpen', {
126+
enumerable: true,
127+
get: function() {
128+
return this.fd !== null && !this.closing;
129+
}
130+
});
124131

125132
if (!this.path) {
126133
throw new TypeError('No path specified');
@@ -201,7 +208,7 @@ SerialPort.prototype._error = function(error, callback) {
201208
* @emits module:serialport#open
202209
*/
203210
SerialPort.prototype.open = function(callback) {
204-
if (this.isOpen()) {
211+
if (this.isOpen) {
205212
return this._error(new Error('Port is already open'), callback);
206213
}
207214

@@ -246,7 +253,7 @@ SerialPort.prototype.open = function(callback) {
246253
* @param {module:serialport~errorCallback=} [callback] Called once the port's baud rate has been changed. If `.update` is called without an callback and there is an error, an error event will be emitted.
247254
*/
248255
SerialPort.prototype.update = function(options, callback) {
249-
if (!this.isOpen()) {
256+
if (!this.isOpen) {
250257
debug('update attempted, but port is not open');
251258
return this._error(new Error('Port is not open'), callback);
252259
}
@@ -262,14 +269,6 @@ SerialPort.prototype.update = function(options, callback) {
262269
}.bind(this));
263270
};
264271

265-
/**
266-
* Returns `true` if the port is open.
267-
* @return {Boolean}
268-
*/
269-
SerialPort.prototype.isOpen = function() {
270-
return this.fd !== null && !this.closing;
271-
};
272-
273272
/**
274273
* Writes data to the given serial port.
275274
* @param {(string|array|buffer)} data Accepts a [`Buffer` ](http://nodejs.org/api/buffer.html) object, or a type that is accepted by the `Buffer` constructor (ex. an array of bytes or a string).
@@ -278,7 +277,7 @@ SerialPort.prototype.isOpen = function() {
278277
* @description Some devices like the Arduino reset when you open a connection to them. In these cases if you immediately write to the device they wont be ready to receive the data. This is often worked around by having the Arduino send a "ready" byte that your node program waits for before writing. You can also often get away with waiting around 400ms.
279278
*/
280279
SerialPort.prototype.write = function(data, callback) {
281-
if (!this.isOpen()) {
280+
if (!this.isOpen) {
282281
debug('write attempted, but port is not open');
283282
return this._error(new Error('Port is not open'), callback);
284283
}
@@ -322,7 +321,7 @@ if (process.platform !== 'win32') {
322321
this.reading = false;
323322
if (err) {
324323
if (err.code && err.code === 'EAGAIN') {
325-
if (this.isOpen()) {
324+
if (this.isOpen) {
326325
this.serialPoller.start();
327326
}
328327
// handle edge case were mac/unix doesn't clearly know the error.
@@ -341,7 +340,7 @@ if (process.platform !== 'win32') {
341340
this.pool.used -= bytesRequested - bytesRead;
342341

343342
if (bytesRead === 0) {
344-
if (this.isOpen()) {
343+
if (this.isOpen) {
345344
this.serialPoller.start();
346345
}
347346
} else {
@@ -396,7 +395,7 @@ if (process.platform !== 'win32') {
396395
}
397396

398397
// No longer open?
399-
if (!this.isOpen()) {
398+
if (!this.isOpen) {
400399
return;
401400
}
402401

@@ -454,7 +453,7 @@ SerialPort.prototype.close = function(callback) {
454453
return this._error(new Error('Port is not open'), callback);
455454
}
456455

457-
if (!this.isOpen()) {
456+
if (!this.isOpen) {
458457
debug('close attempted, but port is not open');
459458
return this._error(new Error('Port is not open'), callback);
460459
}
@@ -484,7 +483,7 @@ SerialPort.prototype.close = function(callback) {
484483
* @param {module:serialport~errorCallback=} callback Called once the flush operation finishes.
485484
*/
486485
SerialPort.prototype.flush = function(callback) {
487-
if (!this.isOpen()) {
486+
if (!this.isOpen) {
488487
debug('flush attempted, but port is not open');
489488
return this._error(new Error('Port is not open'), callback);
490489
}
@@ -509,7 +508,7 @@ SerialPort.prototype.flush = function(callback) {
509508
* @param {module:serialport~errorCallback=} callback Called once the port's flags have been set.
510509
*/
511510
SerialPort.prototype.set = function(options, callback) {
512-
if (!this.isOpen()) {
511+
if (!this.isOpen) {
513512
debug('set attempted, but port is not open');
514513
return this._error(new Error('Port is not open'), callback);
515514
}
@@ -554,7 +553,7 @@ function writeAndDrain (data, callback) {
554553
```
555554
*/
556555
SerialPort.prototype.drain = function(callback) {
557-
if (!this.isOpen()) {
556+
if (!this.isOpen) {
558557
debug('drain attempted, but port is not open');
559558
return this._error(new Error('Port is not open'), callback);
560559
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,12 @@
8888
"license": "MIT",
8989
"scripts": {
9090
"docs": "jsdoc2md -t .docs/README.hbs --partial .docs/sig-name.hbs --partial .docs/sig-link.hbs -r table --separators --name-format lib/* > README.md",
91-
"docs-diff": "jsdoc2md -t .docs/README.hbs --partial .docs/sig-name.hbs --partial .docs/sig-link.hbs -r table --separators --name-format lib/* | diff README.md - || (echo 'Docs out of date, run `npm run docs` and commit the new README.md' && false)",
91+
"docs:diff": "jsdoc2md -t .docs/README.hbs --partial .docs/sig-name.hbs --partial .docs/sig-link.hbs -r table --separators --name-format lib/* | diff README.md - || (echo 'Docs out of date, run `npm run docs` and commit the new README.md' && false)",
9292
"install": "node-pre-gyp install --fallback-to-build",
9393
"rebuild-all": "npm rebuild && node-gyp rebuild",
9494
"stress": "mocha --no-timeouts test/arduinoTest/stress.js",
9595
"lint": "eslint *.js lib/**/*.js test/**/*.js bin/**/*.js examples/**/*.js",
9696
"test": "istanbul cover ./node_modules/mocha/bin/_mocha test/*.js test/arduinoTest/integration.js test/integration-lite.js",
97-
"test:unit": "mocha test/*.js test/arduinoTest/integration.js test/integration-lite.js",
9897
"report-coverage": "cat ./coverage/lcov.info | coveralls",
9998
"valgrind": "valgrind --leak-check=full --show-possibly-lost=no node --expose-gc --trace-gc node_modules/.bin/grunt test"
10099
},

test/integration-lite.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ describe('SerialPort light integration', function() {
5353
it('can open and close', function(done) {
5454
var port = new SerialPort(testPort);
5555
port.on('open', function() {
56-
assert.isTrue(port.isOpen());
56+
assert.isTrue(port.isOpen);
5757
port.close();
5858
});
5959
port.on('close', function() {
60-
assert.isFalse(port.isOpen());
60+
assert.isFalse(port.isOpen);
6161
done();
6262
});
6363
});

test/serialport.js

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,75 @@ describe('SerialPort', function() {
179179
assert.equal(port.path, '/dev/exists');
180180
});
181181
});
182+
183+
describe('.isOpen', function() {
184+
it('is a read only property', function(){
185+
var port = new SerialPort('/dev/exists', {autoOpen: false});
186+
assert.equal(port.isOpen, false);
187+
try {
188+
port.isOpen = 'foo';
189+
} catch(e) {
190+
assert.instanceOf(e, TypeError);
191+
}
192+
assert.equal(port.isOpen, false);
193+
});
194+
195+
it('returns false when the port is created', function(done) {
196+
var port = new SerialPort('/dev/exists', { autoOpen: false });
197+
assert.isFalse(port.isOpen);
198+
done();
199+
});
200+
201+
it('returns false when the port is opening', function(done) {
202+
var port = new SerialPort('/dev/exists', { autoOpen: false });
203+
sandbox.stub(bindings, 'open', function() {
204+
assert.isTrue(port.opening);
205+
assert.isFalse(port.isOpen);
206+
done();
207+
});
208+
port.open();
209+
});
210+
211+
it('returns true when the port is open', function(done) {
212+
var port = new SerialPort('/dev/exists', function() {
213+
assert.isTrue(port.isOpen);
214+
done();
215+
});
216+
});
217+
218+
it('returns false when the port is closing', function(done) {
219+
var port;
220+
sandbox.stub(bindings, 'close', function() {
221+
assert.isTrue(port.closing);
222+
assert.isFalse(port.isOpen);
223+
done();
224+
});
225+
port = new SerialPort('/dev/exists', {}, function() {
226+
port.close();
227+
});
228+
});
229+
230+
it('returns false when the port is closed', function(done) {
231+
var port = new SerialPort('/dev/exists', function() {
232+
port.close();
233+
});
234+
port.on('close', function() {
235+
assert.isFalse(port.isOpen);
236+
done();
237+
});
238+
});
239+
});
182240
});
183241

184242
describe('Methods', function() {
185243
describe('#open', function() {
186244
it('passes the port to the bindings', function(done) {
187245
var openSpy = sandbox.spy(bindings, 'open');
188246
var port = new SerialPort('/dev/exists', { autoOpen: false });
189-
assert.isFalse(port.isOpen());
247+
assert.isFalse(port.isOpen);
190248
port.open(function(err) {
191249
assert.isNull(err);
192-
assert.isTrue(port.isOpen());
250+
assert.isTrue(port.isOpen);
193251
assert.isTrue(openSpy.calledWith('/dev/exists'));
194252
done();
195253
});
@@ -275,7 +333,7 @@ describe('SerialPort', function() {
275333
it('emits a close event', function(done) {
276334
var port = new SerialPort('/dev/exists', function() {
277335
port.on('close', function() {
278-
assert.isFalse(port.isOpen());
336+
assert.isFalse(port.isOpen);
279337
done();
280338
});
281339
port.close();
@@ -285,7 +343,7 @@ describe('SerialPort', function() {
285343
it('has a close callback', function(done) {
286344
var port = new SerialPort('/dev/exists', function() {
287345
port.close(function() {
288-
assert.isFalse(port.isOpen());
346+
assert.isFalse(port.isOpen);
289347
done();
290348
});
291349
});
@@ -341,51 +399,6 @@ describe('SerialPort', function() {
341399
});
342400
});
343401

344-
describe('#isOpen', function() {
345-
it('returns false when the port is created', function(done) {
346-
var port = new SerialPort('/dev/exists', { autoOpen: false });
347-
assert.isFalse(port.isOpen());
348-
done();
349-
});
350-
351-
it('returns false when the port is opening', function(done) {
352-
var port = new SerialPort('/dev/exists', { autoOpen: false });
353-
sandbox.stub(bindings, 'open', function() {
354-
assert.isTrue(port.opening);
355-
assert.isFalse(port.isOpen());
356-
done();
357-
});
358-
port.open();
359-
});
360-
361-
it('returns true when the port is open', function(done) {
362-
var port = new SerialPort('/dev/exists', function() {
363-
assert.isTrue(port.isOpen());
364-
done();
365-
});
366-
});
367-
it('returns false when the port is closing', function(done) {
368-
var port;
369-
sandbox.stub(bindings, 'close', function() {
370-
assert.isTrue(port.closing);
371-
assert.isFalse(port.isOpen());
372-
done();
373-
});
374-
port = new SerialPort('/dev/exists', {}, function() {
375-
port.close();
376-
});
377-
});
378-
it('returns false when the port is closed', function(done) {
379-
var port = new SerialPort('/dev/exists', function() {
380-
port.close();
381-
});
382-
port.on('close', function() {
383-
assert.isFalse(port.isOpen());
384-
done();
385-
});
386-
});
387-
});
388-
389402
describe('#write', function() {
390403
it('errors when the port is not open', function(done) {
391404
var cb = function() {};
@@ -525,13 +538,13 @@ describe('SerialPort', function() {
525538
describe('disconnections', function() {
526539
it('emits a disconnect event and closes the port', function(done) {
527540
var port = new SerialPort('/dev/exists', function() {
528-
assert.isTrue(port.isOpen());
541+
assert.isTrue(port.isOpen);
529542
hardware.disconnect('/dev/exists');
530543
});
531544
var spy = sandbox.spy();
532545
port.on('disconnect', spy);
533546
port.on('close', function() {
534-
assert.isFalse(port.isOpen());
547+
assert.isFalse(port.isOpen);
535548
assert.isTrue(spy.calledOnce);
536549
done();
537550
});

0 commit comments

Comments
 (0)