redux-observable

redux-observable dispatch actions

送分小仙女□ 提交于 2020-01-06 06:44:28
问题 I need to dispatch some actions in some order using redux-observable however, it takes just last action to dispatch. Please see example: export const fetchClientsEpic = (action$, { dispatch }) => action$ .ofType(fetchClients) .mapTo(fetchClientsPending(true)) .mergeMap(() => { return ajax .getJSON('some/get/clients/api') .map((clients: IClient[]) => { return fetchClientsSuccess( map(clients, (client, index) => ({ key: index, ...client, })), ); }); }); fetchClientsSuccess is dispatched with

How to test race condition of a redux observable epic

霸气de小男生 提交于 2020-01-06 05:47:32
问题 I have a use case where I need to cancel an Ajax call and do something else in an epic. There's an example in the redux-observable doc which fits my need exactly. However, when I try to test the racing condition in my epic, the "cancelation" doesn't seem to work. The example code is like: import { ajax } from 'rxjs/ajax'; const fetchUserEpic = action$ => action$.pipe( ofType(FETCH_USER), mergeMap(action => race( ajax.getJSON(`/api/users/${action.payload}`).pipe( map(response =>

How to handle async function in redux-observable?

。_饼干妹妹 提交于 2020-01-05 09:04:37
问题 I am using RxJS and redux-observable. I am trying to read file in epic. In my case, I have to do it in epic, because some other epic trigger this epic multiple "unknown" times by expand operator. But since FileReader is async, the code below does not work. What is the correct way especially RxJS way to handle this? Thanks export const uploadAttachmentEpic = (action$, store) => action$ .ofType(UPLOAD_ATTACHMENT) .map(action => { const reader = new FileReader(); reader.onload = () => { return {

rxjs throttle fetch until request has finished

若如初见. 提交于 2020-01-05 03:43:25
问题 I have an Observable that makes a request that gives back some information that is different each time. This observable is called by a user clicking a button. I want to prevent the user from clicking this button before the request has returned with information, and once it has returned, allow the user to click for an update once again. This is the request without stopping the user at any point. const fetchRepos = () => fetch( 'https://api.github.com/search/repositories?q=+language:javascript

Cancel request using redux observable is not working

谁都会走 提交于 2019-12-24 18:55:17
问题 I'm trying to add in canceling in my request using redux-observable. Trying to implement a simple login example. Currently, I am unable to submit the login request again after I added the cancelation. const loginUserApiCall = (username, password) => { return new Promise((resolve, reject) => { if (username === "foo" && password === "foo") { resolve({ user: { name: "foo", lastName: "fooLName", email: "foo@gmail.com" } }); } reject({ err: "Creds are incorrect" }); }); }; const loginRequestEpic =

Subscribe to a stream of events from server

有些话、适合烂在心里 提交于 2019-12-24 11:14:02
问题 I am expanding upon ping-pong example of redux-observable, and my final goal is to dispatch an action upon each received event from server . But i am having a bit of trouble wrapping my head around how to actually achieve this. What i have so far: 1.Upon opening a connection my server starts sending messages. // server.js setInterval(()=>{ ws.send(JSON.stringify({ type: 'INCREMENT', status: 200 })) console.log("sent some data") },3000) 2. On the client i have established an Observable of that

Redux Observable: Fire same epic for multiple actions

人走茶凉 提交于 2019-12-23 20:35:25
问题 I want to fire the same contents of my epic in response to several actions. Something like this: export default function uploadImage(action$, store) { return action$.ofType([ 'UPDATE_IMAGE, 'UPDATE_INTERESTS', 'UPDATE_LANGUAGE', ... ]) .switchMap(action => { Api.updateUser(); }) }; Is this possible? 回答1: You're correct in your self-answer, ofType accepts an arbitrary number of types as arguments. This isn't currently documented because in practice we've found almost always it's a sign of an

Should we create one Epic per action type? in redux-observable

為{幸葍}努か 提交于 2019-12-23 09:15:04
问题 I am in process of redux-observable learning, and I have some doubts: Should we create an Epic for each action to watch? export const actionEpic = action $ => action$.ofType('ACTION') export const action2Epic = action $ => action$.ofType('ACTION2') Or can we create it for many like reducers with switch? import every single Epic to convine in the middleware is a lot of work 回答1: A large majority of epics will begin by matching a single action, e.g. action$.ofType(SOMETHING) . This is because

How to chain async actions and wait for the result without store.dispatch

廉价感情. 提交于 2019-12-23 04:47:49
问题 I'm trying to write my INITIALIZE action which should chain some async actions together in the following way Call the initialize action. Call two async actions simultaneously. Wait for the completion of above actions. Run additional one action. Finish initialization. here is the redux flow that I expect INITIALIZATION_STARTED => ASYNC_ACTION_A_STARTED AND ASYNC_ACTION_B_STARTED => ASYNC_ACTION_A_FINISHED AND ASYNC_ACTION_B_FINISHED => ASYNC_ACTION_C_STARTED => ASYNC_ACTION_C_FINISHED =>

Fire multiple actions and wait for them to resolve RxJS / Redux Observables

别等时光非礼了梦想. 提交于 2019-12-23 01:52:07
问题 I have an action that I want to use to initialise my app, I want to create an epic for this action and then fire multiple other actions off the back of this, wait for them to all complete and there fire another action. I had a look at other questions and it's pretty similar to this one I've tried this approach and for me, it doesn't work, it fires the APP_INIT action then but then fails to fire any of the other actions in the sequence. Is anyone able to help? import { of } from 'rxjs'; import