How to do periodic / scheduled actions using Flutter Redux? - redux

I'm building a mobile app using Flutter, in which I use flutter_redux and redux_thunk.
There's two kind of timed actions I want to achieve:
Dispatch certain action every N seconds (repeated)
Dispatch an action once after N seconds, likely from a thunk action (single run)
Are there any packages that wrap this logic?
What would be your suggested way to achieve the two kind of scheduled actions?

I don't think there's anything special that you'd need to do specifically for Flutter Redux. In Dart, the general way to do periodic operations would be to use Timer.periodic:
Timer.periodic(Duration(seconds: N), () => store.dispatch(action));
For non-periodic operations, you could use a non-periodic Timer or use Future.delayed. (Timer gives you the ability to easily cancel it, but Future gives you a more direct way for the caller to specify how exceptions are handled.)

Related

If i have 100 reducers in my react app will all reducers get called when an action is dispatched?

If so will this lead to a performance issue ?
Secondly
Is there a way to specifically call a reducer without invoking all 100 reducers ?
This is specifically answered in the Redux FAQ:
https://redux.js.org/faq/performance#wont-calling-all-my-reducers-for-each-action-be-slow
It's important to note that a Redux store really only has a single reducer function.
However, even if you happen to have many different reducer functions composed together, and even with deeply nested state, reducer speed is unlikely to be a problem. JavaScript engines are capable of running a very large number of function calls per second, and most of your reducers are probably just using a switch statement and returning the existing state by default in response to most actions.
Also, "call a reducer" is the wrong mental model here. Actions are like "events" that are being broadcast - you don't ever "call a reducer" directly:
https://redux.js.org/style-guide/style-guide#model-actions-as-events-not-setters

How to run multiple Firestore Functions Sequentially?

We have 20 functions that must run everyday. Each of these functions do something different based on inputs from the previous function.
We tried calling all the functions in one function, but it hits the timeout error as these 20 functions take more than 9 minutes to execute.
How can we trigger these multiple functions sequentially, or avoid timeout error for one function that executes each of these functions?
There is no configuration or easy way to get this done. You will have to set up a fair amount of code and infrastructure to get this done.
The most straightforward solution involves chaining together calls using pubsub type functions. You can send a message to a pubsub topic that will trigger the next function to run. The payload of the message to send can be the parameters that the function should use to determine how it should operate. If the payload is too big, or some more complex sources of data are required to make that decision, you can use a database to store intermediate data that the next function can query and use.
Since we don't have any more specific details about how your functions actually work, nothing more specific can be said. If you run into problems with a specific detail of this scheme, please post again describing that specifically you're trying to do and what's not working the way you expect.
There is a variant to the Doug solution. At the end of the function, instead of publishing a message into pubsub, simply write a specific log (for example " end").
Then, go to stackdriver logging, search for this specific log trace (turn on advanced filters) and configure a sink into a PubSub topic of this log entry. Thereby, every time that the log is detected, a PubSub message is published with the log content.
Finally, plug your next function on this PubSub topic.
If you need to pass values from function to another one, you can simply add these values in the log trace at the end of the function and parse it at the beginning of the next one.
Chaining functions is not an easy things to do. Things are coming, maybe Google Cloud Next will announce new products for helping you in this task.
If you simply want the functions to execute in order, and you don't need to pass the result of one directly to the next, you could wrap them in a scheduled function (docs) that spaces them out with enough time for each to run.
Sketch below with 3 minute spacing:
exports.myScheduler = functions.pubsub
.schedule('every 3 minutes from 22:00 to 23:00')
.onRun(context => {
let time = // check the time
if (time === '22:00') func1of20();
else if (time === '22:03') func2of20();
// etc. through func20of20()
}
If you do need to pass the results of each function to the next, func1 could store its result in a DB entry, then func2 starts by reading that result, and ends by overwriting with its own so func3 can read when fired 3 minutes later, etc. — though perhaps in this case, the other solutions are more tailored to your needs.

Firestore callback inside of signInSuccessWithAuthResult to get extra userdata?

I'm using firebaseui, and it has the signInSuccessWithAuthResult callback which returns me the user.
So inside of there I want to call back out to a firestore document where I have more user profile data to use and save.
But I think this method is completing before the firestore .get() is and just never works.
Am I thinking about this problem wrong? Is this just not the right place to do this?
But I think this method is completing before the Firestore .get() is and just never works.
You are guessing right, the operation of adding a listener is asynchronous and returns immediately and the callback from the Task it returns will be called sometime later. There are no guarantees about how long it will take. Depending on your connection speed and the state, it may take from a few hundred milliseconds to a few seconds before the authentication process is complete.
If you want to use the results of your authentication process, you must wait until the asynchronous operation is complete. This means that you can only use the results inside the listener callback itself.

redux-thunk and redux-saga simultaneously

I've come across two sources where it explains how to use two middleware systems together.
The first one says:
You can add the saga middleware right alongside the thunk middleware. Remember, the order you list middleware does matter.
Code:
const store = createStore(reducer, applyMiddleware(thunk, sagaMiddleware))
The second one provides this part of code source2:
createStore(rootReducer,applyMiddleware(sagaMiddleware, thunk)
Is it ok? Or the first one just remind us that the order matters but in case of the order between saga and thunk it doesn't matter? Maybe there's other reasons to warn about order relatively saga and thunk?
Yes, you can absolutely use both middleware together.
The ordering matters because the middleware pipeline order is based on the order of arguments to applyMiddleware(). That said, it's primarily a concern when you have a custom middleware that calls next(action), which forwards the action to the next middleware in the pipeline. With thunks and sagas you're normally calling dispatch(action), which always starts at the beginning of the pipeline.
For more details, see the Redux FAQ entry on "what is the difference between next and dispatch in a middleware?".
you can use putResolve from saga api
putResolve(action)
Just like put but the effect is blocking (if promise is returned from dispatch it will wait for its resolution) and will bubble up errors from downstream.
https://redux-saga.js.org/docs/api/
Since redux-thunks return a promise, you can call them directly in your sagas and wait for a return value.
Also, it will bubble up any errors.

How to use flux with firebase's sometimes synchronous callbacks

I am trying to use flux with firebase, and found out that the Firebase.on() callbacks are sometimes triggered synchronously by a Firebase.set() called within the same client.
This does not jive with the flux pattern of dispatching an action when the user triggers a firebase set and another one when the value comes back, which will break the multiple dispatch invariant.
A workaround would be to delay the mutation calls with process.nextTick(). Is there a way to force the callbacks to be called in the next tick instead, like how promises work? Or is there a recommended pattern here?

Resources