From 9180bc0aa59558b0fdcbbc3b6098e02cd1fba426 Mon Sep 17 00:00:00 2001 From: Long Zhao Date: Mon, 12 Aug 2024 22:57:30 +1000 Subject: [PATCH] fix: not filter the 1st item from the runQ as it is still processing --- src/CGDGarageDoor.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/CGDGarageDoor.ts b/src/CGDGarageDoor.ts index bab15c4..4f98ad3 100644 --- a/src/CGDGarageDoor.ts +++ b/src/CGDGarageDoor.ts @@ -41,7 +41,10 @@ export class CGDGarageDoor { private withRunQ = async (key: string, fn: () => Promise) => new Promise((resolve, reject) => { this.log.debug('Adding to queue'); - this.runQ = this.runQ.filter((item) => item.name !== key); + + const [item, ...rest] = this.runQ; + this.runQ = [item, ...rest.filter((item) => item.name !== key)]; + this.runQ.push({ name: key, fn: async () => { @@ -55,13 +58,13 @@ export class CGDGarageDoor { }); if (this.runQ.length === 1) { - this.log.debug('Start processing queue'); + this.log.debug('Start RunQ'); this.processRunQ(); } }); private processRunQ = async () => { - this.log.debug('Queue length:', this.runQ.length); + this.log.debug('Start Processing RunQ', this.runQ.length); if (this.runQ.length === 0) { this.log.debug('Queue is empty'); return; @@ -73,6 +76,7 @@ export class CGDGarageDoor { await item.fn(); } finally { this.runQ.shift(); + this.log.debug('Finished Processing RunQ'); this.processRunQ(); } }; @@ -100,7 +104,7 @@ export class CGDGarageDoor { } } - const result = await retry(async () => { + return retry(async () => { this.log.debug(`Running command: ${cmd}=${value}`); const { deviceHostname, deviceLocalKey } = this.config;