Skip to content

Commit 540ce54

Browse files
committed
fix(notifications): dismiss race conditions
- Simplify when to focus the notifications panel for emergency notifications. - Try to limit race conditions when dismissing notifications.
1 parent 4ea6d98 commit 540ce54

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

packages/core/src/notifications/controller.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,20 @@ export class NotificationsController {
9393

9494
await NotificationsNode.instance.setNotifications(startUp, emergency)
9595

96-
// Emergency notifications can't be dismissed, but if the user minimizes the panel then
97-
// we don't want to focus it each time we set the notification nodes.
98-
// So we store it in dismissed once a focus has been fired for it.
99-
const newEmergencies = emergency.map((n) => n.id).filter((id) => !dismissed.has(id))
100-
if (newEmergencies.length > 0) {
101-
this.state.dismissed = [...this.state.dismissed, ...newEmergencies]
102-
await this.writeState()
103-
void this.notificationsNode.focusPanel()
104-
}
105-
10696
// Process on-receive behavior for newly received notifications that passes rule engine
107-
const newlyReceivedToDisplay = [...startUp, ...emergency].filter((n) => this.state.newlyReceived.includes(n.id))
108-
if (newlyReceivedToDisplay.length > 0) {
109-
await this.notificationsNode.onReceiveNotifications(newlyReceivedToDisplay)
97+
const wasNewlyReceived = (n: ToolkitNotification) => this.state.newlyReceived.includes(n.id)
98+
const newStartUp = startUp.filter(wasNewlyReceived)
99+
const newEmergency = emergency.filter(wasNewlyReceived)
100+
const newlyReceived = [...newStartUp, ...newEmergency]
101+
102+
if (newlyReceived.length > 0) {
103+
await this.notificationsNode.onReceiveNotifications(newlyReceived)
110104
// remove displayed notifications from newlyReceived
111-
this.state.newlyReceived = this.state.newlyReceived.filter(
112-
(id) => !newlyReceivedToDisplay.some((n) => n.id === id)
113-
)
105+
this.state.newlyReceived = this.state.newlyReceived.filter((id) => !newlyReceived.some((n) => n.id === id))
114106
await this.writeState()
107+
if (newEmergency.length > 0) {
108+
void this.notificationsNode.focusPanel()
109+
}
115110
}
116111
}
117112

@@ -201,7 +196,7 @@ export class NotificationsController {
201196
*/
202197
private readState() {
203198
const state = this.getDefaultState()
204-
this.state.dismissed = state.dismissed
199+
this.state.dismissed = [...new Set([...this.state.dismissed, ...state.dismissed])]
205200
}
206201

207202
/**

packages/core/src/test/notifications/controller.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ describe('Notifications Controller', function () {
160160
payload: content,
161161
eTag,
162162
},
163-
dismissed: [content.notifications[0].id],
163+
dismissed: [],
164164
newlyReceived: ['id:emergency2'],
165165
})
166166
assert.equal(panelNode.startUpNotifications.length, 0)
@@ -218,7 +218,7 @@ describe('Notifications Controller', function () {
218218
payload: emergencyContent,
219219
eTag: eTag2,
220220
},
221-
dismissed: [emergencyContent.notifications[0].id],
221+
dismissed: [],
222222
newlyReceived: ['id:startup2', 'id:emergency2'],
223223
})
224224
assert.equal(panelNode.startUpNotifications.length, 1)
@@ -415,7 +415,7 @@ describe('Notifications Controller', function () {
415415
payload: emergencyContent,
416416
eTag: '1',
417417
},
418-
dismissed: [emergencyContent.notifications[0].id, startUpContent.notifications[0].id],
418+
dismissed: [startUpContent.notifications[0].id],
419419
newlyReceived: [],
420420
})
421421

@@ -438,7 +438,7 @@ describe('Notifications Controller', function () {
438438
payload: emergencyContent,
439439
eTag: '1',
440440
},
441-
dismissed: [emergencyContent.notifications[0].id],
441+
dismissed: [],
442442
newlyReceived: [],
443443
})
444444
assert.equal(panelNode.getChildren().length, 1)

0 commit comments

Comments
 (0)