osam 2.6.8
osam: ^2.6.8 copied to clipboard
State management library inspired redux, bloc and SOLID prenciples. It is null safety and very stable solution to avoid red screens and bad code.
OSAM #
[GitHub Logo] This state management implements best solutions of Redux, Bloc, Provider and SOLID principles. #
Glossary #
- [Domain layer]
Store - is a Singleton that stores your State of application.
State - is a class that contains data like primitives, collections and other states.
Event - is a parent class for ModificationEvent that can contains "bundle" (payload).
ModificationEvent - is Event that implements Reducer to modify State of application.
Reducer - is a method in ModificationEvent that calls method of target State and returns it.
Middleware - is a class that working between sending event and calling it's Reducer and contains Conditin's to
add and handle with your business logic.
Notice #
- To catch your custom event in middleware, just create your own Event by extending
EventorModificationEvent.
Example:
- [ModificationEvent]
class IncrementEvent extends ModificationEvent<AppState, int> {
IncrementEvent({int bundle});
@override
get reducer => (AppState state, int bundle) => state..increment();
}
- [Event for handle some actions]
class SomeSideEffectEvent extends Event<AppState, void>{}
- [Middleware catching event]
class MyMiddleware extends Middleware<Store<AppState>> {
bool isIncrement(Event event) {
if (event is IncrementEvent) {
Future.delayed(Duration(seconds: 1), () {
store.dispatchEvent(event: IncrementEvent());
});
}
return nextEvent(true);
}
@override
List<Condition> get conditions => [isIncrement];
}
- [Presentation layer]
StoreProvider - is a wrapper of your entry point widget and Store to provide Store for all of Presenter's using
Provider package (https://pub.dev/packages/provider).
Presenter - is a class that have access to Store and being glue based of Stream's to separate domain and
presentation layers.
PresenterProvider - is a wrapper of your widget and Presenter to provide Presenter for all of widgets down of
widget tree using Provider package.
- [Rules]
-
stateStreamandpropertyStreamemits it's state/property only if last state is not equal to next state/property. -
Statesends new it's condition tostateStreamonly by dispatchingModificationEventtoStore -
Modify your
State's only by dispatchingModificationEvent's. -
Don't call
StoreProviderandStorein widgets, usePresenter's andPresenterProvider's to handle withStore. -
Use
Middlewaresfor all of your business rules. -
To throw
Eventthrough all ofMiddlewareconditions stack, you must return in all of conditions this call:
nextEvent(true); // or false if you wouldn't
- "Recommendation" try to use worker_manager package to save your fps. https://pub.dev/packages/worker_manager
- [Special features]
-
Statesupports comparison based on comparison of its meaningfulprops. So you will never get the same as previous state instateStream. So your UI won’t be called with the same data. -
Stateis the mutable class. Garbage collector won’t work so often. And already created Objects will be reused instead of creating new same ones. -
Storetracks app lifecycle and save itsStatewhen app goes to background and restore it when app back to foreground using Hive DB (https://pub.dev/packages/hive). So user can continue with the place/screen he stopped. -
Presentersupportsdispose. So if user goes to another screen all needed clean up and closing subscriptions toState's will be done. -
In
Presenter's you can listen to not only all newStatebut alsopropertyStreamfor listening only one property (which can be property of State or mapped Object fromState). So UI will be rebuilt less time.
Best way I know, to understand how to use it - it's look at examples, enjoy ! =)
Examples #
https://github.com/Renesanse/popular_news
https://github.com/Renesanse/osam/tree/master/example
#Author
- Daniil telegram: @dsrenesanse