Skip to content

Commit b2c52d7

Browse files
authored
fix: fix dangling download notification (#386)
1 parent 139e0cc commit b2c52d7

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

web/src/store/dispatchers/build.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,19 @@ const fetchWasmWithProgress = async (dispatch: DispatchFn, fileName: string) =>
121121
cancelAnimationFrame(prevRafID)
122122
prevRafID = requestAnimationFrame(() => {
123123
dispatch(
124-
newAddNotificationAction({
125-
id: NotificationIDs.WASMAppDownload,
126-
type: NotificationType.Info,
127-
title: 'Downloading compiled application',
128-
canDismiss: false,
129-
progress: {
130-
total: totalBytes,
131-
current: currentBytes,
124+
newAddNotificationAction(
125+
{
126+
id: NotificationIDs.WASMAppDownload,
127+
type: NotificationType.Info,
128+
title: 'Downloading compiled application',
129+
canDismiss: false,
130+
progress: {
131+
total: totalBytes,
132+
current: currentBytes,
133+
},
132134
},
133-
}),
135+
true,
136+
),
134137
)
135138
})
136139
})

web/src/store/notifications/actions.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ import { type Notification } from './state'
33
export enum ActionType {
44
NEW_NOTIFICATION = 'NEW_NOTIFICATION',
55
ADD_NOTIFICATIONS = 'ADD_NOTIFICATIONS',
6+
UPDATE_NOTIFICATION = 'UPDATE_NOTIFICATION',
67
REMOVE_NOTIFICATION = 'REMOVE_NOTIFICATION',
78
}
89

910
export const newNotificationId = () => `${Date.now().toString(36)}_${Math.random().toString(36).substring(2)}`
1011

11-
export const newAddNotificationAction = (notification: Notification) => ({
12-
type: ActionType.NEW_NOTIFICATION,
12+
/**
13+
* Returns an action to create or update a notification by ID.
14+
*
15+
* @param notification Notification body.
16+
* @param updateOnly Skip if notification ID doesn't exist
17+
*/
18+
export const newAddNotificationAction = (notification: Notification, updateOnly = false) => ({
19+
type: updateOnly ? ActionType.UPDATE_NOTIFICATION : ActionType.NEW_NOTIFICATION,
1320
payload: notification,
1421
})
1522

web/src/store/notifications/reducers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ const reducers = mapByAction<NotificationsState>(
1313
...s,
1414
...Object.fromEntries(payload.map((n) => [n.id, n])),
1515
}),
16+
[ActionType.UPDATE_NOTIFICATION]: (s: NotificationsState, { payload }: Action<Notification>) => {
17+
if (!s[payload.id]) {
18+
return s
19+
}
20+
21+
return { ...s, [payload.id]: payload }
22+
},
1623
[ActionType.REMOVE_NOTIFICATION]: (s: NotificationsState, { payload }: Action<string>) => {
1724
const newNotifications = { ...s }
1825
delete newNotifications[payload]

0 commit comments

Comments
 (0)