@@ -12,6 +12,7 @@ import {
12
12
SignalFlags ,
13
13
WrappedSignalFlags ,
14
14
} from '../types' ;
15
+ import { triggerEffects } from '../utils' ;
15
16
import { SignalImpl } from './signal-impl' ;
16
17
17
18
export class WrappedSignalImpl < T > extends SignalImpl < T > implements BackRef {
@@ -41,12 +42,23 @@ export class WrappedSignalImpl<T> extends SignalImpl<T> implements BackRef {
41
42
42
43
invalidate ( ) {
43
44
this . $flags$ |= SignalFlags . INVALID ;
44
- this . $container$ ?. $scheduler$ (
45
- ChoreType . RECOMPUTE_AND_SCHEDULE_EFFECTS ,
46
- this . $hostElement$ ,
47
- this ,
48
- this . $effects$
49
- ) ;
45
+ // we are trying to run computation without creating a chore, which can be expensive
46
+ // for many signals. If it fails, we schedule a chore to run the computation.
47
+ try {
48
+ this . $computeIfNeeded$ ( ) ;
49
+ } catch ( _ ) {
50
+ this . $container$ ?. $scheduler$ (
51
+ ChoreType . RECOMPUTE_AND_SCHEDULE_EFFECTS ,
52
+ this . $hostElement$ ,
53
+ this ,
54
+ this . $effects$
55
+ ) ;
56
+ }
57
+ // if the computation not failed, we can run the effects directly
58
+ if ( this . $flags$ & SignalFlags . RUN_EFFECTS ) {
59
+ this . $flags$ &= ~ SignalFlags . RUN_EFFECTS ;
60
+ triggerEffects ( this . $container$ , this , this . $effects$ ) ;
61
+ }
50
62
}
51
63
52
64
/**
@@ -80,7 +92,14 @@ export class WrappedSignalImpl<T> extends SignalImpl<T> implements BackRef {
80
92
this . $container$ !
81
93
) ;
82
94
// TODO: we should remove invalid flag here, but some tests are failing
95
+ // Sometimes we may call .value on wrapped signals without ctx. This means subscription will be
96
+ // not created and effects will not be triggered. After wrapping this with if (this.$container$)
97
+ // less tests are failing, but still some are failing.
83
98
// this.$flags$ &= ~SignalFlags.INVALID;
99
+
100
+ // reset flag in case we call computedIfNeeded twice and the value was changed only the first time
101
+ // TODO: change to version number?
102
+ this . $flags$ &= ~ SignalFlags . RUN_EFFECTS ;
84
103
const didChange = untrackedValue !== this . $untrackedValue$ ;
85
104
if ( didChange ) {
86
105
this . $flags$ |= SignalFlags . RUN_EFFECTS ;
0 commit comments