Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch ra-realtime for 3.x #5272

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions packages/ra-realtime/src/buildAction.js
Original file line number Diff line number Diff line change
@@ -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 }
});
110 changes: 55 additions & 55 deletions packages/ra-realtime/src/index.js
Original file line number Diff line number Diff line change
@@ -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)
);
};