Skip to content

Commit

Permalink
DRY up the sendToBrowser logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhuff committed Mar 15, 2024
1 parent 9b2cb25 commit 785744c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 33 deletions.
22 changes: 5 additions & 17 deletions app-shell/src/notifications/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mqtt from 'mqtt'

import { connectionStore } from './store'
import {
sendDeserialized,
sendDeserializedGenericError,
deserializeExpectedMessages,
} from './deserialize'
Expand Down Expand Up @@ -135,15 +136,8 @@ function establishListeners({
hostname,
topic,
})
try {
const browserWindow = connectionStore.getBrowserWindow()
browserWindow?.webContents.send(
'notify',
hostname,
topic,
deserializedMessage
)
} catch {} // Prevents shell erroring during app shutdown event.

sendDeserialized({ hostname, topic, message: deserializedMessage })
})
.catch(error => notifyLog.debug(`${error.message}`))
}
Expand All @@ -155,10 +149,7 @@ function establishListeners({
// handles transport layer errors only
client.on('error', error => {
notifyLog.warn(`Error - ${error.name}: ${error.message}`)
sendDeserializedGenericError({
hostname,
topic: 'ALL_TOPICS',
})
sendDeserializedGenericError(hostname, 'ALL_TOPICS')
client.end()
})

Expand All @@ -173,10 +164,7 @@ function establishListeners({
packet.reasonCode ?? 'undefined'
}`
)
sendDeserializedGenericError({
hostname,
topic: 'ALL_TOPICS',
})
sendDeserializedGenericError(hostname, 'ALL_TOPICS')
})
}

Expand Down
8 changes: 4 additions & 4 deletions app-shell/src/notifications/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export function sendDeserialized({
} catch {} // Prevents shell erroring during app shutdown event.
}

export function sendDeserializedGenericError({
hostname,
topic,
}: Omit<SendToBrowserParams, 'message'>): void {
export function sendDeserializedGenericError(
hostname: string,
topic: NotifyTopic
): void {
sendDeserialized({
hostname,
topic,
Expand Down
15 changes: 3 additions & 12 deletions app-shell/src/notifications/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,20 @@ export function subscribe({
void waitUntilActiveOrErrored('subscription').catch(
(error: Error) => {
notifyLog.debug(error.message)
sendDeserializedGenericError({
hostname,
topic,
})
sendDeserializedGenericError(hostname, topic)
}
)
}
})
.catch((error: Error) => {
notifyLog.debug(error.message)
sendDeserializedGenericError({
hostname,
topic,
})
sendDeserializedGenericError(hostname, topic)
})
}

function subscribeCb(error: Error, result: mqtt.ISubscriptionGrant[]): void {
if (error != null) {
sendDeserializedGenericError({
hostname,
topic,
})
sendDeserializedGenericError(hostname, topic)
} else {
notifyLog.debug(
`Successfully subscribed on ${hostname} to topic: ${topic}`
Expand Down

0 comments on commit 785744c

Please sign in to comment.