Redux Saga - yield spawn takeEvery 调用没有超载
Redux Saga - yield spawn takeEvery call no overload
我使用的是 redux-saga 版本 1.1.3,这里有:
import {
spawn,
race,
take,
call,
put,
takeEvery,
delay,
} from "redux-saga/effects";
import {
MessageCenterActionTypes
} from "../types";
function remove(notification) {
return {
type: MessageCenterActionTypes.MESSAGECENTER_REMOVE,
notification,
};
}
export function* saga() {
yield spawn(
takeEvery,
MessageCenterActionTypes.MESSAGECENTER_ENQUEUE,
removeNotificationAfterElapsedTime,
4000,
);
}
export function* removeNotificationAfterElapsedTime(waitFor, {
notification
}) {
const {
shouldBeRemoved
} = yield race({
shouldBeRemoved: call(delay, waitFor),
_: take(
(action) =>
action.type === MessageCenterActionTypes.MESSAGECENTER_REMOVE &&
action.notification.id === notification.id,
),
});
if (shouldBeRemoved) {
yield put(remove(notification));
}
}
export default saga;
But in yield spawn(takeEvery, MessageCenterActionTypes.MESSAGECENTER_ENQUEUE, removeNotificationAfterElapsedTime, 4000,);
它说:
No overload matches this call.
The last overload gave the following error.
Argument of type '{ <P extends ActionPattern<Action<any>>>(pattern: P, worker: (action: ActionMatchingPattern<P>) => any): ForkEffect<never>; <P extends ActionPattern<...>, Fn extends (...args: any[]) => any>(pattern: P, worker: Fn, ...args: HelperWorkerParameters<...>): ForkEffect<...>; <A extends Action<...>>(pattern: ActionPattern...' is not assignable to parameter of type '{ context: unknown; fn: (this: unknown, ...args: any[]) => any; }'.
Type '{ <P extends ActionPattern<Action<any>>>(pattern: P, worker: (action: ActionMatchingPattern<P>) => any): ForkEffect<never>; <P extends ActionPattern<...>, Fn extends (...args: any[]) => any>(pattern: P, worker: Fn, ...args: HelperWorkerParameters<...>): ForkEffect<...>; <A extends Action<...>>(pattern: ActionPattern...' is missing the following properties from type '{ context: unknown; fn: (this: unknown, ...args: any[]) => any; }': context, fn
通话应该是什么样子?
spawn
函数可以接受单个生成器函数,并且在这个生成器函数内部你可以定义你的 takeEvery
逻辑。
您的其余代码不需要任何更改。
export function* saga() {
yield spawn(function*() {
yield takeEvery(
MessageCenterActionTypes.MESSAGECENTER_ENQUEUE,
removeNotificationAfterElapsedTime,
4000,
);
});
}
我使用的是 redux-saga 版本 1.1.3,这里有:
import {
spawn,
race,
take,
call,
put,
takeEvery,
delay,
} from "redux-saga/effects";
import {
MessageCenterActionTypes
} from "../types";
function remove(notification) {
return {
type: MessageCenterActionTypes.MESSAGECENTER_REMOVE,
notification,
};
}
export function* saga() {
yield spawn(
takeEvery,
MessageCenterActionTypes.MESSAGECENTER_ENQUEUE,
removeNotificationAfterElapsedTime,
4000,
);
}
export function* removeNotificationAfterElapsedTime(waitFor, {
notification
}) {
const {
shouldBeRemoved
} = yield race({
shouldBeRemoved: call(delay, waitFor),
_: take(
(action) =>
action.type === MessageCenterActionTypes.MESSAGECENTER_REMOVE &&
action.notification.id === notification.id,
),
});
if (shouldBeRemoved) {
yield put(remove(notification));
}
}
export default saga;
But in
yield spawn(takeEvery, MessageCenterActionTypes.MESSAGECENTER_ENQUEUE, removeNotificationAfterElapsedTime, 4000,);
它说:
No overload matches this call.
The last overload gave the following error.
Argument of type '{ <P extends ActionPattern<Action<any>>>(pattern: P, worker: (action: ActionMatchingPattern<P>) => any): ForkEffect<never>; <P extends ActionPattern<...>, Fn extends (...args: any[]) => any>(pattern: P, worker: Fn, ...args: HelperWorkerParameters<...>): ForkEffect<...>; <A extends Action<...>>(pattern: ActionPattern...' is not assignable to parameter of type '{ context: unknown; fn: (this: unknown, ...args: any[]) => any; }'.
Type '{ <P extends ActionPattern<Action<any>>>(pattern: P, worker: (action: ActionMatchingPattern<P>) => any): ForkEffect<never>; <P extends ActionPattern<...>, Fn extends (...args: any[]) => any>(pattern: P, worker: Fn, ...args: HelperWorkerParameters<...>): ForkEffect<...>; <A extends Action<...>>(pattern: ActionPattern...' is missing the following properties from type '{ context: unknown; fn: (this: unknown, ...args: any[]) => any; }': context, fn
通话应该是什么样子?
spawn
函数可以接受单个生成器函数,并且在这个生成器函数内部你可以定义你的 takeEvery
逻辑。
您的其余代码不需要任何更改。
export function* saga() {
yield spawn(function*() {
yield takeEvery(
MessageCenterActionTypes.MESSAGECENTER_ENQUEUE,
removeNotificationAfterElapsedTime,
4000,
);
});
}