Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BeaconController #86

Merged
merged 5 commits into from
Mar 4, 2024
Merged

Add BeaconController #86

merged 5 commits into from
Mar 4, 2024

Conversation

jinyus
Copy link
Owner

@jinyus jinyus commented Mar 4, 2024

Description

BeaconController

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.

NB: All beacons must be created with as a late variable and with the local BeaconCreator B instead of Beacon.

class CountController extends BeaconController {
  late final count = B.writable(0);
  late final doubledCount = B.derived(() => count.value * 2);
}

This can be used with the lite_ref or Provider package to provide the controller to widgets. lite_ref will dispose the controller when all widgets that use it are disposed.

In the example below, the controller will be disposed when the CounterText is unmounted:

final countControllerRef = Ref.scoped((ctx) => CountController());

class CounterText extends StatelessWidget {
  const CounterText({super.key});

  @override
  Widget build(BuildContext context) {
    final controller = countControllerRef.of(context);
    final count = controller.count.watch(context);
    return Text('$count');
  }
}

See the full example here.

BeaconControllerMixin

A mixin for StatefulWidget's State class that automatically disposes all beacons and effects created within it.

class MyController extends StatefulWidget {
  const MyController({super.key});

  @override
  State<MyController> createState() => _MyControllerState();
}

class _MyControllerState extends State<MyController>
    with BeaconControllerMixin {
  // NO need to dispose these manually
  late final count = B.writable(0);
  late final doubledCount = B.derived(() => count.value * 2);

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

@jinyus jinyus changed the title A BeaconController Add BeaconController Mar 4, 2024
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (e5fc7bd) to head (6fad5fe).
Report is 64 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #86   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           35        36    +1     
  Lines         1016      1043   +27     
=========================================
+ Hits          1016      1043   +27     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jinyus jinyus merged commit 34f681a into main Mar 4, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants