bloc_flux 0.1.0

  • Readme
  • Changelog
  • Installing
  • 31

A reactive flux-like state management library using the BLoC pattern.

Overview #

The goal of the bloc_flux package is to take the best parts of the Flux and BLoC state management patterns.

General Architecture

The general architecture of a bloc_flux application is similar to that of a Flux application. A bloc_flux application consists of a single Dispatcher that dispatches Actions to all registered Blocs. All data flows from component to component in Observables. Observables (from RxDart) are like streams with more built in features for stream transformations and are by default synchronous whereas most stream implementations are asynchronous. The only input and output to the Dispatcher occur in the form of Actions. The Blocs then receive any dispatched Actions from the Dispatchers actionObservable. The Blocs do not receive input from anywhere else. Blocs then update their state in response to the Action. Any changed values will be added to an output Observable. Views listen to the output observables on the Blocs and rebuild whenever a new value is available. The output observable also emits the last emitted value to every new listener so default init values do not have to be specified.

Observables/Streams #

  • A. Output Observable: actionObservable is the observable that emits all Actions dispatched to the Dispatcher. The Blocs listen to this Observable as their only form of input.
  • B. Data Observables: output Observables from Blocs containing the data used by things like UI components/views. Views listen to these observables and redraw/update when a new value is emitted. Bloc output Observables will also emit the last emitted value to every new listener that is added. This means views do not have to wait for a value to be updated to display correct information.
  • C. Action Input: The only input to the Dispatcher. Actions are dispatched to the Dispatcher by calling the dispatch method or by adding an input Observable by calling the addInputObservable

Glossary #

Actions are the only form of input to the Blocs (via the Dispatcher). Actions represent a single event happening in the application. These can be anything from a UI event like a button click to asynchronous network responses.

Blocs manage the state of a domain of the application. The domain can be anything from a single widget/component to entire app/web pages. Blocs can also be used to perform IO operations like networks calls and state persistence. This is the equivalent to a flux or redux store.

Dispatchers are the central hub that all data flows through. Data can be dispatched to a Dispatcher by calling the dispatch(Action action) method. All data is wrapped in an Action. The dispatcher will then dispatch the Action to all registered Blocs. The dispatcher allows for easy logging and debugging as any events affecting the state of the application will flow through the Dispatcher and can be logged.

Fields represent a single output from a Bloc. They allow access to an observable (stream) and the last value that was emitted from the observable. Most Blocs will contain multiple fields.

Main Advantages #

The main goal of bloc_flux is to combine the reusability and compartmentalization of the BLoC pattern and the centralization of the Flux pattern.

  • Compartmentalized: every domain in an application will have its own Bloc. This allows for domain specific functionality to be localized into one component. The outputs from each Bloc are also compartmentalized as each output should consist of one “unit” of information. With most other state management patterns and packages the output consists of one entire state. This makes efficiently redrawing UI components difficult as a new state does not necessarily mean the specific information the UI component cares about has changed.
  • Encapsulated: Blocs only provide one source of input (the actionObservable) and an output for each of the relevant data fields. Blocs cannot be manipulated from anything other than input actions.
  • Testable: having business logic compartmentalized makes unit and component testing significantly easier. Simulating test scenarios also become easier due to Actions. A good example of this is a network response. Because Blocs only have one input they cannot rely on other components with potentially complex APIs. This reduces the number of mock objects needed when writing tests.
  • Debuggable: the centrality of the Dispatcher makes finding what Action caused an error easier. When the Action that caused an error if found it narrows down the number of components that could possibly be causing the issue as most Actions usually only affect the state of one Bloc. The compartmentalization of Blocs also prevents manipulation of state from anywhere outside the Bloc making errors less likely to occur and when they do easier to find.
  • Portable: Blocs can be reused across different projects and platforms as they by definition do not rely on any external output other than the Action input stream.

Counter Example #

Counter Architecture

This diagram represents that state management architecture of a simple counting app that has 3 main functions:

  1. Displays current count in Counter Display.
  2. Increments the counter when the Increment Button is clicked.
  3. Persists the count to local (or server) storage.

In bloc_flux all data flow is through Observables (streams). In the above diagram every arrow represents a stream. An action stream is a stream which only ever emits Actions. A data stream stream refers to any stream not containing exclusively Actions. This can be any data from primitives to complex objects.

