forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
63 lines (59 loc) · 2.24 KB
/
Copy pathcommon.ts
File metadata and controls
63 lines (59 loc) · 2.24 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
60
61
62
63
import { handleError } from '../chrome/common'
/**
* Fix proxy data failed to serialized in Firefox
*/
function cloneData<T = any>(data: T | undefined): T | undefined {
if (data === undefined) return undefined
try {
return JSON.parse(JSON.stringify(data))
} catch (cloneError) {
console.warn("Failed clone data", cloneError)
return data
}
}
type RuntimeMsgArgs<C extends tt4b.mq.ReqCode> = [tt4b.mq.ReqData<C>] extends [undefined]
? [data?: tt4b.mq.ReqData<C>, timeout_ms?: number]
: [data: tt4b.mq.ReqData<C>, timeout_ms?: number]
export function sendMsg2Runtime<C extends tt4b.mq.ReqCode>(
code: C,
...args: RuntimeMsgArgs<C>
): Promise<tt4b.mq.ResData<C>> {
const [data, timeout_ms] = args
const request: tt4b.mq.Request<C> = { code, data: cloneData(data) as tt4b.mq.ReqData<C> }
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
// timeout: no response from runtime
reject('sendMsg2Runtime timeout')
}, timeout_ms ?? 10_000)
try {
chrome.runtime.sendMessage(request, (response: tt4b.mq.Response<C>) => {
clearTimeout(timeout)
handleError('sendMsg2Runtime')
const resCode = response?.code
if (resCode === 'fail') {
console.warn("Error occurred when querying service-worker", code, data, response?.msg)
return reject(new Error(response?.msg || 'Unknown error'))
}
resCode === 'success' && resolve(response.data as tt4b.mq.ResData<C>)
})
} catch (e) {
clearTimeout(timeout)
const msg = e instanceof Error ? e.message : 'Unknown error'
reject(`Failed to send message: ${msg}`)
}
})
}
/**
* Wrap for hooks, after the extension reloaded or upgraded, the context of current content script will be invalid
* And sending messages to the runtime will be failed
*/
export async function trySendMsg2Runtime<C extends tt4b.mq.ReqCode>(
code: C,
...args: RuntimeMsgArgs<C>
): Promise<tt4b.mq.ResData<C> | undefined> {
try {
return await sendMsg2Runtime(code, ...args)
} catch {
return undefined
}
}