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