dart_machine 0.1.2

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new46

State Machine for Dart v0.1.0

A library for Dart developers.

IDartMachine {
	
	/**
	 * What is the current state?
	 * @return The current state.
	 */
	String currentState();
	
	/**
	 * Does an action exist in the state machine?
	 * @param action The action in question.
	 * @return True if the action exists, false if it does not.
	 */
	bool actionExists( String checkAction );
	
	/**
	 * Add a valid link between two states.
	 * The state machine can then move between
	 * @param fromState State you want to move from.
	 * @param toState State you want to move to.
	 * @param action Action that when performed will move from the from state to the to state.
	 * @param handler Optional method that gets called when moving between these two states.
	 * @return true if link was added, false if it was not.
	 */
	bool addAction( String fromState, String toState, String action, [ Function handler = null ]);
	
	/**
	 * Adds a new state to the state machine.
	 * @param newState The new state to add.
	 * @return True is teh state was added, false if it was not.
	 */
	bool addState( String newState );
	
	/**
	 * Move from the current state to another state.
	 * @param toState New state to try and move to.
	 * @return True if the state machine has moved to this new state, false if it was unable to do so.
	 */
	bool changeState( String toState );
	
	/**
	 * Does a state exist?
	 * @param state The state in question.
	 * @return True if the state exists, false if it does not.
	 */
	bool stateExists( String checkState );
	
	/**
	 * Change the current state by performing an action.
	 * @param action The action to perform.
	 * @return True if the action was able to be performed and the state machine moved to a new state, false if the action was unable to be performed.
	 */
	bool performAction( String actionName );
	
	/**
	 * What are the valid actions you can perform from the current state?
	 * @return An array of actions.
	 */
	List<Action> validActions();
	
	/**
	 * What are the valid states you can get to from the current state?
	 * @return An array of states.
	 */
	List<State> validStates();
	
	/**
	 * Go back to the initial starting state
	 */
	void reset();
}

Usage

A simple usage example:

import 'package:dart_machine/dart_machine.dart';

main() {

  DartMachine dartMachine = new DartMachine();
  
	dartMachine.addState( STATE_BEGINS );
	
	dartMachine.addState( STATE_LOADING );
	dartMachine.addState( STATE_LOADING_COMPLETE );
	dartMachine.addState( STATE_LOADING_FAILED );

	dartMachine.addState( STATE_PREPARE_MODEL );
	dartMachine.addState( STATE_PREPARE_CONTROLLER );
	dartMachine.addState( STATE_PREPARE_VIEW );
	dartMachine.addState( STATE_PREPARE_COMPLETE );
	dartMachine.addState( STATE_READY );

	dartMachine.addAction(
		STATE_BEGINS,
		STATE_LOADING,
		ACTION_LOADING_START,
		() {
			print("> CURRENT state: " + dartMachine.currentState());
			scheduleMicrotask(() {
				print("> \t END OF microtask queue -> state: " + dartMachine.currentState());
				dartMachine.performAction(
					Random.secure().nextBool()
					? ACTION_LOADING_COMPLETE
					: ACTION_LOADING_FAILED
				);
			});
		}
	);

	dartMachine.addAction(
		STATE_LOADING,
		STATE_LOADING_COMPLETE,
		ACTION_LOADING_COMPLETE,
		() {
			print("> CURRENT state: " + dartMachine.currentState());
			scheduleMicrotask(() => print("> \t END OF microtask queue -> state: " + dartMachine.currentState()));
		}
	);

	dartMachine.addAction(
		STATE_LOADING,
		STATE_LOADING_FAILED,
		ACTION_LOADING_FAILED,
		() {
			print("> CURRENT state: " + dartMachine.currentState());
			scheduleMicrotask(() => print("> \t END OF microtask queue -> state: " + dartMachine.currentState()));
		}
	);

	dartMachine.performAction( ACTION_LOADING_START );
}

Features and bugs

1.0.0

  • Initial version, created by Stagehand

example/example.dart

import 'dart:async';
import 'dart:math';

import 'package:dart_machine/dart_machine.dart';

main() {

	String STATE_BEGINS = "state_begins";

	String STATE_LOADING = "state_loading";
	String STATE_LOADING_FAILED = "state_loading_failed";
	String STATE_LOADING_COMPLETE = "state_loading_complete";

	String STATE_PREPARE_MODEL = "state_prepare_model";
	String STATE_PREPARE_CONTROLLER = "state_prepare_controller";
	String STATE_PREPARE_VIEW = "state_prepare_view";
	String STATE_PREPARE_COMPLETE = "state_prepare_complete";
	String STATE_READY = "state_ready";

	String ACTION_LOADING_START = "action_start_loading";
	String ACTION_LOADING_FAILED = "action_loading_failed";
	String ACTION_LOADING_COMPLETE = "action_loading_failed";

	DartMachine stateMachine = new DartMachine();

	stateMachine.addState( STATE_BEGINS );

	stateMachine.addState( STATE_LOADING );
	stateMachine.addState( STATE_LOADING_COMPLETE );
	stateMachine.addState( STATE_LOADING_FAILED );

	stateMachine.addState( STATE_PREPARE_MODEL );
	stateMachine.addState( STATE_PREPARE_CONTROLLER );
	stateMachine.addState( STATE_PREPARE_VIEW );
	stateMachine.addState( STATE_PREPARE_COMPLETE );
	stateMachine.addState( STATE_READY );

	stateMachine.addAction(
			STATE_BEGINS,
			STATE_LOADING,
			ACTION_LOADING_START,
			() {
				print("> CURRENT state: " + stateMachine.currentState());
				scheduleMicrotask(() {
					print("> \t END OF microtask queue -> state: " + stateMachine.currentState());
					stateMachine.performAction(
							Random.secure().nextBool()
							? ACTION_LOADING_COMPLETE
							: ACTION_LOADING_FAILED
					);
				});
			});

	stateMachine.addAction(
			STATE_LOADING,
			STATE_LOADING_COMPLETE,
			ACTION_LOADING_COMPLETE,
			() {
				print("> CURRENT state: " + stateMachine.currentState());
				scheduleMicrotask(() => print("> \t END OF microtask queue -> state: " + stateMachine.currentState()));
			}
	);

	stateMachine.addAction(
			STATE_LOADING,
			STATE_LOADING_FAILED,
			ACTION_LOADING_FAILED,
			() {
				print("> CURRENT state: " + stateMachine.currentState());
				scheduleMicrotask(() => print("> \t END OF microtask queue -> state: " + stateMachine.currentState()));
			}
	);

	stateMachine.performAction( ACTION_LOADING_START );

}

Use this package as a library

1. Depend on it

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


dependencies:
  dart_machine: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:dart_machine/dart_machine.dart';
  
Version Uploaded Documentation Archive
0.1.2 Dec 6, 2018 Go to the documentation of dart_machine 0.1.2 Download dart_machine 0.1.2 archive
0.1.0 Dec 6, 2018 Go to the documentation of dart_machine 0.1.0 Download dart_machine 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
46
Learn more about scoring.

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

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:dart_machine/dart_machine.dart.

Health suggestions

Fix lib/src/Main.dart. (-0.50 points)

Analysis of lib/src/Main.dart reported 1 hint:

line 170 col 9: The method '_findAction' isn't used.

Format lib/src/model/Action.dart.

Run dartfmt to format lib/src/model/Action.dart.

Format lib/src/model/State.dart.

Run dartfmt to format lib/src/model/State.dart.

Maintenance suggestions

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
Dev dependencies
test ^1.0.0