Skip to content

Commit

Permalink
Let [Machine] subclasses create custom [State] subclasses (#10).
Browse files Browse the repository at this point in the history
  • Loading branch information
renggli committed Jul 3, 2022
1 parent 956a189 commit bb30885
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/src/machine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@ class Machine<T> {
final StreamController<TransitionEvent<T>> _afterTransitionController =
StreamController.broadcast(sync: true);

/// Internal helper that can be overridden by subclasses to customize the
/// creation of [State] objects.
@protected
State<T> createState(T identifier) => State<T>(this, identifier);

/// Returns a new state. The first call to this method defines the start state
/// of the machine. To identify states a unique identifier has to be provided.
/// of the machine. To identify states a unique [identifier] has to be provided.
State<T> newState(T identifier) {
if (_states.containsKey(identifier)) {
throw ArgumentError.value(
identifier, 'identifier', 'Duplicated state identifier');
}
final state = State<T>(this, identifier);
final state = createState(identifier);
_states[identifier] = state;
_start ??= state;
return state;
Expand Down

0 comments on commit bb30885

Please sign in to comment.