Skip to content

Commit 88fcad3

Browse files
committed
chore(state) expose State.none
1 parent 1097626 commit 88fcad3

3 files changed

Lines changed: 10 additions & 16 deletions

File tree

lib/src/state_machine.dart

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,6 @@ class State extends Disposable implements Function {
120120
});
121121
}
122122

123-
static const _NONE_STATE_NAME = '__none__';
124-
125-
State._none(StateMachine machine) : this._(_NONE_STATE_NAME, machine);
126-
127123
State._wildcard() : this._('__wildcard__', null, listenTo: false);
128124

129125
/// Stream of enter events. Enter event occurs every time
@@ -148,24 +144,22 @@ class State extends Disposable implements Function {
148144
/// You can check that condition with:
149145
///
150146
/// initialState.onEnter((change) {
151-
/// if (!change.from.isNone) {
147+
/// if (!change.from == State.none) {
152148
/// // do something here
153149
/// }
154150
/// })
155151
///
156152
/// to ignore this false initial event.
157153
///
158154
/// bool, true if this state is None,
159-
/// false otherwise
160-
bool get isNone {
161-
return name == _NONE_STATE_NAME;
162-
}
155+
/// false otherwise
156+
static final none = State._('__none__', null, listenTo: false);
163157

164158
@override
165159
String toString() {
166160
String name = this.name;
167-
if (isNone) {
168-
name = 'none - state machine has yet to start';
161+
if (this == none) {
162+
return 'State: none - state machine has yet to start';
169163
}
170164
return 'State: $name (active: ${this()}, machine: ${_machine.name})';
171165
}
@@ -199,13 +193,13 @@ class StateChange {
199193
/// }
200194
/// })
201195
bool get isInitial {
202-
return this.from.isNone;
196+
return this.from == State.none;
203197
}
204198

205199
@override
206200
String toString() {
207201
StringBuffer sb = StringBuffer();
208-
String fromName = from.isNone ? '(none)' : from.name;
202+
String fromName = isInitial ? '(none)' : from.name;
209203
sb.writeln('StateChange: ${fromName} --> ${to.name}');
210204
if (payload != null) {
211205
sb.writeln(' payload: $payload');
@@ -284,7 +278,7 @@ class StateMachine extends Disposable {
284278
/// Start the machine in a temporary state.
285279
/// This allows an initial state transition to occur
286280
/// when the machine is started via [start].
287-
_current = State._none(this);
281+
_current = State.none;
288282

289283
manageDisposable(_current);
290284
}

test/state_machine_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void main() {
3232

3333
test('should be in a dummy state until the machine has been started', () {
3434
expect(machine.current.name, equals('__none__'));
35-
expect(machine.current.isNone, isTrue);
35+
expect(machine.current == State.none, isTrue);
3636
});
3737

3838
test('should throw if machine is started more than once', () {

test/state_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void main() {
7979

8080
test('.toString() should explain what the __none__ state means', () {
8181
State noneState = machine.current;
82-
expect(noneState.isNone, isTrue);
82+
expect(noneState == State.none, isTrue);
8383
String s = noneState.toString();
8484
expect(s, contains('machine has yet to start'));
8585
expect(s, isNot(contains('__none__')));

0 commit comments

Comments
 (0)