Core

CorePondContext

The CorePondContext is the foundational component of the Pond Module in the Flood toolkit. It serves as a container and manager for CorePondComponents, which are the building blocks of your application's functionality.

Lifecycle

A CorePondContext follows a specific lifecycle:

  1. The context is created.
  2. CorePondComponents are registered using the register method.
  3. Once all components are registered, the load method is called, triggering the onLoad behavior of each component in the order they were registered.
ℹ️

When using a CorePondContext in the context of a Flutter App or Automation, it will automatically handle loading your CorePondContext. There is no need to manually call this yourself.

While resetting is typically handled by the Reset Debug Module, you can manually reset the CorePondContext by calling the reset method:

corePondContext.reset()

This will invoke the onReset behavior of each registered component, effectively returning the application to its initial state as if it was never installed.

CorePondComponent

A CorePondComponent represents a specific piece of functionality within your application. While CorePondComponents do not add any functionality by themselves, they provide the mechanism for you to add features to your app in a modular and composable way.

CorePondComponentBehavior

Each CorePondComponent contains CorePondComponentBehaviors that define the component's lifecycle:

  • onRegister: Called when the component is registered with the CorePondContext. This is where you should perform any initialization that other CorePondComponents might depend on during their registration.
  • onLoad: Called after all CorePondComponents have been registered and the CorePondContext is loaded. At this point, you can safely locate and interact with other components.
  • onReset: Called when the CorePondContext is reset. Resetting should return the application to its initial state, as if it had never been run before.

Here's an example of a CorePondComponent that simply stores some state you can use across your app.

example_core/lib/features/hello_world.dart
class HelloWorldPondComponent extends CorePondComponent {
  String? name;
 
  @override
  List<CorePondComponentBehavior> get behaviors => [
        CorePondComponentBehavior(
          onLoad: (context, component) => name = 'John Doe',
        )
      ];
 
  String greet() {
    return 'Hello ${name ?? 'N/A'}';
  }
 
}

Here's how you would register it to your CorePondContext.

example_core/lib/pond.dart
final corePondContext = CorePondContext();
await corePondContext.register(HelloWorldPondComponent());

Locating Components

You can locate any CorePondComponent registered within a CorePondContext using the locate method:

corePondContext.locate<HelloWorldPondComponent>() // returns the instance of `HelloWorldPondComponent` registered in the context.

If there's a possibility that the component hasn't been registered, you can use the locateOrNull method instead:

corePondContext.locateOrNull<MyOtherComponent>()

This will return null if no component of the specified type is found.

Pre-Built Components

Flood provides a FloodCoreComponent that includes a set of essential components you can use to quickly set up your application.

Here are the components included:

To use the FloodCoreComponent within your application, simply register it in your CorePondContext.

example_core/lib/pond.dart
final corePondContext = CorePondContext();
await corePondContext.register(FloodCoreComponent(...));

If you want to exclude some of these components from your context, simply do not register FloodCoreComponent and register the components you want to include.