|
| 1 | +// TODO: Rewrite the entire message handling system as a stream transformer? |
| 2 | + |
| 3 | +import { DeviceCommunicationError } from "./Error.js"; |
| 4 | + |
| 5 | +export type DataProvider<TInput> = () => Promise<TInput[] | DeviceCommunicationError>; |
| 6 | +export type InputHandler<TInput> = (input: TInput[]) => IHandlerResponse<TInput>; |
| 7 | +export interface IHandlerResponse<TInput> { |
| 8 | + remainderData?: TInput[]; |
| 9 | +} |
| 10 | + |
| 11 | +export class InputMessageListener<TInput> { |
| 12 | + public start() { |
| 13 | + // We intentionally want to kick off an async promise here without needing |
| 14 | + // the calling function to be async. The promise we're starting is meant to |
| 15 | + // run forever and never resolve until it's being shut down. |
| 16 | + // eslint-disable-next-line no-async-promise-executor |
| 17 | + new Promise<void>(async (_, reject) => { |
| 18 | + let aggregate: TInput[] = []; |
| 19 | + do { |
| 20 | + const data = await this._dataProvider(); |
| 21 | + |
| 22 | + if (data instanceof DeviceCommunicationError) { |
| 23 | + console.error(`Error getting data from source: `, data); |
| 24 | + reject(data); |
| 25 | + break; |
| 26 | + } |
| 27 | + |
| 28 | + if (data.length === 0) { |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + aggregate.push(...data); |
| 33 | + this.logIfDebug(`Got data from provider, now has ${aggregate.length} items in receive buffer. Data: `, data); |
| 34 | + |
| 35 | + // The handler determines if what we got was sufficient, or if we need more. |
| 36 | + const handleResult = this._inputHandler(aggregate); |
| 37 | + if (handleResult.remainderData !== undefined && handleResult.remainderData.length !== 0) { |
| 38 | + this.logIfDebug(`Input handler provided a ${handleResult.remainderData.length} length incomplete buffer.`); |
| 39 | + } |
| 40 | + |
| 41 | + // The handler is not required to be stateful, this is instead. |
| 42 | + // The handler may indicate more data is expected by returning incomplete |
| 43 | + // data back. We prefix our buffer with that incomplete data to add more |
| 44 | + // to it and wait again for more. |
| 45 | + aggregate = handleResult.remainderData ?? []; |
| 46 | + } while (!this._disposed); |
| 47 | + }) |
| 48 | + .catch((reason) => { |
| 49 | + // TODO: Better logging? |
| 50 | + this.logIfDebug(`Device stream listener stopped listening unexpectedly.`, reason); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + private _disposed = false; |
| 55 | + public dispose() { |
| 56 | + if (this._disposed) { return; } |
| 57 | + this._disposed = true; |
| 58 | + } |
| 59 | + |
| 60 | + constructor( |
| 61 | + private readonly _dataProvider: DataProvider<TInput>, |
| 62 | + private readonly _inputHandler: InputHandler<TInput>, |
| 63 | + private readonly _debugLogging: boolean = true, // TODO: false |
| 64 | + ) {} |
| 65 | + |
| 66 | + private logIfDebug(...obj: unknown[]) { |
| 67 | + if (this._debugLogging) { |
| 68 | + console.debug(...obj); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments