diff --git a/src/client/abstract_client.ts b/src/client/abstract_client.ts index 5bb8b9e..7f9409a 100644 --- a/src/client/abstract_client.ts +++ b/src/client/abstract_client.ts @@ -100,7 +100,7 @@ export abstract class NiimbotAbstractClient extends EventEmitter * * @throws {@link PrintError} when {@link ResponseCommandId.In_PrintError} or {@link ResponseCommandId.In_NotSupported} received. * - * @returns {NiimbotPacket} Printer response object. + * @returns {NiimbotPacket} Packet object. */ public async sendPacketWaitResponse(packet: NiimbotPacket, timeoutMs: number = 1000): Promise { return this.mutex.runExclusive(async () => { @@ -110,40 +110,57 @@ export abstract class NiimbotAbstractClient extends EventEmitter return new NiimbotPacket(ResponseCommandId.Invalid, []); // or undefined is better? } - return new Promise((resolve, reject) => { - let timeout: NodeJS.Timeout | undefined = undefined; - - const listener = (evt: PacketReceivedEvent) => { - const pktIn = evt.packet; - const cmdIn = pktIn.command as ResponseCommandId; - - if ( - packet.validResponseIds.length === 0 || - packet.validResponseIds.includes(cmdIn) || - [ResponseCommandId.In_PrintError, ResponseCommandId.In_NotSupported].includes(cmdIn) - ) { - clearTimeout(timeout); - this.off("packetreceived", listener); - - if (cmdIn === ResponseCommandId.In_PrintError) { - Validators.u8ArrayLengthEquals(pktIn.data, 1); - const errorName = PrinterErrorCode[pktIn.data[0]] ?? "unknown"; - reject(new PrintError(`Print error ${pktIn.data[0]}: ${errorName}`, pktIn.data[0])); - } else if (cmdIn === ResponseCommandId.In_NotSupported) { - reject(new PrintError("Feature not supported", 0)); - } else { - resolve(pktIn); - } - } - }; + return this.waitForPacket(packet.validResponseIds, true, timeoutMs); + }); + } - timeout = setTimeout(() => { + /** + * Send wait for response for {@link timeoutMs} milliseconds. + * + * If {@link ids} is set, it will wait for packet with this command ids. + * + * @throws {@link PrintError} when {@link ResponseCommandId.In_PrintError} or {@link ResponseCommandId.In_NotSupported} received and {@link catchErrorPackets} is true. + * + * @returns {NiimbotPacket} Packet object. + */ + public async waitForPacket( + ids: ResponseCommandId[] = [], + catchErrorPackets: boolean = true, + timeoutMs: number = 1000 + ): Promise { + return new Promise((resolve, reject) => { + let timeout: NodeJS.Timeout | undefined = undefined; + + const listener = (evt: PacketReceivedEvent) => { + const pktIn = evt.packet; + const cmdIn = pktIn.command as ResponseCommandId; + + if ( + ids.length === 0 || + ids.includes(cmdIn) || + (catchErrorPackets && [ResponseCommandId.In_PrintError, ResponseCommandId.In_NotSupported].includes(cmdIn)) + ) { + clearTimeout(timeout); this.off("packetreceived", listener); - reject(new Error(`Timeout waiting response (waited for ${Utils.bufToHex(packet.validResponseIds, ", ")})`)); - }, timeoutMs ?? 1000); - this.on("packetreceived", listener); - }); + if (cmdIn === ResponseCommandId.In_PrintError) { + Validators.u8ArrayLengthEquals(pktIn.data, 1); + const errorName = PrinterErrorCode[pktIn.data[0]] ?? "unknown"; + reject(new PrintError(`Print error ${pktIn.data[0]}: ${errorName}`, pktIn.data[0])); + } else if (cmdIn === ResponseCommandId.In_NotSupported) { + reject(new PrintError("Feature not supported", 0)); + } else { + resolve(pktIn); + } + } + }; + + timeout = setTimeout(() => { + this.off("packetreceived", listener); + reject(new Error(`Timeout waiting response (waited for ${Utils.bufToHex(ids, ", ")})`)); + }, timeoutMs ?? 1000); + + this.on("packetreceived", listener); }); } @@ -156,8 +173,6 @@ export abstract class NiimbotAbstractClient extends EventEmitter return; } - console.log("processRawPacket") - if (data instanceof DataView) { data = new Uint8Array(data.buffer); }