osam 2.6.2 osam: ^2.6.2 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 #
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
Event
orModificationEvent
.
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-web.flutter-io.cn/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]
-
stateStream
andpropertyStream
emits it's state/property only if last state is not equal to next state/property. -
State
sends new it's condition tostateStream
only by dispatchingModificationEvent
toStore
-
Modify your
State
's only by dispatchingModificationEvent
's. -
Don't call
StoreProvider
andStore
in widgets, usePresenter
's andPresenterProvider
's to handle withStore
. -
Use
Middlewares
for all of your business rules. -
To throw
Event
through all ofMiddleware
conditions 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-web.flutter-io.cn/packages/worker_manager
- [Special features]
-
State
supports 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. -
State
is the mutable class. Garbage collector won’t work so often. And already created Objects will be reused instead of creating new same ones. -
Store
tracks app lifecycle and save itsState
when app goes to background and restore it when app back to foreground using Hive DB (https://pub-web.flutter-io.cn/packages/hive). So user can continue with the place/screen he stopped. -
Presenter
supportsdispose
. 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 newState
but alsopropertyStream
for 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