state_graph_bloc 0.1.0+2 copy "state_graph_bloc: ^0.1.0+2" to clipboard
state_graph_bloc: ^0.1.0+2 copied to clipboard

outdated

A bloc package with concise state graph builder. Build transitions, side effects and global events in a centralised, type-safe manner.

state_graph_bloc #

State Graph Builder for Dart. Allows to define states, their transitions and side effects in a result of incoming events.

Inspired by Tinder's state machine Kotlin DSL. Based on awesome BLoC package.

Usage #

StateGraphBloc is just a subclass of Bloc from bloc package which overrides mapEventToState function to use graph map.

Read more about bloc package and how to use it at bloc homepage. To use it with flutter, consider also flutter_bloc.

class DummyBloc extends StateGraphBloc<DummyEvent, DummyState> {
  @override
  DummyState get initialState => StateInitialising();

  @override
  StateGraph<DummyEvent, DummyState> buildGraph() =>
      StateGraph<DummyEvent, DummyState>(
        {
          StateInitialising: {
            LoadEvent: transitionWithSideEffect(
              (state, event) => StateLoading(),
              (state, event) {
                // There could be some async loading. Use [launchFuture] or [launchStream]
                // to fire async processing and properly handle bloc closing.
                add(LoadingCompletedEvent());
              },
            ),
          },
          StateLoading: {
            LoadingCompletedEvent: transition((state, _) => StateReady(0)),
          },
          StateReady: {
            IncrementEvent: transition(
              (state, event) => StateReady((state as StateReady).counter + 1),
            ),
            // Test global event override.
            RestartEvent: transition((state, _) => StateReady(0)),
          },
        },
        {
          RestartEvent: transition((state, event) {
            return StateInitialising();
          })
        },
      );

  // Bind some variables based on [state].
  Stream<bool> isLoading() => bindState((state) {
        if (state is StateLoading) {
          return true;
        } else {
          return false;
        }
      });
}

Samples #

You can experiment with sample application hosted here: https://gitlab.com/marcin.jelenski/bloc-showcase

Also test directory contains all State Graph Bloc use cases.

8
likes
0
pub points
0%
popularity

Publisher

verified publisherjelenski.net

A bloc package with concise state graph builder. Build transitions, side effects and global events in a centralised, type-safe manner.

Repository (GitLab)
View/report issues

License

unknown (LICENSE)

Dependencies

bloc, rxdart

More

Packages that depend on state_graph_bloc