@signalk/streams 5.1.1 → 5.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/serialport.js +41 -32
  3. package/simple.js +1 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@signalk/streams",
3
- "version": "5.1.1",
3
+ "version": "5.1.3",
4
4
  "description": "Utilities for handling streams of Signal K data",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/serialport.js CHANGED
@@ -82,13 +82,52 @@ function SerialStream(options) {
82
82
 
83
83
  const createDebug = options.createDebug || require('debug')
84
84
  this.debug = createDebug('signalk:streams:serialport')
85
+
86
+ let pendingWrites = 0
87
+ const stdOutEvents = isArray(this.options.toStdout)
88
+ ? this.options.toStdout
89
+ : [this.options.toStdout]
90
+ const standardOutEventName = `serial-${this.options.providerId}-toStdout`
91
+ stdOutEvents.push(standardOutEventName)
92
+
93
+ const that = this
94
+ stdOutEvents.forEach((event) => {
95
+ const onDrain = () => {
96
+ pendingWrites--
97
+ }
98
+
99
+ that.options.app.on(event, (d) => {
100
+ if (pendingWrites > that.maxPendingWrites) {
101
+ that.debug('Buffer overflow, not writing:' + d)
102
+ return
103
+ }
104
+ that.debug('Writing:' + d)
105
+ if (isBuffer(d)) {
106
+ that.serial.write(d)
107
+ } else {
108
+ that.serial.write(d + '\r\n')
109
+ }
110
+ setImmediate(() => {
111
+ that.options.app.emit('connectionwrite', {
112
+ providerId: that.options.providerId
113
+ })
114
+ })
115
+ pendingWrites++
116
+ that.serial.drain(onDrain)
117
+ })
118
+ })
119
+
120
+ this.options.app.emitPropertyValue('serialport', {
121
+ id: this.options.providerId,
122
+ eventNames: {
123
+ toStdout: standardOutEventName
124
+ }
125
+ })
85
126
  }
86
127
 
87
128
  require('util').inherits(SerialStream, Transform)
88
129
 
89
130
  SerialStream.prototype.start = function () {
90
- const that = this
91
-
92
131
  if (this.serial !== null) {
93
132
  this.serial.unpipe(this)
94
133
  this.serial.removeAllListeners()
@@ -146,36 +185,6 @@ SerialStream.prototype.start = function () {
146
185
  this.scheduleReconnect()
147
186
  }.bind(this)
148
187
  )
149
-
150
- let pendingWrites = 0
151
- const stdOutEvent = this.options.toStdout
152
- if (stdOutEvent) {
153
- ;(isArray(stdOutEvent) ? stdOutEvent : [stdOutEvent]).forEach((event) => {
154
- const onDrain = () => {
155
- pendingWrites--
156
- }
157
-
158
- that.options.app.on(event, (d) => {
159
- if (pendingWrites > that.maxPendingWrites) {
160
- that.debug('Buffer overflow, not writing:' + d)
161
- return
162
- }
163
- that.debug('Writing:' + d)
164
- if (isBuffer(d)) {
165
- that.serial.write(d)
166
- } else {
167
- that.serial.write(d + '\r\n')
168
- }
169
- setImmediate(() => {
170
- that.options.app.emit('connectionwrite', {
171
- providerId: that.options.providerId
172
- })
173
- })
174
- pendingWrites++
175
- that.serial.drain(onDrain)
176
- })
177
- })
178
- }
179
188
  }
180
189
 
181
190
  SerialStream.prototype.end = function () {
package/simple.js CHANGED
@@ -97,6 +97,7 @@ function Simple(options) {
97
97
  const dataReceivedEventName = `${options.subOptions.providerId}-received`
98
98
 
99
99
  const spy = new Transform({
100
+ objectMode: true,
100
101
  transform(chunk, encoding, callback) {
101
102
  options.app.emit(dataReceivedEventName, chunk)
102
103
  callback(null, chunk)