jinyus / dart_beacon

A reactive primitive (signals) and state management library for dart and flutter
https://pub.dev/packages/state_beacon
28 stars 2 forks source link

[Feat] Add Beacon.derivedStream along with tests #59

Closed jinyus closed 7 months ago

jinyus commented 7 months ago

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:

final userID = Beacon.writable<int>(18235);
final profileBeacon = Beacon.derivedStream(() {
 return getProfileStreamFromUID(userID.value);
});
jinyus commented 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);
  });
codecov-commenter commented 7 months ago

Codecov Report

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.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #59 +/- ## ========================================== Coverage 100.00% 100.00% ========================================== Files 33 35 +2 Lines 850 976 +126 ========================================== + Hits 850 976 +126 ``` | [Flag](https://app.codecov.io/gh/jinyus/dart_beacon/pull/59/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jinyus) | Coverage Ξ” | | |---|---|---| | [unittests](https://app.codecov.io/gh/jinyus/dart_beacon/pull/59/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jinyus) | `100.00% <100.00%> (ΓΈ)` | | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jinyus#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

lukepighetti commented 7 months ago

LGTM!