|
| 1 | +import 'package:solidart/solidart.dart'; |
| 2 | +import 'package:test/test.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + group('effect re-entrancy during initial run', () { |
| 6 | + test( |
| 7 | + '''a signal write during the effect first run does not re-enter the effect callback while it is still on the stack''', |
| 8 | + () { |
| 9 | + final previousAutoDispose = SolidartConfig.autoDispose; |
| 10 | + addTearDown(() => SolidartConfig.autoDispose = previousAutoDispose); |
| 11 | + SolidartConfig.autoDispose = false; |
| 12 | + final source = Signal(0, name: 'source'); |
| 13 | + var running = false; |
| 14 | + var maxConcurrentDepth = 0; |
| 15 | + var runs = 0; |
| 16 | + |
| 17 | + final effect = Effect(() { |
| 18 | + runs++; |
| 19 | + // Detect re-entrancy: if the callback is invoked while a previous |
| 20 | + // invocation is still on the stack, `running` is already true. |
| 21 | + expect(running, isFalse, reason: 'effect re-entered its own run'); |
| 22 | + running = true; |
| 23 | + maxConcurrentDepth++; |
| 24 | + |
| 25 | + // Subscribe to `source`, then write it during the FIRST run. With |
| 26 | + // synchronous flush this re-enters the callback before `running` |
| 27 | + // is reset → the expect above fails. |
| 28 | + final value = source.value; |
| 29 | + if (runs == 1) { |
| 30 | + source.value = value + 1; |
| 31 | + } |
| 32 | + |
| 33 | + maxConcurrentDepth--; |
| 34 | + running = false; |
| 35 | + }); |
| 36 | + |
| 37 | + addTearDown(effect.dispose); |
| 38 | + expect(maxConcurrentDepth, 0); |
| 39 | + // The effect re-runs once (sequentially) for the value change. |
| 40 | + expect(runs, 2); |
| 41 | + expect(source.value, 1); |
| 42 | + }, |
| 43 | + ); |
| 44 | + |
| 45 | + test( |
| 46 | + '''a late-final read inside the effect survives a write triggered mid-construction''', |
| 47 | + () { |
| 48 | + final previousAutoDispose = SolidartConfig.autoDispose; |
| 49 | + addTearDown(() => SolidartConfig.autoDispose = previousAutoDispose); |
| 50 | + SolidartConfig.autoDispose = false; |
| 51 | + // `dep` mimics a controller built lazily during the effect run whose |
| 52 | + // constructor writes to a collection signal. |
| 53 | + final trigger = Signal(0, name: 'trigger'); |
| 54 | + late final int lazy; |
| 55 | + var lazyInitialized = false; |
| 56 | + |
| 57 | + final effect = Effect(() { |
| 58 | + // Read `trigger` (subscribe). On first run, lazily initialize a |
| 59 | + // value whose initializer writes `trigger` — a synchronous flush |
| 60 | + // would re-enter HERE before `lazy` finishes initializing. |
| 61 | + final t = trigger.value; |
| 62 | + if (!lazyInitialized) { |
| 63 | + lazyInitialized = true; |
| 64 | + // Initializer body writes the signal the effect depends on. |
| 65 | + trigger.value = t + 1; |
| 66 | + lazy = 42; |
| 67 | + } |
| 68 | + // Reading `lazy` must never throw LateInitializationError. |
| 69 | + expect(lazy, 42); |
| 70 | + }); |
| 71 | + |
| 72 | + addTearDown(effect.dispose); |
| 73 | + expect(lazy, 42); |
| 74 | + }, |
| 75 | + ); |
| 76 | + }); |
| 77 | +} |
0 commit comments