From 9f26834eefebad491d69aea9d8ec6f621fcb8fa8 Mon Sep 17 00:00:00 2001 From: Fernando Chorney Date: Sat, 6 Apr 2024 13:25:13 -0500 Subject: [PATCH] Output Events: Gross but works, holdWhile + bool --- sdk/smx.ts | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/sdk/smx.ts b/sdk/smx.ts index 6451d06..605d399 100644 --- a/sdk/smx.ts +++ b/sdk/smx.ts @@ -15,6 +15,8 @@ class SMXEvents { otherReports$: Bacon.EventStream; output$; + okSendStatus = true; + constructor(dev: HIDDevice) { this.dev = dev; @@ -44,14 +46,18 @@ class SMXEvents { // this.otherReports$.onValue((value) => console.log("Packet: ", value)); finishedCommand$.log("Cmd Finished"); + finishedCommand$.onValue((e) => { + this.okSendStatus = e; + }); // we write a `true` to this whenever a series of packets is going out to the device this.startedSend$ = new Bacon.Bus(); // true means "it's ok to send", false means "don't send" - const okSend$ = finishedCommand$ // Returns true when host_cmd_finished - .merge(this.startedSend$.not()) // Return false when starting to send - .toProperty(true); + const okSendBus$ = finishedCommand$ // Returns true when host_cmd_finished + .merge(this.startedSend$.not()); // Return false when starting to send + + const okSend$ = okSendBus$.toProperty(true); // Main USB Output this.output$ = new Bacon.Bus>(); @@ -65,16 +71,24 @@ class SMXEvents { // combine together the throttled and unthrottled writes // TODO find an alternative to using `bufferedThrottle` here // (seemingly takeWhile lets too much through via race conditions against the okSend property changing) - const eventsToSend$ = configOutput$.merge(otherOutput$).bufferingThrottle(100).takeWhile(okSend$); + //const eventsToSend$ = configOutput$.merge(otherOutput$).bufferingThrottle(100).takeWhile(okSend$); + + const eventsToSend$ = configOutput$.merge(otherOutput$).holdWhen(okSend$.not()); eventsToSend$.onValue(async (value) => { - this.startedSend$.push(true); - await this.writeToHID(value); + if (!this.okSendStatus) { + // Push back onto the stream + this.output$.push(value); + } else { + this.startedSend$.push(true); + this.okSendStatus = false; // Set this here so it's *Fast Enough* to affect the next event + console.log("writing to HID"); + await this.writeToHID(value); + } }); } private async writeToHID(value: Array) { - console.log("writing to HID"); await send_data(this.dev, value); } }