@@ -25,7 +25,10 @@ const _options = {
2525 verbose : false
2626} ;
2727
28- function OpenBCISimulator ( portName , options ) {
28+ function Simulator ( portName , options ) {
29+ if ( ! ( this instanceof Simulator ) ) {
30+ return new Simulator ( portName , options ) ;
31+ }
2932 options = options || { } ;
3033 var opts = { } ;
3134
@@ -97,10 +100,9 @@ function OpenBCISimulator (portName, options) {
97100}
98101
99102// This allows us to use the emitter class freely outside of the module
100- // TODO: upgrade from old-style streams to stream.Duplex or stream.Transform
101- util . inherits ( OpenBCISimulator , stream . Stream ) ;
103+ util . inherits ( Simulator , EventEmitter ) ;
102104
103- OpenBCISimulator . prototype . flush = function ( callback ) {
105+ Simulator . prototype . flush = function ( callback ) {
104106 this . outputBuffered = 0 ;
105107
106108 clearTimeout ( this . outputLoopHandle ) ;
@@ -109,12 +111,12 @@ OpenBCISimulator.prototype.flush = function (callback) {
109111 if ( callback ) callback ( ) ;
110112} ;
111113
112- OpenBCISimulator . prototype . isOpen = function ( ) {
114+ Simulator . prototype . isOpen = function ( ) {
113115 return this . connected ;
114116} ;
115117
116118// output only size bytes of the output buffer
117- OpenBCISimulator . prototype . _partialDrain = function ( size ) {
119+ Simulator . prototype . _partialDrain = function ( size ) {
118120 if ( ! this . connected ) throw new Error ( 'not connected' ) ;
119121
120122 if ( size > this . outputBuffered ) size = this . outputBuffered ;
@@ -129,7 +131,7 @@ OpenBCISimulator.prototype._partialDrain = function (size) {
129131} ;
130132
131133// queue some data for output and send it out depending on options.fragmentation
132- OpenBCISimulator . prototype . _output = function ( dataBuffer ) {
134+ Simulator . prototype . _output = function ( dataBuffer ) {
133135 // drain full buffers until there is no overflow
134136 while ( this . outputBuffered + dataBuffer . length > this . outputBuffer . length ) {
135137 var len = dataBuffer . copy ( this . outputBuffer , this . outputBuffered ) ;
@@ -185,7 +187,7 @@ OpenBCISimulator.prototype._output = function (dataBuffer) {
185187 }
186188} ;
187189
188- OpenBCISimulator . prototype . write = function ( data , callback ) {
190+ Simulator . prototype . write = function ( data , callback ) {
189191 if ( ! this . connected ) {
190192 /* istanbul ignore else */
191193 if ( callback ) callback ( Error ( 'Not connected' ) ) ;
@@ -286,11 +288,11 @@ OpenBCISimulator.prototype.write = function (data, callback) {
286288 }
287289} ;
288290
289- OpenBCISimulator . prototype . drain = function ( callback ) {
291+ Simulator . prototype . drain = function ( callback ) {
290292 if ( callback ) callback ( ) ;
291293} ;
292294
293- OpenBCISimulator . prototype . close = function ( callback ) {
295+ Simulator . prototype . close = function ( callback ) {
294296 if ( this . connected ) {
295297 this . flush ( ) ;
296298
@@ -304,7 +306,7 @@ OpenBCISimulator.prototype.close = function (callback) {
304306 }
305307} ;
306308
307- OpenBCISimulator . prototype . _startStream = function ( ) {
309+ Simulator . prototype . _startStream = function ( ) {
308310 var intervalInMS = 1000 / this . options . sampleRate ;
309311
310312 if ( intervalInMS < 2 ) intervalInMS = 2 ;
@@ -341,31 +343,31 @@ OpenBCISimulator.prototype._startStream = function () {
341343 } , intervalInMS ) ;
342344} ;
343345
344- OpenBCISimulator . prototype . _syncUp = function ( ) {
346+ Simulator . prototype . _syncUp = function ( ) {
345347 setTimeout ( ( ) => {
346348 this . sendSyncSetPacket = true ;
347349 } , 12 ) ; // 3 packets later
348350} ;
349351
350- OpenBCISimulator . prototype . _printEOT = function ( ) {
352+ Simulator . prototype . _printEOT = function ( ) {
351353 this . _output ( new Buffer ( '$$$' ) ) ;
352354} ;
353355
354- OpenBCISimulator . prototype . _printFailure = function ( ) {
356+ Simulator . prototype . _printFailure = function ( ) {
355357 this . _output ( new Buffer ( 'Failure: ' ) ) ;
356358} ;
357359
358- OpenBCISimulator . prototype . _printSuccess = function ( ) {
360+ Simulator . prototype . _printSuccess = function ( ) {
359361 this . _output ( new Buffer ( 'Success: ' ) ) ;
360362} ;
361363
362- OpenBCISimulator . prototype . _printValidatedCommsTimeout = function ( ) {
364+ Simulator . prototype . _printValidatedCommsTimeout = function ( ) {
363365 this . _printFailure ( ) ;
364366 this . _output ( new Buffer ( 'Communications timeout - Device failed to poll Host' ) ) ;
365367 this . _printEOT ( ) ;
366368} ;
367369
368- OpenBCISimulator . prototype . _processPrivateRadioMessage = function ( dataBuffer ) {
370+ Simulator . prototype . _processPrivateRadioMessage = function ( dataBuffer ) {
369371 switch ( dataBuffer [ 1 ] ) {
370372 case k . OBCIRadioCmdChannelGet :
371373 if ( this . options . firmwareVersion === k . OBCIFirmwareV2 ) {
@@ -479,6 +481,4 @@ OpenBCISimulator.prototype._processPrivateRadioMessage = function (dataBuffer) {
479481 }
480482} ;
481483
482- util . inherits ( OpenBCISimulator , EventEmitter ) ;
483-
484- module . exports = OpenBCISimulator ;
484+ module . exports = Simulator ;
0 commit comments