Skip to content
This repository has been archived by the owner on Feb 18, 2019. It is now read-only.

AsyncAction bug #3

Closed
yohom opened this issue Sep 7, 2018 · 2 comments
Closed

AsyncAction bug #3

yohom opened this issue Sep 7, 2018 · 2 comments
Assignees
Labels
wontfix This will not be worked on

Comments

@yohom
Copy link

yohom commented Sep 7, 2018

Dispatch an AsyncAction invokes the middleware's beforeAction method twice.I tweak a little on your example:

import 'dart:async';

import 'package:flutter/material.dart' hide State;
import 'package:flutter_redurx/flutter_redurx.dart';

class State {
  State({this.title, this.count});
  final String title;
  final int count;
}

/***************** tweaked ****************/
class Increment extends AsyncAction<State> {
  @override
  Future<Computation<State>> reduce(State state) async {
    print('in action: Increment: $state');
    return (State state) => State(title: state.title, count: state.count + 1);
  }
}
/***************** tweaked ****************/

void main() {
  /***************** tweaked ****************/
  final store =
      Store<State>(State(title: 'Flutter-ReduRx Demo Title', count: 0))
          .add(LogMiddleware());
  /***************** tweaked ****************/
  runApp(Provider(store: store, child: MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter-ReduRx Demo',
      theme: ThemeData(primarySwatch: Colors.pink),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Connect<State, String>(
          convert: (state) => state.title,
          where: (prev, next) => next != prev,
          builder: (title) {
            print('Building title: $title');
            return Text(title);
          },
        ),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('You have pushed the button this many times:'),
            Connect<State, String>(
              convert: (state) => state.count.toString(),
              where: (prev, next) => next != prev,
              builder: (count) {
                print('Building counter: $count');
                return Text(count, style: Theme.of(context).textTheme.display1);
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => Provider.dispatch<State>(context, Increment()),
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

This will output

12:40:44.015 1 info flutter.tools flutter: Before action: Increment: Instance of 'State'
12:40:44.015 2 info flutter.tools flutter: in action: Increment: Instance of 'State'
12:40:44.015 3 info flutter.tools flutter: Before action: Increment: Instance of 'State'
12:40:44.015 4 info flutter.tools flutter: After action: Increment: Instance of 'State'
12:40:44.038 5 info flutter.tools flutter: Building counter: 1
@leocavalcante leocavalcante self-assigned this Sep 7, 2018
@leocavalcante leocavalcante added the wontfix This will not be worked on label Sep 7, 2018
@leocavalcante
Copy link
Owner

I'm adding a [wontfix] for now because this isn't a bug, it is a expected behavior. Before middlewares are indeed computed twice at redurx.dart#L47 and redurx.dart#L50. That is because being a asynchronous execution, the first call is right after the Action dispatch e the second call is when Action fulfilled its asynchronous behavior, but didn't added it's computation as a new state to the Store.

But sure, I would like to discuss if this is a good behavior. What do you think? Are you having trouble with it?

@leocavalcante
Copy link
Owner

Closed due inactivity.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants