CorePondContext
The CorePondContext
is the foundational component of the Pond Module in the Flood toolkit. It serves as a container and manager for CorePondComponent
s, which are the building blocks of your application's functionality.
Lifecycle
A CorePondContext
follows a specific lifecycle:
- The context is created.
CorePondComponent
s are registered using theregister
method.- Once all components are registered, the
load
method is called, triggering theonLoad
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 CorePondComponent
s 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 CorePondComponentBehavior
s that define the component's lifecycle:
onRegister
: Called when the component is registered with theCorePondContext
. This is where you should perform any initialization that otherCorePondComponent
s might depend on during their registration.onLoad
: Called after allCorePondComponent
s have been registered and theCorePondContext
is loaded. At this point, you can safely locate and interact with other components.onReset
: Called when theCorePondContext
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.
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
.
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:
- Type
- Environment
- Logs
- Drop
- Auth
- Action
- And other minor components that don't currently include documentation.
To use the FloodCoreComponent
within your application, simply register it in your CorePondContext
.
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.