@splcode/pod-abstract-driver
v3.0.0
Published
Podcart driver base class with injected runtime context
Readme
@splcode/pod-abstract-driver
@splcode/pod-abstract-driver is the shared Podcart driver base class. It is self-contained and no longer depends on @splcode/state-server.
What changed
this.eventBusis injected per runtime instance.- The old top-level
eventBusexport is gone. - Host defaults such as
syncIntervalMsare merged through the fourth constructor argument.
Driver usage
import { PodcartAbstractDriver } from '@splcode/pod-abstract-driver';
export default class MyDriver extends PodcartAbstractDriver {
async _connect() {
this._registerMeter('enabled', false);
this._onPluginEvent = (event) => {
if (event.deviceId !== this.name) return;
if (event.channel === 'enabled.toggle') {
this._meterEcho('enabled', !event.payload?.value);
}
};
this.eventBus.on('plugin:event:enabled.toggle', this._onPluginEvent);
}
async _disconnect() {
this.eventBus.off('plugin:event:enabled.toggle', this._onPluginEvent);
}
}Host usage
The host creates one event bus per server process and passes it into every driver instance:
const hostContext = {
defaultDriverConfig: {
syncIntervalMs: 5000,
setThrottleMs: 50,
unlockMeterDebounceMs: 100,
},
eventBus: new EventEmitter(),
};
const device = new Driver('lighting', driverConfig, deviceConfig, hostContext);Examples
- examples/host-runtime.mjs shows the host-side constructor contract.
- examples/plugin-driver.js shows the backend event-bus contract a generated plugin should follow.
