states 1.0.0-beta states: ^1.0.0-beta copied to clipboard
States is Finite State Machine for Dart. You can define states of your application and reaction on transition between states
States - Beautiful Finite State Machine (FMS) for Dart #
API: #
abstract class IStates {
String get current;
bool get locked;
List<StatesTransition> get all;
List<StatesTransition> actions({String from});
List<StatesMeta> metas({String from});
StatesMeta add( String state );
StatesTransition when({
String from,
String to,
String on,
StatesTransitionFunction run
});
String subscribe(StatesTransitionFunction listener);
bool unsubscribe(String subscriptionKey);
bool change({ String to, bool run = true });
bool has({
String action,
String state,
bool conform = true
});
StatesTransition get( String action );
bool run( String action );
void reset();
void dispose();
void lock({@required String key});
void unlock({@required String key});
}
Usage #
A simple usage example: #
States states = new States();
states.add( STATE_INITIAL );
states.add( STATE_LOADING );
states.add( STATE_LOADING_COMPLETE );
states.add( STATE_LOADING_FAILED );
states.when(
from: STATE_INITIAL,
to: STATE_LOADING,
on: ACTION_LOADING_START,
run: (StatesTransition action) {
print("> CURRENT on ACTION_LOADING_START state: " + states.current);
scheduleMicrotask(() {
print("> \t END OF microtask queue -> state: " + states.current);
var nextBool = Random.secure().nextBool();
print("> \t\t next bool: " + nextBool.toString());
states.run( nextBool ? ACTION_LOADING_COMPLETE : ACTION_LOADING_FAILED );
});
});
states.when(
from: STATE_LOADING,
to: STATE_LOADING_COMPLETE,
on: ACTION_LOADING_COMPLETE,
run: (StatesTransition action) {
print("> CURRENT on ACTION_LOADING_COMPLETE - state: " + states.current);
scheduleMicrotask(() => print("> \t END OF microtask queue -> state: " + states.current));
}
);
states.when(
from: STATE_LOADING,
to: STATE_LOADING_FAILED,
on: ACTION_LOADING_FAILED,
run: (StatesTransition action) {
print("> CURRENT on ACTION_LOADING_FAILED state: " + states.current);
scheduleMicrotask(() => print("> \t END OF microtask queue -> state: " + states.current));
}
);
states.run( ACTION_LOADING_START );
SPA with States #
Please take a look how this library can help to create SPA with simple dart:html See folder example/spa_with_states