Skip to content

Commit 378fe04

Browse files
committed
feat: Add event listeners for 'close' and 'error' events on a HEOS connection
fix #23
1 parent 45bfbd0 commit 378fe04

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ A zero-dependency low level Node.js api-wrapper for communicating with HEOS devi
3131
- [connection.on(event, listener)](#connectiononevent-listener)
3232
- [connection.once(event, listener)](#connectiononceevent-listener)
3333
- [connection.onAll(listener)](#connectiononalllistener)
34+
- [connection.onClose(listener)](#connectiononcloselistener)
35+
- [connection.onError(listener)](#connectiononerrorlistener)
3436
- [HeosEvent and HeosResponse](#heosevent-and-heosresponse)
3537
- [Documentation](#documentation)
3638
- [Contributing](#contributing)
@@ -174,6 +176,34 @@ Exactly like [`connection.on()`](<#connection.on(event,-listener)>) but will onl
174176

175177
Exactly like [`connection.on()`](<#connection.on(event,-listener)>) but will trigger the listener for every response or event. It is useful for logging or debugging purposes. These listeners are triggered before any other as they can be useful for understanding why other listeners might be faulty.
176178

179+
#### connection.onClose(listener)
180+
181+
- `listener`: (hadError: boolean) => void
182+
183+
Adds an event listener for when the connection is closed. `hadError` is true if there was a transmission error.
184+
185+
```js
186+
connection.onClose(hadError => {
187+
if (hadError) {
188+
console.error('There was a transmission error and the connection closed.')
189+
} else {
190+
console.log('Connection closed')
191+
}
192+
})
193+
```
194+
195+
#### connection.onError(listener)
196+
197+
- `listener`: (error: Error) => void
198+
199+
Adds an event listener for when an error occurs.
200+
201+
```js
202+
connection.onError(error => {
203+
console.error(error)
204+
})
205+
```
206+
177207
#### HeosEvent and HeosResponse
178208

179209
The responses to commands are objects like this example:

src/connection/heosConnection.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,24 @@ export class HeosConnection {
105105
this.socket.end('', undefined, resolve)
106106
})
107107
}
108+
109+
/**
110+
* Adds an event listener for when the connection is closed
111+
* @param listener A callback that is called when the connection is closed. `hadError` is true if there was a transmission error.
112+
*/
113+
onClose(listener: (hadError: boolean) => void): HeosConnection {
114+
this.closed = true
115+
this.socket.on('close', listener)
116+
return this
117+
}
118+
119+
/**
120+
* Adds an event listener for when an error occurs.
121+
* @param listener A callback thar is called when an error occurs.
122+
*/
123+
onError(listener: (error: Error) => void): HeosConnection {
124+
this.closed = true
125+
this.socket.on('error', listener)
126+
return this
127+
}
108128
}

0 commit comments

Comments
 (0)