From bf9e6902520fd07b48ad8dd683379f65458c40d4 Mon Sep 17 00:00:00 2001 From: gstvg <28798827+gstvg@users.noreply.github.com> Date: Fri, 18 Sep 2020 10:38:20 -0300 Subject: [PATCH 1/2] Update index.js --- packages/ra-realtime/src/index.js | 110 +++++++++++++++--------------- 1 file changed, 55 insertions(+), 55 deletions(-) diff --git a/packages/ra-realtime/src/index.js b/packages/ra-realtime/src/index.js index 05f664fe8ac..a2d7a85100b 100644 --- a/packages/ra-realtime/src/index.js +++ b/packages/ra-realtime/src/index.js @@ -1,66 +1,66 @@ -import { LOCATION_CHANGE } from 'react-router-redux'; -import { takeLatest, call, put, take, cancelled } from 'redux-saga/effects'; -import { CRUD_GET_LIST, CRUD_GET_ONE, FETCH_START, FETCH_END } from 'ra-core'; -import omit from 'lodash/omit'; +import { LOCATION_CHANGE } from "connected-react-router"; +import { takeLatest, call, put, take, cancelled } from "redux-saga/effects"; +import { CRUD_GET_LIST, CRUD_GET_ONE, FETCH_START, FETCH_END } from "ra-core"; +import omit from "lodash/omit"; -import buildAction from './buildAction'; -import createObserverChannel from './createObserverChannel'; +import buildAction, { getFetchType } from "./buildAction"; +import createObserverChannel from "./createObserverChannel"; -export const watchCrudActionsFactory = observeRequest => - function* watchCrudActions(action) { - const { - payload: params, - meta: { fetch: fetchType, resource }, - } = action; - const observer = yield call( - observeRequest, - fetchType, - resource, - params - ); +export const watchCrudActionsFactory = (observeRequest) => + function* watchCrudActions(action) { + const { + payload: params, + meta: { resource } + } = action; - if (!observer) return; + const fetchType = getFetchType(action.type); - const realtimeChannel = yield call(createObserverChannel, observer); + if (!fetchType) return; - try { - while (true) { - // eslint-disable-line - const payload = yield take(realtimeChannel); - const { type, payload: requestPayload, meta } = action; + const observer = yield call(observeRequest, fetchType, resource, params); - yield [ - put({ - type: `${type}_LOADING`, - payload: requestPayload, - meta: omit(meta, 'fetch'), - }), - put({ type: FETCH_START }), - ]; + if (!observer) return; - const raAction = yield call(buildAction, action, payload); + const realtimeChannel = yield call(createObserverChannel, observer); - yield put(raAction); + try { + while (true) { + // eslint-disable-line + const payload = yield take(realtimeChannel); + const { type, payload: requestPayload, meta } = action; - yield put({ type: FETCH_END }); - } - } finally { - if (yield cancelled() && realtimeChannel) { - realtimeChannel.close(); - } - } - }; + yield [ + put({ + type: `${type}_LOADING`, + payload: requestPayload, + meta: omit(meta, "fetch") + }), + put({ type: FETCH_START }) + ]; -export const watchLocationChangeFactory = watchCrudActions => - function* watchLocationChange() { - yield takeLatest([CRUD_GET_LIST, CRUD_GET_ONE], watchCrudActions); - }; + const raAction = yield call(buildAction, action, payload); -export default observeQuery => - function* realtimeSaga() { - const watchCrudActions = watchCrudActionsFactory(observeQuery); - yield takeLatest( - LOCATION_CHANGE, - watchLocationChangeFactory(watchCrudActions) - ); - }; + yield put(raAction); + + yield put({ type: FETCH_END }); + } + } finally { + if (yield cancelled() && realtimeChannel) { + realtimeChannel.close(); + } + } + }; + +export const watchLocationChangeFactory = (watchCrudActions) => + function* watchLocationChange() { + yield takeLatest([CRUD_GET_LIST, CRUD_GET_ONE], watchCrudActions); + }; + +export default (observeQuery) => + function* realtimeSaga() { + const watchCrudActions = watchCrudActionsFactory(observeQuery); + yield takeLatest( + LOCATION_CHANGE, + watchLocationChangeFactory(watchCrudActions) + ); + }; From 30939b0a0225f4eeee092df5f92928f6f0a287ae Mon Sep 17 00:00:00 2001 From: gstvg <28798827+gstvg@users.noreply.github.com> Date: Fri, 18 Sep 2020 10:41:23 -0300 Subject: [PATCH 2/2] Update buildAction.js --- packages/ra-realtime/src/buildAction.js | 33 +++++++++++++++++++------ 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/packages/ra-realtime/src/buildAction.js b/packages/ra-realtime/src/buildAction.js index 4821ae1af27..8613df672a0 100644 --- a/packages/ra-realtime/src/buildAction.js +++ b/packages/ra-realtime/src/buildAction.js @@ -1,11 +1,30 @@ -import { FETCH_END } from 'ra-core'; +import { + CRUD_GET_LIST, + CRUD_GET_ONE, + FETCH_END, + GET_LIST, + GET_ONE +} from "ra-core"; + +export const getFetchType = (actionType) => { + if (actionType === CRUD_GET_LIST) { + return GET_LIST; + } else if (actionType === CRUD_GET_ONE) { + return GET_ONE; + } else { + console.error( + `unexpected action type: ${actionType}, should be only ${CRUD_GET_LIST} or ${CRUD_GET_ONE}` + ); + return undefined; + } +}; export default ( - { type, payload: requestPayload, meta: { fetch: restType, ...meta } }, - payload + { type, payload: requestPayload, meta: { fetch: restType, ...meta } }, + payload ) => ({ - type: `${type}_SUCCESS`, - payload, - requestPayload, - meta: { ...meta, fetchResponse: restType, fetchStatus: FETCH_END }, + type: `${type}_SUCCESS`, + payload, + requestPayload, + meta: { ...meta, fetchResponse: getFetchType(type), fetchStatus: FETCH_END } });