1
1
import { sendMessage } from "@coreUtils/utils" ;
2
- import { BackgroundMessage , Fees } from "@models/types" ;
2
+ import {
3
+ BackgroundMessage ,
4
+ BlockPopupMessage ,
5
+ Fees ,
6
+ FeesPopupMessage ,
7
+ PlayBlockNotificationSoundOffscreenMessage
8
+ } from "@models/types" ;
3
9
4
10
import { getSocketUrl } from "./utils/utils" ;
5
11
@@ -13,6 +19,7 @@ let fees: Fees | null;
13
19
let lastBlockTime : number | null ;
14
20
let lastBlockHeight : number | null ;
15
21
let blockNotificationVolume : number = 100 ;
22
+ let blockNotificationSound : string | null = null ;
16
23
17
24
async function checkOffscreenDocumentExist ( ) : Promise < boolean > {
18
25
// Check all windows controlled by the service worker to see if one
@@ -50,8 +57,9 @@ function onOpenSocketHandler(): void {
50
57
socket ?. send ( JSON . stringify ( { action : "init" } ) ) ;
51
58
socket ?. send ( JSON . stringify ( { action : "want" , data : [ "blocks" , "stats" ] } ) ) ;
52
59
53
- chrome . storage . local . get ( [ "blockNotificationVolume" ] ) . then ( ( result ) => {
60
+ chrome . storage . local . get ( [ "blockNotificationVolume" , "blockNotificationSound" ] ) . then ( ( result ) => {
54
61
blockNotificationVolume = result . blockNotificationVolume ?? 100 ;
62
+ blockNotificationSound = result . blockNotificationSound ;
55
63
} ) ;
56
64
}
57
65
@@ -93,15 +101,19 @@ function onBlockMessageHandler(eventData: any): void {
93
101
console . info ( "New block" ) ;
94
102
setupOffscreenDocument ( ) . then ( ( ) => {
95
103
console . debug ( "Send block notification method to offscreen" ) ;
96
- sendMessage ( { data : { volume : blockNotificationVolume } , target : "offscreen" , type : "playBlockNotificationSound" } ) ;
104
+ sendMessage < PlayBlockNotificationSoundOffscreenMessage > ( {
105
+ data : { volume : blockNotificationVolume , sound : blockNotificationSound } ,
106
+ target : "offscreen" ,
107
+ type : "playBlockNotificationSound"
108
+ } ) ;
97
109
} ) ;
98
110
99
111
lastBlockTime = eventData . block . timestamp * 1000 ;
100
112
lastBlockHeight = eventData . block . height ;
101
113
102
114
console . debug ( "Update blocks info with new block" ) ;
103
115
104
- sendMessage ( {
116
+ sendMessage < BlockPopupMessage > ( {
105
117
target : "popup" ,
106
118
data : { blockInfo : { lastBlockTime, lastBlockHeight } } ,
107
119
type : "blockInfo"
@@ -116,7 +128,7 @@ function onFeesMessageHandler(eventData: any): void {
116
128
if ( "fees" in eventData ) {
117
129
console . debug ( "Fee updated" ) ;
118
130
fees = eventData . fees ;
119
- sendMessage ( { target : "popup" , data : { fees } , type : "fees" } ) ;
131
+ sendMessage < FeesPopupMessage > ( { target : "popup" , data : { fees } , type : "fees" } ) ;
120
132
121
133
console . info ( `Current fees: slow: ${ fees ?. hourFee } , medium: ${ fees ?. halfHourFee } , fast: ${ fees ?. fastestFee } ` ) ;
122
134
}
@@ -129,7 +141,7 @@ function onBlocksMessageHandler(eventData: any): void {
129
141
const lastBlock = eventData . blocks . at ( - 1 ) ;
130
142
lastBlockHeight = lastBlock . height ;
131
143
lastBlockTime = lastBlock . timestamp * 1000 ;
132
- sendMessage ( {
144
+ sendMessage < BlockPopupMessage > ( {
133
145
target : "popup" ,
134
146
data : { blockInfo : { lastBlockTime, lastBlockHeight } } ,
135
147
type : "blockInfo"
@@ -197,12 +209,12 @@ function disableBlocksTracking(): void {
197
209
lastBlockTime = null ;
198
210
lastBlockHeight = null ;
199
211
200
- sendMessage ( {
212
+ sendMessage < BlockPopupMessage > ( {
201
213
target : "popup" ,
202
214
data : { blockInfo : { lastBlockTime, lastBlockHeight } } ,
203
215
type : "blockInfo"
204
216
} ) ;
205
- sendMessage ( { target : "popup" , data : { fees } , type : "fees" } ) ;
217
+ sendMessage < FeesPopupMessage > ( { target : "popup" , data : { fees } , type : "fees" } ) ;
206
218
}
207
219
208
220
chrome . storage . local . get ( [ "isTrackingEnabled" , "isMainnet" ] ) . then ( ( result ) => {
@@ -231,15 +243,15 @@ chrome.runtime.onMessage.addListener((message: BackgroundMessage) => {
231
243
}
232
244
} ) ;
233
245
234
- chrome . runtime . onMessage . addListener ( ( message : BackgroundMessage , sender , sendResponse ) => {
246
+ chrome . runtime . onMessage . addListener ( ( message : BackgroundMessage , _ , sendResponse ) => {
235
247
if ( message . target === "background" && message . type === "requestFees" ) {
236
248
console . debug ( "Send initial fees info" ) ;
237
249
238
250
sendResponse ( { target : "popup" , data : { fees } , type : "initialFees" } ) ;
239
251
}
240
252
} ) ;
241
253
242
- chrome . runtime . onMessage . addListener ( ( message : BackgroundMessage , sender , sendResponse ) => {
254
+ chrome . runtime . onMessage . addListener ( ( message : BackgroundMessage , _ , sendResponse ) => {
243
255
if ( message . target === "background" && message . type === "requestLastBlockInfo" ) {
244
256
console . debug ( "Send initial last block info" ) ;
245
257
@@ -258,3 +270,11 @@ chrome.runtime.onMessage.addListener((message: BackgroundMessage) => {
258
270
blockNotificationVolume = message . data . volume ;
259
271
}
260
272
} ) ;
273
+
274
+ chrome . runtime . onMessage . addListener ( ( message : BackgroundMessage ) => {
275
+ if ( message . target === "background" && message . type === "changeBlockNotificationSound" ) {
276
+ console . debug ( "Change block notification sound" ) ;
277
+
278
+ blockNotificationSound = message . data . sound ;
279
+ }
280
+ } ) ;
0 commit comments