编辑1
所以我现在更新了projectorSaga函数以使用yield call(),但我现在每次调用函数时都会收到以下错误,我认为这是由于我嵌套函数和返回promise的方式不是返回承诺的函数,但我对如何重组它来修复它有点不知所措。
redux-saga error: uncaught at check
call: argument [object Promise] is not a function
[14:57:43.351] [verbose] Error: call: argument [object Promise] is not a function
这里是更新传奇代码,其他功能仍然如下。
import { put, takeEvery, call } from 'redux-saga/effects';
import * as pjControl from '../../pjcontrol/pjControl';
import { TURNON, TURNOFF, SENDCOMMAND, SHUTTERCLOSE, SHUTTEROPEN } from '../actions/actionTypes';
const log = require('electron-log');
const ip = '192.168.100.100';
const port = 3629;
const model = 'L1500';
function* sendCommand(payload) {
yield put({ type: 'SENDING COMMAND', payload });
try {
let response;
const command = payload.payload.command;
if (command === TURNON) {
response = yield call(pjControl.turnOn(ip, port, model));
} else if (command === TURNOFF) {
response = yield call(pjControl.turnOff(ip, port, model));
} else if (command === SHUTTERCLOSE) {
response = yield call(pjControl.shutterClose(ip, port, model));
} else if (command === SHUTTEROPEN) {
response = yield call(pjControl.shutterOpen(ip, port, model));
} else {
throw Error('Unknwon Command');
}
log.verbose(response);
yield put({ type: 'SEND_COMMAND_SUCCEEDED', response });
} catch (e) {
log.verbose(e);
yield put({ type: 'SEND_COMMAND_FAILED', e });
}
}
export default function* projectorSetSaga() {
yield takeEvery(SENDCOMMAND, sendCommand);
}
原始
我刚刚开始尝试在我正在使用的Electron应用程序中使用Redux-Saga,而且我在使用其中一个生成器函数时遇到问题。我怀疑我不理解所说的函数和回调e.t.c的嵌套,但到目前为止我已经在很多方面试过这个并没有区别。
问题的奇怪之处在于sendCommand中的初始yield会导致事件出现在Redux中。我所使用的日志命令也会在我期望它们时被调用,并且具有正确的值。如果连接超时,则会调用catch中的日志,但是两个throw命令都不会出现在Redux中。
这是我的根佐贺:
import { all, call } from 'redux-saga/effects';
import projectorGetSaga from './projectorGetSaga';
import projectorSetSaga from './projectorSetSaga';
export default function* rootSaga() {
yield all([
call(projectorGetSaga),
call(projectorSetSaga),
]);
}
这是投影机设备,我正在努力奋斗。
import { put, takeEvery } from 'redux-saga/effects';
import * as pjControl from '../../pjcontrol/pjControl';
import { TURNON, TURNOFF, SENDCOMMAND, SHUTTERCLOSE, SHUTTEROPEN } from '../actions/actionTypes';
const log = require('electron-log');
const ip = '192.168.100.100';
const port = 3629;
const model = 'L1500';
function* sendCommand(payload) {
yield put({ type: 'SENDING COMMAND', payload });
try {
let response;
const command = payload.payload.command;
if (command === TURNON) {
response = yield pjControl.turnOn(ip, port, model);
} else if (command === TURNOFF) {
response = yield pjControl.turnOff(ip, port, model);
} else if (command === SHUTTERCLOSE) {
response = yield pjControl.shutterClose(ip, port, model);
} else if (command === SHUTTEROPEN) {
response = yield pjControl.shutterOpen(ip, port, model);
} else {
throw Error('Unknwon Command');
}
log.verbose(response);
yield put({ type: 'SEND_COMMAND_SUCCEEDED', response });
} catch (e) {
log.verbose(e);
yield put({ type: 'SEND_COMMAND_FAILED', e });
}
}
export default function* projectorSetSaga() {
yield takeEvery(SENDCOMMAND, sendCommand);
}
以下是saga调用的函数的其余代码,因为它可能是相关的,我可能完全误解了async函数/ promises e.t.c需要返回的方式。
sendCommand函数调用的PJ Control函数如下所示。
import * as epsonControl from './epson/epsonControl';
import { manufacturerlLookup, commandLookup } from './pjLookups';
function sendCommand(ip, port, model, command, input) {
const manufacturer = manufacturerlLookup(model);
const mappedCommand = commandLookup(manufacturer, command, input);
switch (manufacturer) {
case 'epson':
return epsonControl.sendCommand(ip, port, mappedCommand);
default:
return null;
}
}
exports.turnOn = (ip, port, model) => sendCommand(ip, port, model, 'PWR ON');
exports.turnOff = (ip, port, model) => sendCommand(ip, port, model, 'PWR OFF');
exports.shutterOpen = (ip, port, model) => sendCommand(ip, port, model, 'MUTE OFF');
exports.shutterClose = (ip, port, model) => sendCommand(ip, port, model, 'MUTE ON');
exports.setInput = (ip, port, model, input) => sendCommand(ip, port, model, 'SOURCE', input);
exports.getPowerStatus = (ip, port, model) => sendCommand(ip, port, model, 'PWR?');
exports.getShutterStatus = (ip, port, model) => sendCommand(ip, port, model, 'MUTE?');
exports.getInputStatus = (ip, port, model) => sendCommand(ip, port, model, 'SOURCE?');
最后,实际的异步函数,实际上是返回一个带有return / throw的promise。
exports.sendCommand = async (host, port, command) => {
log.info(`sending command ${command} to ${host}`);
// throw new Error('Oh dear!');
// Set a time to cancel the operation if the connection times out
const mergedCommand = `${command}\r`;
// Create a new client
const socket = new net.Socket();
socket.setTimeout(1000);
const client = new PromiseSocket(socket);
await client.connect({ host, port });
await client.write(buildBuffer(handshake));
client.stream.on('data', (data) => {
if (data.toString() === response) {
log.verbose(`Got handshake, sending command ${command}`);
client.write(buildBuffer(mergedCommand));
} else if (data.toString() === ':') {
// log.verbose('command executed successfully, disconnecting');
sendStatus(data.toString(), host);
client.end();
return (true, 'command');
} else {
const respose = data.toString().substring(0, data.toString().length - 2);
// log.verbose(`got status ${response} disconnecting`);
sendStatus(respose, host);
client.end();
return (true, 'command', data);
}
});
};
答案 0 :(得分:3)
我认为你是yielding
(暂停)生成器函数而不调用它的下一个。佐贺有这方面的功能。例如,不是在函数调用上产生,而是在saga函数call
上产生。
所以它应该是:
try {
let response;
const command = payload.payload.command;
if (command === TURNON) {
response = yield call([pjControl,'turnOn'],ip, port, model);
}
...