Skip to content

Commit

Permalink
fix(app-shell, app-shell-odd): Fix notification fallback for multiple…
Browse files Browse the repository at this point in the history
… subscriptions (#14545)

Closes RQA-2376
  • Loading branch information
mjhuff authored Feb 26, 2024
1 parent 1556c1c commit 10c0e77
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 86 deletions.
87 changes: 44 additions & 43 deletions app-shell-odd/src/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export function registerNotify(
}
}

const CHECK_CONNECTION_INTERVAL = 500

interface NotifyParams {
browserWindow: BrowserWindow
hostname: string
Expand Down Expand Up @@ -115,24 +117,21 @@ function subscribe(notifyParams: NotifyParams): Promise<void> {
// 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<void>(() => 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 {
Expand All @@ -151,36 +150,38 @@ function subscribe(notifyParams: NotifyParams): Promise<void> {
}

// 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<void>(() =>
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<void> {
return new Promise<void>((resolve, reject) => {
const MAX_RETRIES = 4
let counter = 0
const intervalId = setInterval(() => {
const client = connectionStore[hostname]?.client
if (client != null) {
clearInterval(intervalId)
new Promise<void>(() =>
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)
})
}
}

Expand Down
87 changes: 44 additions & 43 deletions app-shell/src/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export function registerNotify(
}
}

const CHECK_CONNECTION_INTERVAL = 500

interface NotifyParams {
browserWindow: BrowserWindow
hostname: string
Expand Down Expand Up @@ -111,24 +113,21 @@ function subscribe(notifyParams: NotifyParams): Promise<void> {
// 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<void>(() => 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 {
Expand All @@ -147,36 +146,38 @@ function subscribe(notifyParams: NotifyParams): Promise<void> {
}

// 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<void>(() =>
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<void> {
return new Promise<void>((resolve, reject) => {
const MAX_RETRIES = 4
let counter = 0
const intervalId = setInterval(() => {
const client = connectionStore[hostname]?.client
if (client != null) {
clearInterval(intervalId)
new Promise<void>(() =>
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)
})
}
}

Expand Down

0 comments on commit 10c0e77

Please sign in to comment.