Skip to content

Commit

Permalink
Merge pull request #94 from jinyus:feat/family-entries
Browse files Browse the repository at this point in the history
add entries to family beacon as a replacement for beacons getter.
  • Loading branch information
jinyus authored Mar 15, 2024
2 parents eb693cf + f74e164 commit 5396304
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ count.value = 40;

An abstract mixin class that automatically disposes all beacons and effects created within it. This can be used to create a controller that manages a group of beacons. use the included [BeaconGroup](#beacongroup)(`B.writable()`) instead of `Beacon.writable()` to create beacons and effects.

NB: All beacons must be created with as a `late` variable.
NB: All beacons must be created as a `late` variable.

```dart
class CountController extends BeaconController {
Expand Down
24 changes: 13 additions & 11 deletions packages/state_beacon_core/lib/src/beacons/family.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ class BeaconFamily<Arg, BeaconType extends ReadableBeacon<dynamic>> {

late final _cache = <Arg, BeaconType>{};

late final _beacons = Beacon.list<BeaconType>(
late final _entries = Beacon.list<MapEntry<Arg, BeaconType>>(
[],
name: 'family beacons list',
name: 'family entries',
);

/// All beacons in the cache
ReadableBeacon<List<BeaconType>> get beacons {
ReadableBeacon<List<MapEntry<Arg, BeaconType>>> get entries {
if (!_beaconsAccessed && _cache.isNotEmpty) {
// populate the list on first access
_beacons.value = _cache.values.toList();
_entries.value = _cache.entries.toList();
}
_beaconsAccessed = true;
return _beacons;
return _entries;
}

final BeaconType Function(Arg) _create;
Expand All @@ -47,13 +47,13 @@ class BeaconFamily<Arg, BeaconType extends ReadableBeacon<dynamic>> {

beacon = _create(arg);
_cache[arg] = beacon;
if (_beaconsAccessed) _beacons.add(beacon);
if (_beaconsAccessed) _entries.add(MapEntry(arg, beacon));

beacon.onDispose(() {
if (_clearing) return;
final removed = _cache.remove(arg);
if (!_beaconsAccessed || removed == null) return;
_beacons.remove(removed);
_entries.removeWhere((e) => e.value == removed);
});

return beacon;
Expand All @@ -64,9 +64,11 @@ class BeaconFamily<Arg, BeaconType extends ReadableBeacon<dynamic>> {

/// Removes a beacon from the cache if it exists.
BeaconType? remove(Arg arg) {
final beacon = _cache.remove(arg);
if (beacon != null && _beaconsAccessed) _beacons.remove(beacon);
return beacon;
final removed = _cache.remove(arg);
if (removed != null && _beaconsAccessed) {
_entries.removeWhere((e) => e.value == removed);
}
return removed;
}

/// Clears the cache of beacon if caching is enabled.
Expand All @@ -83,7 +85,7 @@ class BeaconFamily<Arg, BeaconType extends ReadableBeacon<dynamic>> {
_clearing = false;

_cache.clear();
if (_beaconsAccessed) _beacons.clear();
if (_beaconsAccessed) _entries.clear();
_beaconsAccessed = false;
}
}
19 changes: 11 additions & 8 deletions packages/state_beacon_core/test/src/beacons/family_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void main() {

var ran = 0;

family.beacons.subscribe((_) => ran++, synchronous: true, startNow: false);
family.entries.subscribe((_) => ran++, synchronous: true, startNow: false);

final beacon1 = family(1);
final beacon2 = family(2);
Expand All @@ -143,13 +143,16 @@ void main() {
final beacon1 = family(1);
final beacon2 = family(2);

expect(family.beacons.value, [beacon1, beacon2]);
List<WritableBeacon<String>> getBeacons() =>
family.entries.value.map((e) => e.value).toList();

family.beacons.subscribe((_) => ran++, synchronous: true, startNow: false);
expect(getBeacons(), [beacon1, beacon2]);

family.entries.subscribe((_) => ran++, synchronous: true, startNow: false);

final beacon3 = family(3);

expect(family.beacons.value, [beacon1, beacon2, beacon3]);
expect(getBeacons(), [beacon1, beacon2, beacon3]);

expect(ran, 1);

Expand All @@ -159,19 +162,19 @@ void main() {

beacon1.dispose();

expect(family.beacons.value, [beacon2, beacon3]);
expect(getBeacons(), [beacon2, beacon3]);

expect(ran, 2);

family.remove(2);

expect(family.beacons.value, [beacon3]);
expect(getBeacons(), [beacon3]);

expect(ran, 3);

final beacon4 = family(4);

expect(family.beacons.value, [beacon3, beacon4]);
expect(getBeacons(), [beacon3, beacon4]);

expect(ran, 4);

Expand All @@ -180,7 +183,7 @@ void main() {

family.clear();

expect(family.beacons.value, isEmpty);
expect(getBeacons(), isEmpty);

expect(ran, 5);
});
Expand Down

0 comments on commit 5396304

Please sign in to comment.