Observables/Streams #

  • A. The actionObservable from the Dispatcher. This is the observable that all Blocs receive their Action inputs from.
  • B. A data output observable from the Counter Bloc that emits the new count whenever it is updated. Counter View listens to this observable and redraws/updates itself whenever a new count is emitted.
  • C. The input stream for the Dispatcher. Actions can be added to the input stream from anywhere in an the application. The most common type of input would be UI events (like button clicks)but other Blocs can also add Actions to the input stream. This can be done by calling the dispatch method or by adding an Observable using the addInputObservable method.
  • D. Action output from Blocs. Blocs can also dispatch Actions in response to other Actions. The ValueBloc extends Bloc class allows for easy dispatching of Actions and automatic dispatching of FieldValueActions when Field values are updated. A FieldValueAction is an Action with a non null FieldID identifying the Field the value came from and a non null data field with the new value.

Components #

  • Increment Button: This button dispatches an Action when it is clicked, telling the Counter Bloc to increment the counter.
  • Counter Display: Displays the current count. The display will listen to the count output Observable on the Counter Bloc and will rebuild whenever a new count is avaliable.
  • Counter Bloc: This Bloc is responsible for managing the state of the counter function. All Blocs recieve all Actions dispatched to the Dispatcher but will only mutate its state it response to some of them. In the case of the Counter Bloc these Actions are IncrementAction (increment the count by 1), and SetCountAction (set the count). These Actions will be implemented in the example code. The Counter Bloc will have a counter Observable that emitts a new count whenever it is updated. The count wrapped in a FieldValueAction will also be dispatched after the Persistence Bloc requests updates using a FieldQuery.
  • Persistence Bloc: This Bloc is responsible for presisting the count (the state of the Counter Bloc) to local storage. For this example this will be simulated to allow the code to be run on any platform. The Persistence Bloc recieves the updated counts by dispatching a FieldQueryAction wrapping a FieldQuery. It is important that the Bloc does not directly listen to the count Observable as this defeats the purpose of having a central Dispatcher.
  • Dispatcher: The dispatcher has actions dispatched to it. The Actions are than sent to the Blocs. In this example the Increment Button dispatches an Action to the Dispatcher. The Action will be forwarded to the registered Blocs. In most cases the default Dispatcher has enough functionality and is already implemented.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

1.0.0 #

  • Initial version

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  bloc_flux: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:bloc_flux/bloc_flux.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
55
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
31
Learn more about scoring.

We analyzed this package on Dec 10, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21

Health issues and suggestions

Fix lib/src/serializers/composite_serializers.dart. (-44.59 points)

Analysis of lib/src/serializers/composite_serializers.dart failed with 2 errors, 3 hints:

line 20 col 7: Missing concrete implementation of getter Serializers.builderFactories.

line 372 col 7: Missing concrete implementations of SerializersBuilder.merge and SerializersBuilder.mergeAll.

line 168 col 11: Use rethrow to rethrow a caught exception.

line 301 col 16: Don't explicitly initialize variables to null.

line 320 col 16: Don't explicitly initialize variables to null.

Fix lib/src/field/field_id.dart. (-0.50 points)

Analysis of lib/src/field/field_id.dart reported 1 hint:

line 35 col 40: Avoid types as parameter names.

Fix lib/src/query/state_query.dart. (-0.50 points)

Analysis of lib/src/query/state_query.dart reported 1 hint:

line 59 col 43: Avoid types as parameter names.

Maintenance issues and suggestions

Support latest dependencies. (-20 points)

The version constraint in pubspec.yaml does not support the latest published versions for 2 dependencies (built_value, rxdart).

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and bloc_flux.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
built_collection ^4.1.0 4.3.0
built_value ^6.3.0 6.8.2 7.0.0
meta ^1.1.7 1.1.8
mockito ^4.0.0 4.1.1
quiver >=2.0.0 <3.0.0 2.1.2+1
rxdart ^0.20.0 0.20.0 0.22.6
Transitive dependencies
analyzer 0.38.5 0.39.2+1
analyzer_plugin 0.2.1
args 1.5.2
async 2.4.0
boolean_selector 1.0.5
build 1.2.2
build_config 0.4.1+1
charcode 1.1.2
checked_yaml 1.0.2
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
dart_style 1.3.3
fixnum 0.10.11
front_end 0.1.27 0.1.29
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
json_annotation 3.0.0
kernel 0.3.27 0.3.29
logging 0.11.3+2
matcher 0.12.6
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
path 1.6.4
pedantic 1.9.0
pub_semver 1.4.2
pubspec_parse 0.1.5
source_gen 0.9.4+6
source_span 1.5.5
stack_trace 1.9.3
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
test_api 0.2.11
typed_data 1.1.6
watcher 0.9.7+13
yaml 2.2.0
Dev dependencies
bloc_flux_testing ^0.1.0
build_runner ^1.1.3
built_value_generator ^6.3.0 6.8.2 7.0.0
test ^1.0.0