Skip to content

Commit 7ee0e04

Browse files
committed
feat: implement controlled timout functionality
1 parent c627051 commit 7ee0e04

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/internal/endpoint-runtime.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export interface EndpointRuntime {
2020
>(
2121
messageID: K,
2222
data: GetDataType<K, JsonValue>,
23-
destination?: Destination
23+
destination?: Destination,
24+
options?: { timeout?: number }
2425
) => Promise<GetReturnType<K, ReturnType>>
2526
onMessage: <Data extends JsonValue, K extends DataTypeKey = DataTypeKey>(
2627
messageID: K,
@@ -143,7 +144,7 @@ export const createEndpointRuntime = (
143144
transactionP?.reject('Transaction was ended before it could complete')
144145
openTransactions.delete(transactionID)
145146
},
146-
sendMessage: (messageID, data, destination = 'background') => {
147+
sendMessage: (messageID, data, destination = 'background', options) => {
147148
const endpoint
148149
= typeof destination === 'string'
149150
? parseEndpoint(destination)
@@ -170,6 +171,15 @@ export const createEndpointRuntime = (
170171

171172
openTransactions.set(payload.transactionId, { resolve, reject })
172173

174+
if (options?.timeout) {
175+
setTimeout(() => {
176+
if (openTransactions.has(payload.transactionId)) {
177+
openTransactions.delete(payload.transactionId)
178+
reject(new Error(`[webext-bridge] Timed out waiting for response to "${messageID}"`))
179+
}
180+
}, options.timeout)
181+
}
182+
173183
try {
174184
handleMessage(payload)
175185
}

0 commit comments

Comments
 (0)