此代码位于redux saga文件中。
我正在尝试将响应发送回saga函数,但我做不到。
new GraphRequestManager().addRequest(infoRequest).start();
infoRequest = new GraphRequest(
'/me', {
httpMethod: 'GET',
version: 'v2.5',
parameters: {
'fields': {
'string': 'email,first_name,last_name,id'
}
}
}, startFacebookRegister);
facebookRegister函数
function * startFacebookRegister(err, res) {
const { id, email, first_name, last_name } = res;
yield put(initFacebookLogin.success(res));
}
答案 0 :(得分:0)
如果我们看GraphRequestManager.start
。
const bar = { x: 'hello', y: { 'world': (a: string, b: number) => ({ type: a, payload: b}) } } const _ensureA: A = bar; // Type test
您传递的回调以普通的 const that = this;
const callback = (error, result, response) => {
if (response) {
that.requestCallbacks.forEach((innerCallback, index, array) => {
if (innerCallback) {
// here it is called as a normal callback, not a generator
innerCallback(response[index][0], response[index][1]);
}
});
}
if (that.batchCallback) {
that.batchCallback(error, result);
}
};
NativeGraphRequestManager.start(this.requestBatch, timeout || 0, callback); } ```
而不是Function
的形式调用。
由于它们的执行方式不同,这就是为什么什么都不会发生的原因,因为您的GeneratorFunction
在被调用时只是实例化并坐在那里,直到收集垃圾为止。
因此,解决此问题的首选方法是与GeneratorFunction
流程对齐,使用eventChannel
,它将流程保留在redux-saga
内,实际上是advised for similar cases。 / p>
另一种方法(不是那么可取)是只调用原始redux-saga
,它显然突破了store.dispatch
的抽象级别和流程,但是仍然可以通过调用其他方法来完成工作redux-saga
操作可以处理saga
与call
的{{1}}。
我们需要将整个startFacebookRegister
块包装到response
中,以便我们可以将GraphRequestManager...
或eventChannel
从供应商控件重定向到Saga流中。
可以是这样的:
response
在商店中,您可以将error
或import {
cancelled,
call,
take,
put
} from "redux-saga/effects";
import {
eventChannel,
END
} from 'redux-saga';
function* startFacebookRegister(err, res) {
const {
id,
email,
first_name,
last_name
} = res;
yield put(initFacebookLogin.success(res));
}
function* graphRequestWrapper() {
return eventChannel(emit => {
const infoRequest = new GraphRequest(
'/me', {
httpMethod: 'GET',
version: 'v2.5',
parameters: {
'fields': {
'string': 'email,first_name,last_name,id'
}
}
}, (err, res) => {
if (err) {
emit(new Error(err));
} else {
emit(res);
}
emit(END);
});
// BTW infoRequest variable should be instantiated before we
// can add it with addRequest, just a side note
new GraphRequestManager().addRequest(infoRequest).start();
return () => {
// clean up
};
})
}
export function* mainSaga() {
const chan = yield call(graphRequestWrapper);
while (true) {
try {
const res = yield take(chan);
yield call(startFacebookRegister, null, res);
} catch (err) {
yield call(startFacebookRegister, err);
} finally() {
if (yield cancelled()) chan.close()
}
}
}
与所有的reducer和中间件一起从store
导出main.js
,并在这里直接使用。
index.js