@@ -88,8 +88,8 @@ class RunStoresScope { |
88 | 88 | #stack; |
89 | 89 |
|
90 | 90 | constructor(activeChannel, data) { |
91 | | - // eslint-disable-next-line no-restricted-globals |
92 | | - using stack = new DisposableStack(); |
| 91 | + // TODO: use native DisposableStack once supported in core |
| 92 | + const stack = []; |
93 | 93 |
|
94 | 94 | // Enter stores using withScope |
95 | 95 | if (activeChannel._stores) { |
@@ -109,19 +109,27 @@ class RunStoresScope { |
109 | 109 | } |
110 | 110 | } |
111 | 111 |
|
112 | | - stack.use(store.withScope(newContext)); |
| 112 | + ArrayPrototypePush(stack, store.withScope(newContext)); |
113 | 113 | } |
114 | 114 | } |
115 | 115 |
|
116 | 116 | // Publish data |
117 | 117 | activeChannel.publish(data); |
118 | 118 |
|
119 | 119 | // Transfer ownership of the stack |
120 | | - this.#stack = stack.move(); |
| 120 | + // TODO: Restore stack.move() when DisposableStack is restored in core to avoid broken behavior. |
| 121 | + this.#stack = stack; |
121 | 122 | } |
122 | 123 |
|
123 | 124 | [SymbolDispose]() { |
124 | | - this.#stack[SymbolDispose](); |
| 125 | + if (this.#stack !== undefined) { |
| 126 | + const stack = this.#stack; |
| 127 | + this.#stack = undefined; |
| 128 | + |
| 129 | + for (let i = stack.length - 1; i >= 0; i--) { |
| 130 | + stack[i][SymbolDispose](); |
| 131 | + } |
| 132 | + } |
125 | 133 | } |
126 | 134 | } |
127 | 135 |
|
@@ -197,9 +205,12 @@ class ActiveChannel { |
197 | 205 | } |
198 | 206 |
|
199 | 207 | runStores(data, fn, thisArg, ...args) { |
200 | | - // eslint-disable-next-line no-unused-vars |
201 | | - using scope = this.withStoreScope(data); |
202 | | - return ReflectApply(fn, thisArg, args); |
| 208 | + const scope = this.withStoreScope(data); |
| 209 | + try { |
| 210 | + return ReflectApply(fn, thisArg, args); |
| 211 | + } finally { |
| 212 | + scope[SymbolDispose](); |
| 213 | + } |
203 | 214 | } |
204 | 215 | } |
205 | 216 |
|
@@ -396,9 +407,12 @@ class BoundedChannel { |
396 | 407 |
|
397 | 408 | run(context, fn, thisArg, ...args) { |
398 | 409 | context ??= {}; |
399 | | - // eslint-disable-next-line no-unused-vars |
400 | | - using scope = this.withScope(context); |
401 | | - return ReflectApply(fn, thisArg, args); |
| 410 | + const scope = this.withScope(context); |
| 411 | + try { |
| 412 | + return ReflectApply(fn, thisArg, args); |
| 413 | + } finally { |
| 414 | + scope[SymbolDispose](); |
| 415 | + } |
402 | 416 | } |
403 | 417 | } |
404 | 418 |
|
@@ -520,9 +534,8 @@ class TracingChannel { |
520 | 534 | } |
521 | 535 |
|
522 | 536 | const { error } = this; |
| 537 | + const scope = this.#callWindow.withScope(context); |
523 | 538 |
|
524 | | - // eslint-disable-next-line no-unused-vars |
525 | | - using scope = this.#callWindow.withScope(context); |
526 | 539 | try { |
527 | 540 | const result = ReflectApply(fn, thisArg, args); |
528 | 541 | context.result = result; |
@@ -531,6 +544,8 @@ class TracingChannel { |
531 | 544 | context.error = err; |
532 | 545 | error.publish(context); |
533 | 546 | throw err; |
| 547 | + } finally { |
| 548 | + scope[SymbolDispose](); |
534 | 549 | } |
535 | 550 | } |
536 | 551 |
|
@@ -550,9 +565,9 @@ class TracingChannel { |
550 | 565 | context.error = err; |
551 | 566 | error.publish(context); |
552 | 567 | // Use continuation window for asyncStart/asyncEnd |
553 | | - // eslint-disable-next-line no-unused-vars |
554 | | - using scope = continuationWindow.withScope(context); |
| 568 | + const scope = continuationWindow.withScope(context); |
555 | 569 | // TODO: Is there a way to have asyncEnd _after_ the continuation? |
| 570 | + scope[SymbolDispose](); |
556 | 571 | } |
557 | 572 |
|
558 | 573 | function onRejectWithRethrow(err) { |
@@ -563,14 +578,14 @@ class TracingChannel { |
563 | 578 | function onResolve(result) { |
564 | 579 | context.result = result; |
565 | 580 | // Use continuation window for asyncStart/asyncEnd |
566 | | - // eslint-disable-next-line no-unused-vars |
567 | | - using scope = continuationWindow.withScope(context); |
| 581 | + const scope = continuationWindow.withScope(context); |
568 | 582 | // TODO: Is there a way to have asyncEnd _after_ the continuation? |
| 583 | + scope[SymbolDispose](); |
569 | 584 | return result; |
570 | 585 | } |
571 | 586 |
|
572 | | - // eslint-disable-next-line no-unused-vars |
573 | | - using scope = this.#callWindow.withScope(context); |
| 587 | + const scope = this.#callWindow.withScope(context); |
| 588 | + |
574 | 589 | try { |
575 | 590 | const result = ReflectApply(fn, thisArg, args); |
576 | 591 | // If the return value is not a thenable, return it directly with a warning. |
@@ -595,6 +610,8 @@ class TracingChannel { |
595 | 610 | context.error = err; |
596 | 611 | error.publish(context); |
597 | 612 | throw err; |
| 613 | + } finally { |
| 614 | + scope[SymbolDispose](); |
598 | 615 | } |
599 | 616 | } |
600 | 617 |
|
@@ -615,23 +632,28 @@ class TracingChannel { |
615 | 632 | } |
616 | 633 |
|
617 | 634 | // Use continuation window for asyncStart/asyncEnd around callback |
618 | | - // eslint-disable-next-line no-unused-vars |
619 | | - using scope = continuationWindow.withScope(context); |
620 | | - return ReflectApply(callback, this, arguments); |
| 635 | + const scope = continuationWindow.withScope(context); |
| 636 | + try { |
| 637 | + return ReflectApply(callback, this, arguments); |
| 638 | + } finally { |
| 639 | + scope[SymbolDispose](); |
| 640 | + } |
621 | 641 | } |
622 | 642 |
|
623 | 643 | const callback = ArrayPrototypeAt(args, position); |
624 | 644 | validateFunction(callback, 'callback'); |
625 | 645 | ArrayPrototypeSplice(args, position, 1, wrappedCallback); |
626 | 646 |
|
627 | | - // eslint-disable-next-line no-unused-vars |
628 | | - using scope = this.#callWindow.withScope(context); |
| 647 | + const scope = this.#callWindow.withScope(context); |
| 648 | + |
629 | 649 | try { |
630 | 650 | return ReflectApply(fn, thisArg, args); |
631 | 651 | } catch (err) { |
632 | 652 | context.error = err; |
633 | 653 | error.publish(context); |
634 | 654 | throw err; |
| 655 | + } finally { |
| 656 | + scope[SymbolDispose](); |
635 | 657 | } |
636 | 658 | } |
637 | 659 | } |
|