Skip to content

Commit 755abb3

Browse files
committed
use safeUnref in offline transport
1 parent 35ace69 commit 755abb3

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

packages/core/src/transports/offline.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { InternalBaseTransportOptions, Transport, TransportMakeRequestRespo
44
import { debug } from '../utils/debug-logger';
55
import { envelopeContainsItemType } from '../utils/envelope';
66
import { parseRetryAfterHeader } from '../utils/ratelimit';
7+
import { safeUnref } from '../utils/timer';
78

89
export const MIN_DELAY = 100; // 100 ms
910
export const START_DELAY = 5_000; // 5 seconds
@@ -97,26 +98,24 @@ export function makeOfflineTransport<TO>(
9798
clearTimeout(flushTimer as ReturnType<typeof setTimeout>);
9899
}
99100

100-
flushTimer = setTimeout(async () => {
101-
flushTimer = undefined;
102-
103-
const found = await store.shift();
104-
if (found) {
105-
log('Attempting to send previously queued event');
101+
// We need to unref the timer in node.js, otherwise the node process never exit.
102+
flushTimer = safeUnref(
103+
setTimeout(async () => {
104+
flushTimer = undefined;
106105

107-
// We should to update the sent_at timestamp to the current time.
108-
found[0].sent_at = new Date().toISOString();
106+
const found = await store.shift();
107+
if (found) {
108+
log('Attempting to send previously queued event');
109109

110-
void send(found, true).catch(e => {
111-
log('Failed to retry sending', e);
112-
});
113-
}
114-
}, delay) as Timer;
110+
// We should to update the sent_at timestamp to the current time.
111+
found[0].sent_at = new Date().toISOString();
115112

116-
// We need to unref the timer in node.js, otherwise the node process never exit.
117-
if (typeof flushTimer !== 'number' && flushTimer.unref) {
118-
flushTimer.unref();
119-
}
113+
void send(found, true).catch(e => {
114+
log('Failed to retry sending', e);
115+
});
116+
}
117+
}, delay),
118+
) as Timer;
120119
}
121120

122121
function flushWithBackOff(): void {

0 commit comments

Comments
 (0)