State class

Represents a state that can be visited within a StateMachine instance.

States must be created from a StateMachine instance:

StateMachine door = new StateMachine('door');

State isOpen = door.newState('open');
State isClosed = door.newState('closed');

door.start(isOpen);

There are 3 things that can be done with states:

  1. Listen for onEnter events (every time the machine enters this state).
  2. Listen for onLeave events (every time the machine leaves this state).
  3. Determine if the state is active (machine is currently in this state).

To demonstrate:

// 1.
isOpen.onEnter.listen((StateChange change) {
  // onEnter stream event always includes the StateChange info.
  print('${change.from.name} --> ${isOpen.name}');
});

// 2.
isOpen.onLeave.listen((StateChange change) {
  // onLeave stream event always includes the StateChange info.
  print('${isOpen.name} --> ${change.to.name}');
});

// 3.
isOpen();   // true
isClosed(); // false

It's recommended that states be named in the format "isState". This may seem strange at first, but it has two main benefits:

  1. It helps differentiate states from transitions, which can be confusing since many words in English are the same as a verb and an adjective ("open" or "secure", for example).
  2. It reads better when calling the state to determine if it's active, as demonstrated above when calling isOpen() and isClosed().
Inheritance

Properties

didDispose Future<Null>
A Future that will complete when this object has been disposed.
read-onlyinherited
disposableTypeName String
A type name, similar to runtimeType but intended to work with minified code.
read-onlyoverride
disposalTreeSize int
The total size of the disposal tree rooted at the current Disposable instance.
read-onlyinherited
hashCode int
The hash code for this object.
read-onlyinherited
isDisposed bool
Whether this object has been disposed.
read-onlyinherited
isLeakFlagSet bool
Whether the leak flag for this object has been set.
read-onlyinherited
isOrWillBeDisposed bool
Whether the disposal of this object has been requested, is in progress, or is complete.
read-onlyinherited
name String
Name of the state. Used for debugging.
read / write
onEnter Stream<StateChange>
Stream of enter events. Enter event occurs every time the machine enters this state.
read-only
onLeave Stream<StateChange>
Stream of leave events. Leave event occurs every time the machine leaves this state.
read-only
runtimeType Type
A representation of the runtime type of the object.
read-onlyinherited

Methods

awaitBeforeDispose<T>(Future<T> future) Future<T>
Add future to a list of futures that will be awaited before the object is disposed.
@mustCallSuperinherited
call([dynamic _]) bool
Determine whether or not this State is active.
dispose() Future<Null>
Dispose of the object, cleaning up to prevent memory leaks.
inherited
flagLeak([String description]) → void
Flag the object as having been disposed in a way that allows easier profiling.
@mustCallSuperinherited
getManagedDelayedFuture<T>(Duration duration, T callback()) Future<T>
Creates a Future that will complete, with the value returned by callback, after the given amount of time has elapsed.
@mustCallSuperinherited
getManagedDisposer(Disposer disposer) ManagedDisposer
Automatically handle arbitrary disposals using a callback.
@mustCallSuperinherited
getManagedPeriodicTimer(Duration duration, void callback(Timer timer)) Timer
Creates a periodic Timer that will be cancelled if active upon disposal.
@mustCallSuperinherited
getManagedTimer(Duration duration, void callback()) Timer
Creates a Timer instance that will be cancelled if active upon disposal.
@mustCallSuperinherited
listenToStream<T>(Stream<T> stream, void onData(T event), {Function onError, void onDone(), bool cancelOnError}) StreamSubscription<T>
Returns a StreamSubscription which handles events from the stream using the provided onData, onError and onDone handlers.
@mustCallSuperinherited
manageAndReturnTypedDisposable<T extends Disposable>(T disposable) → T
Automatically dispose another object when this object is disposed.
@mustCallSuperinherited
manageCompleter<T>(Completer<T> completer) Completer<T>
Ensure that a completer is completed when the object is disposed.
@mustCallSuperinherited
manageDisposable(Disposable disposable) → void
inherited
manageStreamController(StreamController controller) → void
Automatically cancel a stream controller when this object is disposed.
@mustCallSuperinherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
onDispose() Future<Null>
Callback to allow arbitrary cleanup on dispose.
@protectedinherited
onWillDispose() Future<Null>
Callback to allow arbitrary cleanup as soon as disposal is requested (i.e. dispose is called) but prior to disposal actually starting.
@protectedinherited
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

any State
Wildcard state that should be used to define state transitions that allow transitioning from any state.
read / write