import { takeEvery, take, call, put, race } from "redux-saga/effects";
import { graphQuery } from "./actions";
function* sendRequest(req, id, params) {
    try {
        const response = yield call(req, ...params);
        yield put(graphQuery.success({ id, response }));
    }
    catch (error) {
        yield put(graphQuery.failure({ id, error }));
    }
}
function* handleRequest(orm, action) {
    const { id, entity, method, args } = action.payload;
    const params = Array.isArray(args) ? args : [args];
    yield race({
        result: yield call(sendRequest, orm[entity][method], id, params),
        cancel: yield take(graphQuery.cancel)
    });
}
export function* handleGraphRequests(orm) {
    if (!orm) {
        return;
    }
    yield takeEvery(graphQuery.request, handleRequest, orm);
}
