gpt4 book ai didi

javascript - 无法让 Redux 在生成器函数中运行

转载 作者:行者123 更新时间:2023-11-29 18:57:41 29 4
gpt4 key购买 nike

编辑 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

这里是update saga的代码,其他功能依旧如下。

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 中的日志,但 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);
}

这是 saga 正在调用的函数的其余代码,因为它可能是相关的,我可能完全误解了需要返回异步函数/promises 等的方式。

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);
}
});
};

最佳答案

我认为您正在yielding(暂停)生成器函数而不调用 next。 Saga 有这方面的功能。例如,不是在函数调用时产生,而是在 saga 函数 call 上产生。

所以应该是:

 try {
let response;
const command = payload.payload.command;
if (command === TURNON) {
response = yield call([pjControl,'turnOn'],ip, port, model);
}
...

请参阅https://redux-saga.js.org/docs/api/

关于javascript - 无法让 Redux 在生成器函数中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48580846/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com