Skip to content

Commit

Permalink
fix(app-shell, app-shell-odd): fix intermittently dropped notifications
Browse files Browse the repository at this point in the history
Because subscription logic is directly tied to the component lifecycle, it is possible for a
component to trigger an unsubscribe event on dismount while a new component mounts and triggers a
subscribe event. For the connection store and MQTT to reflect correct topic subscriptions, do not
unsubscribe and close connections before newly mounted components have had time to update the
connection store.
  • Loading branch information
mjhuff committed Feb 12, 2024
1 parent 180d9ca commit 7073a1c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 40 deletions.
48 changes: 28 additions & 20 deletions app-shell-odd/src/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,30 +184,38 @@ function subscribe(notifyParams: NotifyParams): Promise<void> {
}
}

// Because subscription logic is directly tied to the component lifecycle, it is possible
// for a component to trigger an unsubscribe event on dismount while a new component mounts and
// triggers a subscribe event. For the connection store and MQTT to reflect correct topic subscriptions,
// do not unsubscribe and close connections before newly mounted components have had time to update the connection store.
const RENDER_TIMEOUT = 250

Check warning on line 191 in app-shell-odd/src/notify.ts

View check run for this annotation

Codecov / codecov/patch

app-shell-odd/src/notify.ts#L191

Added line #L191 was not covered by tests

function unsubscribe(notifyParams: NotifyParams): Promise<void> {
const { hostname, topic } = notifyParams
return new Promise<void>(() => {
if (hostname in connectionStore) {
const { client } = connectionStore[hostname]
const subscriptions = connectionStore[hostname]?.subscriptions
const isLastSubscription = subscriptions[topic] <= 1

if (isLastSubscription) {
client?.unsubscribe(topic, {}, (error, result) => {
if (error != null) {
log.warn(
`Failed to unsubscribe on ${hostname} from topic: ${topic}`
)
} else {
log.info(
`Successfully unsubscribed on ${hostname} from topic: ${topic}`
)
handleDecrementSubscriptionCount(hostname, topic)
}
})
} else {
subscriptions[topic] -= 1
}
setTimeout(() => {
const { client } = connectionStore[hostname]
const subscriptions = connectionStore[hostname]?.subscriptions
const isLastSubscription = subscriptions[topic] <= 1

Check warning on line 200 in app-shell-odd/src/notify.ts

View check run for this annotation

Codecov / codecov/patch

app-shell-odd/src/notify.ts#L197-L200

Added lines #L197 - L200 were not covered by tests

if (isLastSubscription) {
client?.unsubscribe(topic, {}, (error, result) => {

Check warning on line 203 in app-shell-odd/src/notify.ts

View check run for this annotation

Codecov / codecov/patch

app-shell-odd/src/notify.ts#L203

Added line #L203 was not covered by tests
if (error != null) {
log.warn(

Check warning on line 205 in app-shell-odd/src/notify.ts

View check run for this annotation

Codecov / codecov/patch

app-shell-odd/src/notify.ts#L205

Added line #L205 was not covered by tests
`Failed to unsubscribe on ${hostname} from topic: ${topic}`
)
} else {
log.info(

Check warning on line 209 in app-shell-odd/src/notify.ts

View check run for this annotation

Codecov / codecov/patch

app-shell-odd/src/notify.ts#L209

Added line #L209 was not covered by tests
`Successfully unsubscribed on ${hostname} from topic: ${topic}`
)
handleDecrementSubscriptionCount(hostname, topic)

Check warning on line 212 in app-shell-odd/src/notify.ts

View check run for this annotation

Codecov / codecov/patch

app-shell-odd/src/notify.ts#L212

Added line #L212 was not covered by tests
}
})
} else {
subscriptions[topic] -= 1

Check warning on line 216 in app-shell-odd/src/notify.ts

View check run for this annotation

Codecov / codecov/patch

app-shell-odd/src/notify.ts#L216

Added line #L216 was not covered by tests
}
}, RENDER_TIMEOUT)
} else {
log.info(
`Attempted to unsubscribe from unconnected hostname: ${hostname}`
Expand Down
48 changes: 28 additions & 20 deletions app-shell/src/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,30 +180,38 @@ function subscribe(notifyParams: NotifyParams): Promise<void> {
}
}

// Because subscription logic is directly tied to the component lifecycle, it is possible
// for a component to trigger an unsubscribe event on dismount while a new component mounts and
// triggers a subscribe event. For the connection store and MQTT to reflect correct topic subscriptions,
// do not unsubscribe and close connections before newly mounted components have had time to update the connection store.
const RENDER_TIMEOUT = 250

Check warning on line 187 in app-shell/src/notify.ts

View check run for this annotation

Codecov / codecov/patch

app-shell/src/notify.ts#L187

Added line #L187 was not covered by tests

function unsubscribe(notifyParams: NotifyParams): Promise<void> {
const { hostname, topic } = notifyParams
return new Promise<void>(() => {
if (hostname in connectionStore) {
const { client } = connectionStore[hostname]
const subscriptions = connectionStore[hostname]?.subscriptions
const isLastSubscription = subscriptions[topic] <= 1

if (isLastSubscription) {
client?.unsubscribe(topic, {}, (error, result) => {
if (error != null) {
log.warn(
`Failed to unsubscribe on ${hostname} from topic: ${topic}`
)
} else {
log.info(
`Successfully unsubscribed on ${hostname} from topic: ${topic}`
)
handleDecrementSubscriptionCount(hostname, topic)
}
})
} else {
subscriptions[topic] -= 1
}
setTimeout(() => {
const { client } = connectionStore[hostname]
const subscriptions = connectionStore[hostname]?.subscriptions
const isLastSubscription = subscriptions[topic] <= 1

Check warning on line 196 in app-shell/src/notify.ts

View check run for this annotation

Codecov / codecov/patch

app-shell/src/notify.ts#L193-L196

Added lines #L193 - L196 were not covered by tests

if (isLastSubscription) {
client?.unsubscribe(topic, {}, (error, result) => {

Check warning on line 199 in app-shell/src/notify.ts

View check run for this annotation

Codecov / codecov/patch

app-shell/src/notify.ts#L199

Added line #L199 was not covered by tests
if (error != null) {
log.warn(

Check warning on line 201 in app-shell/src/notify.ts

View check run for this annotation

Codecov / codecov/patch

app-shell/src/notify.ts#L201

Added line #L201 was not covered by tests
`Failed to unsubscribe on ${hostname} from topic: ${topic}`
)
} else {
log.info(

Check warning on line 205 in app-shell/src/notify.ts

View check run for this annotation

Codecov / codecov/patch

app-shell/src/notify.ts#L205

Added line #L205 was not covered by tests
`Successfully unsubscribed on ${hostname} from topic: ${topic}`
)
handleDecrementSubscriptionCount(hostname, topic)

Check warning on line 208 in app-shell/src/notify.ts

View check run for this annotation

Codecov / codecov/patch

app-shell/src/notify.ts#L208

Added line #L208 was not covered by tests
}
})
} else {
subscriptions[topic] -= 1

Check warning on line 212 in app-shell/src/notify.ts

View check run for this annotation

Codecov / codecov/patch

app-shell/src/notify.ts#L212

Added line #L212 was not covered by tests
}
}, RENDER_TIMEOUT)
} else {
log.info(
`Attempted to unsubscribe from unconnected hostname: ${hostname}`
Expand Down

0 comments on commit 7073a1c

Please sign in to comment.