-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathAudioPlayerNotificationsPlugin.ts
More file actions
59 lines (52 loc) · 1.79 KB
/
AudioPlayerNotificationsPlugin.ts
File metadata and controls
59 lines (52 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { AudioPlayerPlugin } from './AudioPlayerPlugin';
import { type AudioPlayerErrorCode } from '../AudioPlayer';
import type { TFunction } from 'i18next';
import type { AddNotification } from '../../Notifications/hooks/useNotificationApi';
import type { NotificationTargetPanel } from '../../Notifications/notificationTarget';
const SEEK_NOT_SUPPORTED_NOTIFICATION_DEBOUNCE_INTERVAL_MS = 1000;
export const audioPlayerNotificationsPluginFactory = ({
addNotification,
panel = 'channel',
t,
}: {
addNotification: AddNotification;
panel?: NotificationTargetPanel;
t: TFunction;
}): AudioPlayerPlugin => {
const errors: Record<AudioPlayerErrorCode, Error> = {
'failed-to-start': new Error(t('Failed to play the recording')),
'not-playable': new Error(
t('Recording format is not supported and cannot be reproduced'),
),
'seek-not-supported': new Error(t('Cannot seek in the recording')),
};
let lastSeekNotSupportedNotificationAt: number | undefined;
return {
id: 'AudioPlayerNotificationsPlugin',
onError: ({ errCode, error: e }) => {
if (errCode === 'seek-not-supported') {
const now = Date.now();
if (
typeof lastSeekNotSupportedNotificationAt === 'number' &&
now - lastSeekNotSupportedNotificationAt <
SEEK_NOT_SUPPORTED_NOTIFICATION_DEBOUNCE_INTERVAL_MS
) {
return;
}
lastSeekNotSupportedNotificationAt = now;
}
const error =
(errCode && errors[errCode]) ??
e ??
new Error(t('Error reproducing the recording'));
addNotification({
emitter: 'AudioPlayer',
error,
message: error.message,
severity: 'error',
targetPanels: [panel],
type: 'browser:audio:playback:error',
});
},
};
};