Closed jinyus closed 7 months ago
Test used to validate behaviour:
@lukepighetti let me know if there's something that this test doesn't cover.
test('should unsub from old stream when dependency changes', () async {
final controller = StreamController<int>.broadcast();
final counter = Beacon.writable(5);
// should increment when dependency changes
var unsubs = 0;
var listens = 0;
controller.onCancel = () => unsubs++;
controller.onListen = () {
listens++;
addItems(controller, counter.value);
};
final beacon = Beacon.derivedStream(() {
counter.value;
return controller.stream;
});
expect(listens, 1);
counter.increment(); // dep changed, should unsub from old stream
expect(unsubs, 1);
expect(listens, 2);
counter.increment();
expect(unsubs, 2); // dep changed, should unsub from old stream
expect(listens, 3);
beacon.dispose(); // should unsub when disposed
expect(unsubs, 3);
expect(listens, 3);
});
All modified and coverable lines are covered by tests :white_check_mark:
Comparison is base (
bde8f95
) 100.00% compared to head (d5ad1b8
) 100.00%. Report is 88 commits behind head on main.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
LGTM!
Description
Specialized
DerivedBeacon
that subscribes to the stream returned from its callback and updates its value based on the emitted values. When a dependency changes, the beacon will unsubscribe from the old stream and subscribe to the new one.Example: