From f6346060b228cfabedceac471fffc8a2101a103a Mon Sep 17 00:00:00 2001 From: Sourcegraph Date: Thu, 18 Jan 2024 20:44:40 +0000 Subject: [PATCH] Convert w_flux Action to ActionV2 --- example/web/random_color/random_color.dart | 4 +- example/web/todo_app/actions.dart | 8 ++-- .../components/todo_app_component.dart | 2 +- test/action_test.dart | 48 +++++++++---------- test/store_test.dart | 22 ++++----- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/example/web/random_color/random_color.dart b/example/web/random_color/random_color.dart index e173c17..c26f547 100644 --- a/example/web/random_color/random_color.dart +++ b/example/web/random_color/random_color.dart @@ -35,7 +35,7 @@ main() async { } class RandomColorActions { - final Action changeBackgroundColor = Action(); + final ActionV2 changeBackgroundColor = ActionV2(); } class RandomColorStore extends Store { @@ -73,7 +73,7 @@ class _RandomColorComponent 'This module uses a flux pattern to change its background color.', react.button({ 'style': {'padding': '10px', 'margin': '10px'}, - 'onClick': (_) => actions.changeBackgroundColor() + 'onClick': (_) => actions.changeBackgroundColor(null) }, 'Change Background Color') ]); } diff --git a/example/web/todo_app/actions.dart b/example/web/todo_app/actions.dart index d7dd3cd..31b9019 100644 --- a/example/web/todo_app/actions.dart +++ b/example/web/todo_app/actions.dart @@ -19,8 +19,8 @@ import 'package:w_flux/w_flux.dart'; import 'store.dart'; class ToDoActions { - final Action createTodo = Action(); - final Action completeTodo = Action(); - final Action deleteTodo = Action(); - final Action clearTodoList = Action(); + final ActionV2 createTodo = ActionV2(); + final ActionV2 completeTodo = ActionV2(); + final ActionV2 deleteTodo = ActionV2(); + final ActionV2 clearTodoList = ActionV2(); } diff --git a/example/web/todo_app/components/todo_app_component.dart b/example/web/todo_app/components/todo_app_component.dart index 35a594c..e00ae38 100644 --- a/example/web/todo_app/components/todo_app_component.dart +++ b/example/web/todo_app/components/todo_app_component.dart @@ -48,7 +48,7 @@ class _ToDoAppComponent extends FluxComponent { } _clearList(_) { - this.actions.clearTodoList(); + this.actions.clearTodoList(null); } _createTodo(String value) { diff --git a/test/action_test.dart b/test/action_test.dart index 64bdfb5..6ced7f5 100644 --- a/test/action_test.dart +++ b/test/action_test.dart @@ -22,27 +22,27 @@ import 'package:test/test.dart'; void main() { group('Action', () { - late Action action; + late ActionV2 action; setUp(() { - action = Action(); + action = ActionV2(); addTearDown(action.dispose); }); test('should only be equivalent to itself', () { - Action action = Action(); - Action actionV2 = Action(); + ActionV2 action = ActionV2(); + ActionV2 actionV2 = ActionV2(); expect(action == action, isTrue); expect(action == actionV2, isFalse); }); test('should support dispatch without a payload', () async { - Action action = Action() + ActionV2 action = ActionV2() ..listen(expectAsync1((payload) { expect(payload, isNull); })); - await action(); + await action(null); }); test('should support dispatch by default when called with a payload', @@ -59,7 +59,7 @@ void main() { 'should invoke and complete synchronous listeners in future event in ' 'event queue', () async { var listenerCompleted = false; - var action = Action() + var action = ActionV2() ..listen((_) { listenerCompleted = true; }); @@ -76,7 +76,7 @@ void main() { test( 'should invoke asynchronous listeners in future event and complete ' 'in another future event', () async { - var action = Action(); + var action = ActionV2(); var listenerInvoked = false; var listenerCompleted = false; action.listen((_) async { @@ -99,7 +99,7 @@ void main() { }); test('should complete future after listeners complete', () async { - var action = Action(); + var action = ActionV2(); var asyncListenerCompleted = false; action.listen((_) async { await Future.delayed(Duration(milliseconds: 100), () { @@ -107,7 +107,7 @@ void main() { }); }); - Future? future = action(); + Future? future = action(null); expect(asyncListenerCompleted, isFalse); await future; @@ -115,51 +115,51 @@ void main() { }); test('should surface errors in listeners', () { - var action = Action()..listen((_) => throw UnimplementedError()); + var action = ActionV2()..listen((_) => throw UnimplementedError()); expect(action(0), throwsUnimplementedError); }); }); group('listen', () { test('should stop listening when subscription is canceled', () async { - var action = Action(); + var action = ActionV2(); var listened = false; var subscription = action.listen((_) => listened = true); - await action(); + await action(null); expect(listened, isTrue); listened = false; subscription.cancel(); - await action(); + await action(null); expect(listened, isFalse); }); test('should stop listening when listeners are cleared', () async { - var action = Action(); + var action = ActionV2(); var listened = false; action.listen((_) => listened = true); - await action(); + await action(null); expect(listened, isTrue); listened = false; await action.dispose(); - await action(); + await action(null); expect(listened, isFalse); }); test('should stop listening when actions are disposed', () async { - var action = Action(); + var action = ActionV2(); var listened = false; action.listen((_) => listened = true); - await action(); + await action(null); expect(listened, isTrue); listened = false; await action.dispose(); - await action(); + await action(null); expect(listened, isFalse); }); }); @@ -169,12 +169,12 @@ void main() { const int sampleSize = 1000; var stopwatch = Stopwatch(); - var awaitableAction = Action() + var awaitableAction = ActionV2() ..listen((_) => {}) ..listen((_) async {}); stopwatch.start(); for (var i = 0; i < sampleSize; i++) { - await awaitableAction(); + await awaitableAction(null); } stopwatch.stop(); var averageActionDispatchTime = @@ -184,7 +184,7 @@ void main() { late Completer syncCompleter; late Completer asyncCompleter; - var action = Action() + var action = ActionV2() ..listen((_) => syncCompleter.complete()) ..listen((_) async { asyncCompleter.complete(); @@ -193,7 +193,7 @@ void main() { for (var i = 0; i < sampleSize; i++) { syncCompleter = Completer(); asyncCompleter = Completer(); - await action(); + await action(null); await Future.wait([syncCompleter.future, asyncCompleter.future]); } stopwatch.stop(); diff --git a/test/store_test.dart b/test/store_test.dart index 4990b13..3d85135 100644 --- a/test/store_test.dart +++ b/test/store_test.dart @@ -94,10 +94,10 @@ void main() { }); test('should trigger in response to an action', () async { - Action _action = Action(); + ActionV2 _action = ActionV2(); store.triggerOnActionV2(_action); - _action(); + _action(null); Store payload = await store.first; expect(payload, store); @@ -106,7 +106,7 @@ void main() { test( 'should execute a given method and then trigger in response to an action', () { - Action _action = Action(); + ActionV2 _action = ActionV2(); bool methodCalled = false; syncCallback(_) { methodCalled = true; @@ -117,13 +117,13 @@ void main() { expect(payload, store); expect(methodCalled, isTrue); }) as StoreHandler); - _action(); + _action(null); }); test( 'should execute a given async method and then trigger in response to an action', () { - Action _action = Action(); + ActionV2 _action = ActionV2(); bool afterTimer = false; asyncCallback(_) async { await Future.delayed(Duration(milliseconds: 30)); @@ -135,13 +135,13 @@ void main() { expect(payload, store); expect(afterTimer, isTrue); }) as StoreHandler); - _action(); + _action(null); }); test( 'should execute a given method and then trigger in response to an action with payload', () { - Action _action = Action(); + ActionV2 _action = ActionV2(); num? counter = 0; store.triggerOnActionV2(_action, (num? payload) => counter = payload); store.listen(expectAsync1((payload) { @@ -172,7 +172,7 @@ void main() { test('cleans up its ActionSubscriptions on dispose', () { bool afterDispose = false; - Action _action = Action(); + ActionV2 _action = ActionV2(); store.triggerOnActionV2(_action); store.listen(expectAsync1((payload) async { // Safety check to avoid infinite trigger loop @@ -183,15 +183,15 @@ void main() { afterDispose = true; // This should no longer fire after dispose - _action(); + _action(null); }) as StoreHandler); - _action(); + _action(null); }); test('does not allow adding action subscriptions after dispose', () async { await store.dispose(); - expect(() => store.triggerOnActionV2(Action()), throwsStateError); + expect(() => store.triggerOnActionV2(ActionV2()), throwsStateError); }); test('does not allow listening after dispose', () async {