mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-25 18:37:19 +00:00
Hello,
I have a use case where size of an application is important, I'd like to
webpack as much as possible. The problematic part is related *.node
files, I found a potential solution, example with sqlite:
```
externals: {
'./sqlite3-binding.js': 'commonjs ./node_sqlite3.node',
}
```
I can replace sqlite3-binding.js [1] with *.node, then I copy
node_sqlite3.node to the main application folder. I'd like to try
something similar with the serialport. However, it is slightly more
complicated since serialport doesn't have single import file. This PR is
an attempt to fix the problem.
[1]
https://github.com/TryGhost/node-sqlite3/blob/master/lib/sqlite3-binding.js
---------
Co-authored-by: Krzysztof Rosiński <krzysiek@jrdltd.co.uk>
116 lines
2.9 KiB
TypeScript
116 lines
2.9 KiB
TypeScript
import debug from 'debug'
|
|
import { EventEmitter } from 'events'
|
|
import { BindingsError } from './errors'
|
|
import { binding } from './serialport-bindings'
|
|
|
|
const { Poller: PollerBindings } = binding as { Poller: PollerClass }
|
|
const logger = debug('serialport/bindings-cpp/poller')
|
|
|
|
interface PollerClass {
|
|
new(fd: number, cb: (err: Error, flag: number)=> void): PollerInstance
|
|
}
|
|
|
|
interface PollerInstance {
|
|
poll(flag: number): void
|
|
stop(): void
|
|
destroy(): void
|
|
}
|
|
|
|
export const EVENTS = {
|
|
UV_READABLE: 0b0001,
|
|
UV_WRITABLE: 0b0010,
|
|
UV_DISCONNECT: 0b0100,
|
|
}
|
|
|
|
function handleEvent(error: Error, eventFlag: number) {
|
|
if (error) {
|
|
logger('error', error)
|
|
this.emit('readable', error)
|
|
this.emit('writable', error)
|
|
this.emit('disconnect', error)
|
|
return
|
|
}
|
|
if (eventFlag & EVENTS.UV_READABLE) {
|
|
logger('received "readable"')
|
|
this.emit('readable', null)
|
|
}
|
|
if (eventFlag & EVENTS.UV_WRITABLE) {
|
|
logger('received "writable"')
|
|
this.emit('writable', null)
|
|
}
|
|
if (eventFlag & EVENTS.UV_DISCONNECT) {
|
|
logger('received "disconnect"')
|
|
this.emit('disconnect', null)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Polls unix systems for readable or writable states of a file or serialport
|
|
*/
|
|
export class Poller extends EventEmitter {
|
|
poller: PollerInstance
|
|
constructor(fd: number, FDPoller: PollerClass = PollerBindings) {
|
|
logger('Creating poller')
|
|
super()
|
|
this.poller = new FDPoller(fd, handleEvent.bind(this))
|
|
}
|
|
/**
|
|
* Wait for the next event to occur
|
|
* @param {string} event ('readable'|'writable'|'disconnect')
|
|
* @returns {Poller} returns itself
|
|
*/
|
|
once(event: 'readable' | 'writable' | 'disconnect', callback: (err: null | Error) => void): this {
|
|
switch (event) {
|
|
case 'readable':
|
|
this.poll(EVENTS.UV_READABLE)
|
|
break
|
|
case 'writable':
|
|
this.poll(EVENTS.UV_WRITABLE)
|
|
break
|
|
case 'disconnect':
|
|
this.poll(EVENTS.UV_DISCONNECT)
|
|
break
|
|
}
|
|
return super.once(event, callback)
|
|
}
|
|
|
|
/**
|
|
* Ask the bindings to listen for an event, it is recommend to use `.once()` for easy use
|
|
* @param {EVENTS} eventFlag polls for an event or group of events based upon a flag.
|
|
*/
|
|
poll(eventFlag = 0) {
|
|
if (eventFlag & EVENTS.UV_READABLE) {
|
|
logger('Polling for "readable"')
|
|
}
|
|
if (eventFlag & EVENTS.UV_WRITABLE) {
|
|
logger('Polling for "writable"')
|
|
}
|
|
if (eventFlag & EVENTS.UV_DISCONNECT) {
|
|
logger('Polling for "disconnect"')
|
|
}
|
|
|
|
this.poller.poll(eventFlag)
|
|
}
|
|
|
|
/**
|
|
* Stop listening for events and cancel all outstanding listening with an error
|
|
*/
|
|
stop(): void {
|
|
logger('Stopping poller')
|
|
this.poller.stop()
|
|
this.emitCanceled()
|
|
}
|
|
|
|
destroy(): void {
|
|
logger('Destroying poller')
|
|
this.poller.destroy()
|
|
this.emitCanceled()
|
|
}
|
|
|
|
emitCanceled(): void {
|
|
const err = new BindingsError('Canceled', { canceled: true })
|
|
this.emit('readable', err)
|
|
this.emit('writable', err)
|
|
this.emit('disconnect', err)
|
|
}
|
|
}
|