@@ -20,6 +20,7 @@ const { |
20 | 20 | } = primordials; |
21 | 21 |
|
22 | 22 | const { |
| 23 | + aggregateTwoErrors, |
23 | 24 | codes: { |
24 | 25 | ERR_INVALID_ARG_TYPE, |
25 | 26 | }, |
@@ -93,12 +94,25 @@ function maybeMarkInactive(channel) { |
93 | 94 | } |
94 | 95 | } |
95 | 96 |
|
| 97 | +function disposeStoreScopes(stack, error, hasError = false) { |
| 98 | + for (let i = stack.length - 1; i >= 0; i--) { |
| 99 | + try { |
| 100 | + stack[i][SymbolDispose](); |
| 101 | + } catch (err) { |
| 102 | + error = hasError ? aggregateTwoErrors(err, error) : err; |
| 103 | + hasError = true; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (hasError) throw error; |
| 108 | +} |
| 109 | + |
96 | 110 | class RunStoresScope { |
97 | 111 | #stack; |
98 | 112 |
|
99 | 113 | constructor(activeChannel, data) { |
100 | | - // eslint-disable-next-line no-restricted-globals |
101 | | - using stack = new DisposableStack(); |
| 114 | + // TODO: use native DisposableStack once supported in core |
| 115 | + const stack = []; |
102 | 116 |
|
103 | 117 | // Enter stores using withScope |
104 | 118 | if (activeChannel._stores) { |
@@ -118,19 +132,29 @@ class RunStoresScope { |
118 | 132 | } |
119 | 133 | } |
120 | 134 |
|
121 | | - stack.use(store.withScope(newContext)); |
| 135 | + try { |
| 136 | + ArrayPrototypePush(stack, store.withScope(newContext)); |
| 137 | + } catch (err) { |
| 138 | + disposeStoreScopes(stack, err, true); |
| 139 | + } |
122 | 140 | } |
123 | 141 | } |
124 | 142 |
|
125 | 143 | // Publish data |
126 | 144 | activeChannel.publish(data); |
127 | 145 |
|
128 | 146 | // Transfer ownership of the stack |
129 | | - this.#stack = stack.move(); |
| 147 | + // TODO: Restore stack.move() when DisposableStack is restored in core to avoid broken behavior. |
| 148 | + this.#stack = stack; |
130 | 149 | } |
131 | 150 |
|
132 | 151 | [SymbolDispose]() { |
133 | | - this.#stack[SymbolDispose](); |
| 152 | + if (this.#stack !== undefined) { |
| 153 | + const stack = this.#stack; |
| 154 | + this.#stack = undefined; |
| 155 | + |
| 156 | + disposeStoreScopes(stack); |
| 157 | + } |
134 | 158 | } |
135 | 159 | } |
136 | 160 |
|
@@ -206,9 +230,12 @@ class ActiveChannel { |
206 | 230 | } |
207 | 231 |
|
208 | 232 | runStores(data, fn, thisArg, ...args) { |
209 | | - // eslint-disable-next-line no-unused-vars |
210 | | - using scope = this.withStoreScope(data); |
211 | | - return ReflectApply(fn, thisArg, args); |
| 233 | + const scope = this.withStoreScope(data); |
| 234 | + try { |
| 235 | + return ReflectApply(fn, thisArg, args); |
| 236 | + } finally { |
| 237 | + scope[SymbolDispose](); |
| 238 | + } |
212 | 239 | } |
213 | 240 | } |
214 | 241 |
|
@@ -404,9 +431,12 @@ class BoundedChannel { |
404 | 431 |
|
405 | 432 | run(context, fn, thisArg, ...args) { |
406 | 433 | context ??= {}; |
407 | | - // eslint-disable-next-line no-unused-vars |
408 | | - using scope = this.withScope(context); |
409 | | - return ReflectApply(fn, thisArg, args); |
| 434 | + const scope = this.withScope(context); |
| 435 | + try { |
| 436 | + return ReflectApply(fn, thisArg, args); |
| 437 | + } finally { |
| 438 | + scope[SymbolDispose](); |
| 439 | + } |
410 | 440 | } |
411 | 441 | } |
412 | 442 |
|
@@ -532,9 +562,8 @@ class TracingChannel { |
532 | 562 | } |
533 | 563 |
|
534 | 564 | const { error } = this; |
| 565 | + const scope = this.#callWindow.withScope(context); |
535 | 566 |
|
536 | | - // eslint-disable-next-line no-unused-vars |
537 | | - using scope = this.#callWindow.withScope(context); |
538 | 567 | try { |
539 | 568 | const result = ReflectApply(fn, thisArg, args); |
540 | 569 | context.result = result; |
@@ -543,6 +572,8 @@ class TracingChannel { |
543 | 572 | context.error = err; |
544 | 573 | error.publish(context); |
545 | 574 | throw err; |
| 575 | + } finally { |
| 576 | + scope[SymbolDispose](); |
546 | 577 | } |
547 | 578 | } |
548 | 579 |
|
@@ -566,9 +597,9 @@ class TracingChannel { |
566 | 597 | context.error = err; |
567 | 598 | error.publish(context); |
568 | 599 | // Use continuation window for asyncStart/asyncEnd |
569 | | - // eslint-disable-next-line no-unused-vars |
570 | | - using scope = continuationWindow.withScope(context); |
| 600 | + const scope = continuationWindow.withScope(context); |
571 | 601 | // TODO: Is there a way to have asyncEnd _after_ the continuation? |
| 602 | + scope[SymbolDispose](); |
572 | 603 | } |
573 | 604 |
|
574 | 605 | function onRejectWithRethrow(err) { |
@@ -579,14 +610,14 @@ class TracingChannel { |
579 | 610 | function onResolve(result) { |
580 | 611 | context.result = result; |
581 | 612 | // Use continuation window for asyncStart/asyncEnd |
582 | | - // eslint-disable-next-line no-unused-vars |
583 | | - using scope = continuationWindow.withScope(context); |
| 613 | + const scope = continuationWindow.withScope(context); |
584 | 614 | // TODO: Is there a way to have asyncEnd _after_ the continuation? |
| 615 | + scope[SymbolDispose](); |
585 | 616 | return result; |
586 | 617 | } |
587 | 618 |
|
588 | | - // eslint-disable-next-line no-unused-vars |
589 | | - using scope = this.#callWindow.withScope(context); |
| 619 | + const scope = this.#callWindow.withScope(context); |
| 620 | + |
590 | 621 | try { |
591 | 622 | const result = ReflectApply(fn, thisArg, args); |
592 | 623 | // If the return value is not a thenable, return it directly with a warning. |
@@ -611,6 +642,8 @@ class TracingChannel { |
611 | 642 | context.error = err; |
612 | 643 | error.publish(context); |
613 | 644 | throw err; |
| 645 | + } finally { |
| 646 | + scope[SymbolDispose](); |
614 | 647 | } |
615 | 648 | } |
616 | 649 |
|
@@ -631,23 +664,28 @@ class TracingChannel { |
631 | 664 | } |
632 | 665 |
|
633 | 666 | // Use continuation window for asyncStart/asyncEnd around callback |
634 | | - // eslint-disable-next-line no-unused-vars |
635 | | - using scope = continuationWindow.withScope(context); |
636 | | - return ReflectApply(callback, this, arguments); |
| 667 | + const scope = continuationWindow.withScope(context); |
| 668 | + try { |
| 669 | + return ReflectApply(callback, this, arguments); |
| 670 | + } finally { |
| 671 | + scope[SymbolDispose](); |
| 672 | + } |
637 | 673 | } |
638 | 674 |
|
639 | 675 | const callback = ArrayPrototypeAt(args, position); |
640 | 676 | validateFunction(callback, 'callback'); |
641 | 677 | ArrayPrototypeSplice(args, position, 1, wrappedCallback); |
642 | 678 |
|
643 | | - // eslint-disable-next-line no-unused-vars |
644 | | - using scope = this.#callWindow.withScope(context); |
| 679 | + const scope = this.#callWindow.withScope(context); |
| 680 | + |
645 | 681 | try { |
646 | 682 | return ReflectApply(fn, thisArg, args); |
647 | 683 | } catch (err) { |
648 | 684 | context.error = err; |
649 | 685 | error.publish(context); |
650 | 686 | throw err; |
| 687 | + } finally { |
| 688 | + scope[SymbolDispose](); |
651 | 689 | } |
652 | 690 | } |
653 | 691 | } |
|