File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -648,7 +648,12 @@ Effect.prototype._dispose = function () {
648
648
649
649
function effect ( compute : ( ) => unknown ) : ( ) => void {
650
650
const effect = new Effect ( compute ) ;
651
- effect . _callback ( ) ;
651
+ try {
652
+ effect . _callback ( ) ;
653
+ } catch ( err ) {
654
+ effect . _dispose ( ) ;
655
+ throw err ;
656
+ }
652
657
// Return a bound function instead of a wrapper like `() => effect._dispose()`,
653
658
// because bound functions seem to be just as fast and take up a lot less memory.
654
659
return effect . _dispose . bind ( effect ) ;
Original file line number Diff line number Diff line change @@ -461,6 +461,19 @@ describe("effect()", () => {
461
461
expect ( spy ) . to . be . calledOnce ;
462
462
} ) ;
463
463
464
+ it ( "should not subscribe to anything if first run throws" , ( ) => {
465
+ const s = signal ( 0 ) ;
466
+ const spy = sinon . spy ( ( ) => {
467
+ s . value ;
468
+ throw new Error ( "test" ) ;
469
+ } ) ;
470
+ expect ( ( ) => effect ( spy ) ) . to . throw ( "test" ) ;
471
+ expect ( spy ) . to . be . calledOnce ;
472
+
473
+ s . value ++ ;
474
+ expect ( spy ) . to . be . calledOnce ;
475
+ } ) ;
476
+
464
477
it ( "should reset the cleanup if the effect throws" , ( ) => {
465
478
const a = signal ( 0 ) ;
466
479
const spy = sinon . spy ( ) ;
You can’t perform that action at this time.
0 commit comments