Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app-shell, app-shell-odd): Fix intermittently dropped notifications #14477

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}
}

// 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 = 10000 // 10 seconds

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 @@
}
}

// 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 = 10000 // 10 seconds

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
Loading