Skip to content

Commit f4e92ca

Browse files
authored
fix(node): reuseExisting does not need to call bind on domain (#7780)
1 parent 715876b commit f4e92ca

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

packages/node/src/async/domain.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,22 @@ function getCurrentHub(): Hub | undefined {
2727
}
2828

2929
function runWithAsyncContext<T>(callback: (hub: Hub) => T, options: RunWithAsyncContextOptions): T {
30-
const local = options?.reuseExisting ? getActiveDomain<domain.Domain>() || domain.create() : domain.create();
30+
if (options?.reuseExisting) {
31+
const activeDomain = getActiveDomain<domain.Domain & Carrier>();
32+
33+
if (activeDomain) {
34+
for (const emitter of options.args || []) {
35+
if (emitter instanceof EventEmitter) {
36+
activeDomain.add(emitter);
37+
}
38+
}
39+
40+
// We're already in a domain, so we don't need to create a new one, just call the callback with the current hub
41+
return callback(getHubFromCarrier(activeDomain));
42+
}
43+
}
44+
45+
const local = domain.create();
3146

3247
for (const emitter of options.args || []) {
3348
if (emitter instanceof EventEmitter) {

0 commit comments

Comments
 (0)