Skip to content

Commit 0648a21

Browse files
psyiriusantfu
andauthored
fix: timeoutId compatible for browser-like environments (#16)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent 34ced14 commit 0648a21

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export function createBirpc<RemoteFunctions = Record<string, never>, LocalFuncti
160160
timeout = DEFAULT_TIMEOUT,
161161
} = options
162162

163-
const rpcPromiseMap = new Map<string, { resolve: (arg: any) => void, reject: (error: any) => void, timeoutId: Parameters<typeof clearTimeout>[0] }>()
163+
const rpcPromiseMap = new Map<string, { resolve: (arg: any) => void, reject: (error: any) => void, timeoutId?: ReturnType<typeof setTimeout> }>()
164164

165165
let _promise: Promise<any> | any
166166

@@ -185,7 +185,7 @@ export function createBirpc<RemoteFunctions = Record<string, never>, LocalFuncti
185185
await _promise
186186
return new Promise((resolve, reject) => {
187187
const id = nanoid()
188-
let timeoutId
188+
let timeoutId: ReturnType<typeof setTimeout> | undefined
189189

190190
if (timeout >= 0) {
191191
timeoutId = setTimeout(() => {
@@ -198,7 +198,11 @@ export function createBirpc<RemoteFunctions = Record<string, never>, LocalFuncti
198198
reject(e)
199199
}
200200
rpcPromiseMap.delete(id)
201-
}, timeout).unref?.()
201+
}, timeout)
202+
203+
// For node.js, `unref` is not available in browser-like environments
204+
if (typeof timeoutId === 'object')
205+
timeoutId = timeoutId.unref?.()
202206
}
203207

204208
rpcPromiseMap.set(id, { resolve, reject, timeoutId })

0 commit comments

Comments
 (0)