Skip to content

Commit 6534909

Browse files
committed
format files
1 parent 1f8f19f commit 6534909

File tree

6 files changed

+79
-17
lines changed

6 files changed

+79
-17
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 1.3.0
2+
* ADDED: Global Observer Command State Listener
3+
14
## 1.2.0
25
* ADDEd: when operator
36

Diff for: README.md

+18
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,24 @@ These getters allow you to write cleaner and more intuitive code when interactin
130130

131131
---
132132

133+
## Observer
134+
135+
Sets the default observer listener for all commands.
136+
This listener is called whenever the state of any command changes.
137+
This can be useful for logging, debugging, or global state management.
138+
139+
140+
```dart
141+
main(){
142+
143+
Command.setObserverListener((state) {
144+
print(state);
145+
});
146+
147+
runApp(MainApp());
148+
}
149+
```
150+
133151
## Examples
134152

135153
### Example 1: Simple Command with No Arguments

Diff for: example/lib/main.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ class MyHomePage extends StatelessWidget {
6363
valueListenable: incrementCommand,
6464
builder: (context, state, snapshot) {
6565
return FloatingActionButton(
66-
onPressed: incrementCommand.isRunning ? null : () => incrementCommand.execute(),
66+
onPressed: incrementCommand.isRunning
67+
? null
68+
: () => incrementCommand.execute(),
6769
tooltip: 'Increment',
6870
child: const Icon(Icons.add),
6971
);

Diff for: example/pubspec.lock

+16-16
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ packages:
3737
dependency: transitive
3838
description:
3939
name: collection
40-
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
40+
sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf
4141
url: "https://pub.dev"
4242
source: hosted
43-
version: "1.18.0"
43+
version: "1.19.0"
4444
cupertino_icons:
4545
dependency: "direct main"
4646
description:
@@ -79,18 +79,18 @@ packages:
7979
dependency: transitive
8080
description:
8181
name: leak_tracker
82-
sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
82+
sha256: "7bb2830ebd849694d1ec25bf1f44582d6ac531a57a365a803a6034ff751d2d06"
8383
url: "https://pub.dev"
8484
source: hosted
85-
version: "10.0.5"
85+
version: "10.0.7"
8686
leak_tracker_flutter_testing:
8787
dependency: transitive
8888
description:
8989
name: leak_tracker_flutter_testing
90-
sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806"
90+
sha256: "9491a714cca3667b60b5c420da8217e6de0d1ba7a5ec322fab01758f6998f379"
9191
url: "https://pub.dev"
9292
source: hosted
93-
version: "3.0.5"
93+
version: "3.0.8"
9494
leak_tracker_testing:
9595
dependency: transitive
9696
description:
@@ -145,7 +145,7 @@ packages:
145145
path: ".."
146146
relative: true
147147
source: path
148-
version: "1.1.0"
148+
version: "1.2.0"
149149
result_dart:
150150
dependency: "direct main"
151151
description:
@@ -158,7 +158,7 @@ packages:
158158
dependency: transitive
159159
description: flutter
160160
source: sdk
161-
version: "0.0.99"
161+
version: "0.0.0"
162162
source_span:
163163
dependency: transitive
164164
description:
@@ -171,10 +171,10 @@ packages:
171171
dependency: transitive
172172
description:
173173
name: stack_trace
174-
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
174+
sha256: "9f47fd3630d76be3ab26f0ee06d213679aa425996925ff3feffdec504931c377"
175175
url: "https://pub.dev"
176176
source: hosted
177-
version: "1.11.1"
177+
version: "1.12.0"
178178
stream_channel:
179179
dependency: transitive
180180
description:
@@ -187,10 +187,10 @@ packages:
187187
dependency: transitive
188188
description:
189189
name: string_scanner
190-
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
190+
sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3"
191191
url: "https://pub.dev"
192192
source: hosted
193-
version: "1.2.0"
193+
version: "1.3.0"
194194
term_glyph:
195195
dependency: transitive
196196
description:
@@ -203,10 +203,10 @@ packages:
203203
dependency: transitive
204204
description:
205205
name: test_api
206-
sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
206+
sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c"
207207
url: "https://pub.dev"
208208
source: hosted
209-
version: "0.7.2"
209+
version: "0.7.3"
210210
vector_math:
211211
dependency: transitive
212212
description:
@@ -219,10 +219,10 @@ packages:
219219
dependency: transitive
220220
description:
221221
name: vm_service
222-
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
222+
sha256: f6be3ed8bd01289b34d679c2b62226f63c0e69f9fd2e50a6b3c1c729a961041b
223223
url: "https://pub.dev"
224224
source: hosted
225-
version: "14.2.5"
225+
version: "14.3.0"
226226
sdks:
227227
dart: ">=3.5.4 <4.0.0"
228228
flutter: ">=3.18.0-18.0.pre.54"

Diff for: lib/src/command.dart

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ part 'implementations.dart';
99
part 'states.dart';
1010
part 'types.dart';
1111

12+
void Function(CommandState state)? _defaultObserverListener;
13+
1214
/// Represents a generic command with lifecycle and execution.
1315
///
1416
/// This class supports state management, notifications, and execution
@@ -24,6 +26,13 @@ abstract class Command<T extends Object> extends ChangeNotifier
2426
_setValue(IdleCommand<T>(), metadata: {'reason': 'Command created'});
2527
}
2628

29+
/// Sets the default observer listener for all commands.
30+
/// This listener is called whenever the state of any command changes.
31+
/// This can be useful for logging, debugging, or global state management.
32+
static void setObserverListener(void Function(CommandState state) listener) {
33+
_defaultObserverListener = listener;
34+
}
35+
2736
/// The current state of the command.
2837
CommandState<T> _value = IdleCommand<T>();
2938

@@ -121,6 +130,7 @@ abstract class Command<T extends Object> extends ChangeNotifier
121130
return;
122131
}
123132
_value = newValue;
133+
_defaultObserverListener?.call(newValue);
124134
addHistoryEntry(CommandHistoryEntry(state: newValue, metadata: metadata));
125135
notifyListeners(); // Notify listeners using ChangeNotifier.
126136
}

Diff for: test/src/command_test.dart

+29
Original file line numberDiff line numberDiff line change
@@ -476,5 +476,34 @@ void main() {
476476

477477
expect(result, 'default value');
478478
});
479+
480+
test('Global Observable', () {
481+
final command1 = Command0<String>(() async => const Success('success 1'));
482+
final command2 = Command0<String>(
483+
() async => const Failure(AppException('success 2')));
484+
485+
final expectedValues = [
486+
isA<RunningCommand<String>>(),
487+
isA<SuccessCommand<String>>(),
488+
isA<RunningCommand<String>>(),
489+
isA<FailureCommand<String>>(),
490+
];
491+
var index = 0;
492+
493+
Command.setObserverListener(expectAsync1(
494+
max: expectedValues.length,
495+
(value) {
496+
expect(value, expectedValues[index++]);
497+
},
498+
));
499+
500+
command1.execute().then((_) => command2.execute());
501+
});
479502
});
480503
}
504+
505+
class AppException implements Exception {
506+
final String message;
507+
508+
const AppException(this.message);
509+
}

0 commit comments

Comments
 (0)