编辑 1
所以我现在已经更新了projectorSaga函数以按照建议使用yield call(),但是现在每次调用该函数时都会出现以下错误,我认为这是由于我嵌套函数和返回promise的方式造成的不是返回 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,并且在我的一个生成器函数中使用时遇到问题。我怀疑是我不理解 say 函数和回调等的嵌套,但到目前为止我已经尝试了很多方法,没有区别。
问题的奇怪之处在于 sendCommand 中的初始 yield 确实导致 Redux 中出现事件。此外,我现有的日志命令确实会在我期望的时候被调用,并且具有正确的值。如果连接超时,则调用 catch get 中的日志,但两个 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),
]);
}
这就是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);
}
这是传奇正在调用的函数的其余代码,因为它可能是相关的,我可能完全误解了异步函数/ promise 等需要返回的方式。
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);
}
});
};
最佳答案
我认为您正在屈服
(暂停)生成器函数而不调用 next 。 Saga 有这方面的功能。例如,您不是在函数调用上屈服,而是在 saga 函数 call
上屈服。
所以应该是:
try {
let response;
const command = payload.payload.command;
if (command === TURNON) {
response = yield call([pjControl,'turnOn'],ip, port, model);
}
...
关于javascript - 无法让 Redux 在生成器函数中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48580846/