From 10c0e77ed62b86c3a45cdad90130f7fe8c1baba4 Mon Sep 17 00:00:00 2001 From: Jamey Huffnagle Date: Mon, 26 Feb 2024 11:34:15 -0500 Subject: [PATCH] fix(app-shell, app-shell-odd): Fix notification fallback for multiple subscriptions (#14545) Closes RQA-2376 --- app-shell-odd/src/notify.ts | 87 +++++++++++++++++++------------------ app-shell/src/notify.ts | 87 +++++++++++++++++++------------------ 2 files changed, 88 insertions(+), 86 deletions(-) diff --git a/app-shell-odd/src/notify.ts b/app-shell-odd/src/notify.ts index 0cb948e7bcb..77ad4e170fd 100644 --- a/app-shell-odd/src/notify.ts +++ b/app-shell-odd/src/notify.ts @@ -71,6 +71,8 @@ export function registerNotify( } } +const CHECK_CONNECTION_INTERVAL = 500 + interface NotifyParams { browserWindow: BrowserWindow hostname: string @@ -115,24 +117,21 @@ function subscribe(notifyParams: NotifyParams): Promise { // true if the connection store has an entry for the hostname. else { const subscriptions = connectionStore[hostname]?.subscriptions - if (subscriptions && subscriptions[topic] > 0) { - subscriptions[topic] += 1 - return Promise.resolve() - } else { - if (subscriptions) { - subscriptions[topic] = 1 - } - return new Promise(() => checkIfClientConnected()).catch(() => { - log.warn(`Failed to subscribe on ${hostname} to topic: ${topic}`) - sendToBrowserDeserialized({ - browserWindow, - hostname, - topic, - message: 'ECONNFAILED', - }) - handleDecrementSubscriptionCount(hostname, topic) - }) + if (subscriptions) { + if (subscriptions[topic] > 0) subscriptions[topic] += 1 + else subscriptions[topic] = 1 } + + return checkIfClientConnected().catch(() => { + log.warn(`Failed to subscribe on ${hostname} to topic: ${topic}`) + sendToBrowserDeserialized({ + browserWindow, + hostname, + topic, + message: 'ECONNFAILED', + }) + handleDecrementSubscriptionCount(hostname, topic) + }) } function subscribeCb(error: Error, result: mqtt.ISubscriptionGrant[]): void { @@ -151,36 +150,38 @@ function subscribe(notifyParams: NotifyParams): Promise { } // Check every 500ms for 2 seconds before failing. - function checkIfClientConnected(): void { - const MAX_RETRIES = 4 - let counter = 0 - const intervalId = setInterval(() => { - const client = connectionStore[hostname]?.client - if (client != null) { - clearInterval(intervalId) - new Promise(() => - client.subscribe(topic, subscribeOptions, subscribeCb) - ) - .then(() => Promise.resolve()) - .catch(() => - Promise.reject( - new Error( - `Maximum number of subscription retries reached for hostname: ${hostname}` + function checkIfClientConnected(): Promise { + return new Promise((resolve, reject) => { + const MAX_RETRIES = 4 + let counter = 0 + const intervalId = setInterval(() => { + const client = connectionStore[hostname]?.client + if (client != null) { + clearInterval(intervalId) + new Promise(() => + client.subscribe(topic, subscribeOptions, subscribeCb) + ) + .then(() => resolve()) + .catch(() => + reject( + new Error( + `Maximum number of subscription retries reached for hostname: ${hostname}` + ) ) ) - ) - } + } - counter++ - if (counter === MAX_RETRIES) { - clearInterval(intervalId) - Promise.reject( - new Error( - `Maximum number of subscription retries reached for hostname: ${hostname}` + counter++ + if (counter === MAX_RETRIES) { + clearInterval(intervalId) + reject( + new Error( + `Maximum number of subscription retries reached for hostname: ${hostname}` + ) ) - ) - } - }, 500) + } + }, CHECK_CONNECTION_INTERVAL) + }) } } diff --git a/app-shell/src/notify.ts b/app-shell/src/notify.ts index a407cb0bab2..33f1fcd8ff4 100644 --- a/app-shell/src/notify.ts +++ b/app-shell/src/notify.ts @@ -67,6 +67,8 @@ export function registerNotify( } } +const CHECK_CONNECTION_INTERVAL = 500 + interface NotifyParams { browserWindow: BrowserWindow hostname: string @@ -111,24 +113,21 @@ function subscribe(notifyParams: NotifyParams): Promise { // true if the connection store has an entry for the hostname. else { const subscriptions = connectionStore[hostname]?.subscriptions - if (subscriptions && subscriptions[topic] > 0) { - subscriptions[topic] += 1 - return Promise.resolve() - } else { - if (subscriptions) { - subscriptions[topic] = 1 - } - return new Promise(() => checkIfClientConnected()).catch(() => { - log.warn(`Failed to subscribe on ${hostname} to topic: ${topic}`) - sendToBrowserDeserialized({ - browserWindow, - hostname, - topic, - message: 'ECONNFAILED', - }) - handleDecrementSubscriptionCount(hostname, topic) - }) + if (subscriptions) { + if (subscriptions[topic] > 0) subscriptions[topic] += 1 + else subscriptions[topic] = 1 } + + return checkIfClientConnected().catch(() => { + log.warn(`Failed to subscribe on ${hostname} to topic: ${topic}`) + sendToBrowserDeserialized({ + browserWindow, + hostname, + topic, + message: 'ECONNFAILED', + }) + handleDecrementSubscriptionCount(hostname, topic) + }) } function subscribeCb(error: Error, result: mqtt.ISubscriptionGrant[]): void { @@ -147,36 +146,38 @@ function subscribe(notifyParams: NotifyParams): Promise { } // Check every 500ms for 2 seconds before failing. - function checkIfClientConnected(): void { - const MAX_RETRIES = 4 - let counter = 0 - const intervalId = setInterval(() => { - const client = connectionStore[hostname]?.client - if (client != null) { - clearInterval(intervalId) - new Promise(() => - client.subscribe(topic, subscribeOptions, subscribeCb) - ) - .then(() => Promise.resolve()) - .catch(() => - Promise.reject( - new Error( - `Maximum number of subscription retries reached for hostname: ${hostname}` + function checkIfClientConnected(): Promise { + return new Promise((resolve, reject) => { + const MAX_RETRIES = 4 + let counter = 0 + const intervalId = setInterval(() => { + const client = connectionStore[hostname]?.client + if (client != null) { + clearInterval(intervalId) + new Promise(() => + client.subscribe(topic, subscribeOptions, subscribeCb) + ) + .then(() => resolve()) + .catch(() => + reject( + new Error( + `Maximum number of subscription retries reached for hostname: ${hostname}` + ) ) ) - ) - } + } - counter++ - if (counter === MAX_RETRIES) { - clearInterval(intervalId) - Promise.reject( - new Error( - `Maximum number of subscription retries reached for hostname: ${hostname}` + counter++ + if (counter === MAX_RETRIES) { + clearInterval(intervalId) + reject( + new Error( + `Maximum number of subscription retries reached for hostname: ${hostname}` + ) ) - ) - } - }, 500) + } + }, CHECK_CONNECTION_INTERVAL) + }) } }