Skip to content

Commit 24483f3

Browse files
committed
domain: consolidate run/bound/intercepted into runInDomain
Extract common domain context setup/teardown logic into a single runInDomain helper function, reducing code duplication.
1 parent bd87fa7 commit 24483f3

File tree

1 file changed

+16
-60
lines changed

1 file changed

+16
-60
lines changed

lib/domain.js

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -475,26 +475,25 @@ Domain.prototype.remove = function(ee) {
475475
};
476476

477477

478-
Domain.prototype.run = function(fn) {
478+
// Helper to run a function within a domain context.
479+
// Uses enterWith instead of ALS.run() so the context is NOT automatically
480+
// restored on exception - this matches original domain behavior where
481+
// exit() only runs on success.
482+
function runInDomain(domain, thisArg, fn, args) {
479483
const currentStore = getDomainStore() || { domain: null, stack: [] };
480484
const newStack = ArrayPrototypeSlice(currentStore.stack);
481-
ArrayPrototypePush(newStack, this);
482-
483-
const args = ArrayPrototypeSlice(arguments, 1);
485+
ArrayPrototypePush(newStack, domain);
484486
const previousDomain = currentDomain;
485487
const previousStack = currentStack;
486488

487-
// Set currentDomain and currentStack before running so exception handler can see them
488-
currentDomain = this;
489+
currentDomain = domain;
489490
currentStack = newStack;
490491
updateExceptionCapture();
491492

492-
// Use enterWith instead of run() so the context is NOT automatically restored on exception.
493-
// This matches the original domain.run() behavior where exit() only runs on success.
494-
setDomainStore({ domain: this, stack: newStack });
495-
exports.active = this;
493+
setDomainStore({ domain, stack: newStack });
494+
exports.active = domain;
496495

497-
const result = ReflectApply(fn, this, args);
496+
const result = ReflectApply(fn, thisArg, args);
498497

499498
// On success, restore context (if exception thrown, context stays for catch blocks)
500499
setDomainStore(currentStore);
@@ -504,6 +503,10 @@ Domain.prototype.run = function(fn) {
504503
updateExceptionCapture();
505504

506505
return result;
506+
}
507+
508+
Domain.prototype.run = function(fn) {
509+
return runInDomain(this, this, fn, ArrayPrototypeSlice(arguments, 1));
507510
};
508511

509512

@@ -523,31 +526,7 @@ function intercepted(_this, self, cb, fnargs) {
523526
return;
524527
}
525528

526-
const currentStore = getDomainStore() || { domain: null, stack: [] };
527-
const newStack = ArrayPrototypeSlice(currentStore.stack);
528-
ArrayPrototypePush(newStack, self);
529-
const args = ArrayPrototypeSlice(fnargs, 1);
530-
const previousDomain = currentDomain;
531-
const previousStack = currentStack;
532-
533-
currentDomain = self;
534-
currentStack = newStack;
535-
updateExceptionCapture();
536-
537-
// Use enterWith instead of run() so the context is NOT automatically restored on exception.
538-
setDomainStore({ domain: self, stack: newStack });
539-
exports.active = self;
540-
541-
const result = ReflectApply(cb, _this, args);
542-
543-
// On success, restore context
544-
setDomainStore(currentStore);
545-
exports.active = currentStore.domain;
546-
currentDomain = previousDomain;
547-
currentStack = previousStack;
548-
updateExceptionCapture();
549-
550-
return result;
529+
return runInDomain(self, _this, cb, ArrayPrototypeSlice(fnargs, 1));
551530
}
552531

553532

@@ -563,30 +542,7 @@ Domain.prototype.intercept = function(cb) {
563542

564543

565544
function bound(_this, self, cb, fnargs) {
566-
const currentStore = getDomainStore() || { domain: null, stack: [] };
567-
const newStack = ArrayPrototypeSlice(currentStore.stack);
568-
ArrayPrototypePush(newStack, self);
569-
const previousDomain = currentDomain;
570-
const previousStack = currentStack;
571-
572-
currentDomain = self;
573-
currentStack = newStack;
574-
updateExceptionCapture();
575-
576-
// Use enterWith instead of run() so the context is NOT automatically restored on exception.
577-
setDomainStore({ domain: self, stack: newStack });
578-
exports.active = self;
579-
580-
const result = ReflectApply(cb, _this, fnargs);
581-
582-
// On success, restore context
583-
setDomainStore(currentStore);
584-
exports.active = currentStore.domain;
585-
currentDomain = previousDomain;
586-
currentStack = previousStack;
587-
updateExceptionCapture();
588-
589-
return result;
545+
return runInDomain(self, _this, cb, fnargs);
590546
}
591547

592548

0 commit comments

Comments
 (0)