Creates a new SerialPortClient instance
Serial port configuration options
Debugging options for the current instance.
StaticisToggle debug mode for all instances.
Default: true if the DEBUG environment variable is set to emittery or *, otherwise false.
import Emittery from 'emittery';
Emittery.isDebugEnabled = true;
const emitter1 = new Emittery({debug: {name: 'myEmitter1'}});
const emitter2 = new Emittery({debug: {name: 'myEmitter2'}});
emitter1.on('test', data => {
// …
});
emitter2.on('otherTest', data => {
// …
});
emitter1.emit('test');
//=> [16:43:20.417][emittery:subscribe][myEmitter1] Event Name: test
// data: undefined
emitter2.emit('otherTest');
//=> [16:43:20.417][emittery:subscribe][myEmitter2] Event Name: otherTest
// data: undefined
Static ReadonlylistenerFires when an event listener was added.
An object with listener and eventName (if on or off was used) is provided as event data.
Static ReadonlylistenerFires when an event listener was removed.
An object with listener and eventName (if on or off was used) is provided as event data.
Get an async iterator which buffers a tuple of an event name and data each time an event is emitted.
Call return() on the iterator to remove the subscription.
In the same way as for events, you can subscribe by using the for await statement.
import Emittery from 'emittery';
const emitter = new Emittery();
const iterator = emitter.anyEvent();
emitter.emit('🦄', '🌈1'); // Buffered
emitter.emit('🌟', '🌈2'); // Buffered
iterator.next()
.then(({value, done}) => {
// done is false
// value is ['🦄', '🌈1']
return iterator.next();
})
.then(({value, done}) => {
// done is false
// value is ['🌟', '🌈2']
// revoke subscription
return iterator.return();
})
.then(({done}) => {
// done is true
});
Bind the given methodNames, or all Emittery methods if methodNames is not defined, into the target object.
OptionalmethodNames: readonly string[]Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
A promise that resolves when all the event listeners are done. Done meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.
Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
A promise that resolves when all the event listeners are done. Done meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.
Same as emit(), but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer emit() whenever possible.
If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will not be called.
A promise that resolves when all the event listeners are done.
Same as emit(), but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer emit() whenever possible.
If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will not be called.
A promise that resolves when all the event listeners are done.
Get an async iterator which buffers data each time an event is emitted.
Call return() on the iterator to remove the subscription.
import Emittery from 'emittery';
const emitter = new Emittery();
const iterator = emitter.events('🦄');
emitter.emit('🦄', '🌈1'); // Buffered
emitter.emit('🦄', '🌈2'); // Buffered
iterator
.next()
.then(({value, done}) => {
// done === false
// value === '🌈1'
return iterator.next();
})
.then(({value, done}) => {
// done === false
// value === '🌈2'
// Revoke subscription
return iterator.return();
})
.then(({done}) => {
// done === true
});
In practice you would usually consume the events using the for await statement. In that case, to revoke the subscription simply break the loop.
import Emittery from 'emittery';
const emitter = new Emittery();
const iterator = emitter.events('🦄');
emitter.emit('🦄', '🌈1'); // Buffered
emitter.emit('🦄', '🌈2'); // Buffered
// In an async context.
for await (const data of iterator) {
if (data === '🌈2') {
break; // Revoke the subscription when we see the value `🌈2`.
}
}
It accepts multiple event names.
import Emittery from 'emittery';
const emitter = new Emittery();
const iterator = emitter.events(['🦄', '🦊']);
emitter.emit('🦄', '🌈1'); // Buffered
emitter.emit('🦊', '🌈2'); // Buffered
iterator
.next()
.then(({value, done}) => {
// done === false
// value === '🌈1'
return iterator.next();
})
.then(({value, done}) => {
// done === false
// value === '🌈2'
// Revoke subscription
return iterator.return();
})
.then(({done}) => {
// done === true
});
Remove one or more event subscriptions.
import Emittery from 'emittery';
const emitter = new Emittery();
const listener = data => {
console.log(data);
};
emitter.on(['🦄', '🐶', '🦊'], listener);
await emitter.emit('🦄', 'a');
await emitter.emit('🐶', 'b');
await emitter.emit('🦊', 'c');
emitter.off('🦄', listener);
emitter.off(['🐶', '🦊'], listener);
await emitter.emit('🦄', 'a'); // nothing happens
await emitter.emit('🐶', 'b'); // nothing happens
await emitter.emit('🦊', 'c'); // nothing happens
Remove an onAny subscription.
Subscribe to one or more events.
Using the same listener multiple times for the same event will result in only one method call per emitted event.
An unsubscribe method.
Subscribe to be notified about any event.
Optionaloptions: { signal?: AbortSignal }A method to unsubscribe.
Subscribe to one or more events only once. It will be unsubscribed after the first event that matches the predicate (if provided).
The event name(s) to subscribe to.
Optionalpredicate: (Optional predicate function to filter event data. The event will only be emitted if the predicate returns true.
The promise of event data when eventName is emitted and predicate matches (if provided). This promise is extended with an off method.
import Emittery from 'emittery';
const emitter = new Emittery();
emitter.once('🦄').then(data => {
console.log(data);
//=> '🌈'
});
emitter.once(['🦄', '🐶']).then(data => {
console.log(data);
});
// With predicate
emitter.once('data', data => data.ok === true).then(data => {
console.log(data);
//=> {ok: true, value: 42}
});
emitter.emit('🦄', '🌈'); // Logs `🌈` twice
emitter.emit('🐶', '🍖'); // Nothing happens
emitter.emit('data', {ok: false}); // Nothing happens
emitter.emit('data', {ok: true, value: 42}); // Logs {ok: true, value: 42}
Open the serial port
Promise that resolves when the port is opened
// Basic usage
const port = new SerialPortClient({
path: 'COM3',
baudRate: 115200
});
await port.open();
// Full configuration
const port2 = new SerialPortClient({
path: '/dev/ttyUSB0',
baudRate: 9600,
dataBits: 8,
stopBits: 1,
parity: 'none',
rtscts: false,
xon: false,
xoff: false
});
await port2.open();
Write data to the serial port
Data to write (Buffer or array of bytes)
Promise that resolves when data is written and drained
const port = new SerialPortClient({ path: 'COM3', baudRate: 115200 });
await port.open();
// Write using Buffer
await port.write(Buffer.from('Hello'));
// Write using byte array
await port.write([0x01, 0x02, 0x03, 0x04]);
// Write hex string as buffer
await port.write(Buffer.from('48454C4C4F', 'hex'));
StaticlistList all available serial ports on the system
Promise that resolves with array of port information
StaticmixinIn TypeScript, it returns a decorator which mixins Emittery as property emitteryPropertyName and methodNames, or all Emittery methods if methodNames is not defined, into the target class.
OptionalmethodNames: readonly string[]
SerialPort client class for serial communication in worker scripts. Provides methods to open, close, read, and write serial ports. Supports multiple simultaneous serial port connections. The port ID is automatically set to the path. Extends Emittery for event-based data handling.
Example