在特定日期时间调度操作
Dispatch action on a specific datetime
我目前正在编写一个React应用程序,我需要在我的Redux状态下监听下一个日历条目。
我正在寻求关于如何最有效和正确地做到这一点的建议。
我的calendar
状态减速器包括:
entries: [
{
title: "Event 1",
start: "2016-09-26T08:00:00.000Z"
end: "2016-09-26T09:00:00.000Z"
},
{
title: "Event 2",
start: "2016-09-26T10:00:00.000Z"
end: "2016-09-26T11:00:00.000Z"
},
{
title: "Event 3",
start: "2016-09-26T13:00:00.000Z"
end: "2016-09-26T14:00:00.000Z"
}
]
当下一个事件(事件1)将要发生时,我想要调度一个事件来处理这个日历条目的状态。条目减速器可以随时更新,所以我需要能够比下一个条目更早地推送条目。
我有Redux和Redux Saga可以处理这个问题。
目前我正在使用一个Redux Saga监听器,如:
export default function * watchCalendar() {
while (true) {
const entry = yield select((state) => state.calendar.entries[0]);
if (entry) {
const now = moment().startOf("minute");
const start = moment(entry.start);
if (now.isAfter(start)) {
put(CalendarActions.setActiveEntry(entry));
}
}
}
}
但没有像预期的那样工作,因为while
在第一次尝试后退出。我得让它继续监听国家的声音。以上并没有我想要的那么有效。
欢迎任何建议、想法或代码示例。
我还在努力,更近了一点:
export function * watchNextCalendarEntry() {
while (true) { // eslint-disable-line no-constant-condition
const next = yield select((state) => CalendarSelectors.getNextEntry(state.calendar));
if (next) {
const start = moment(next.start);
const seconds = yield call(timeleft, start);
yield call(delay, seconds * 1000);
yield put(CalendarActions.setActiveCalendarEntry(next));
}
}
}
function * currentCalendarEntry(action) {
try {
while (true) { // eslint-disable-line no-constant-condition
const entry = action.payload;
const end = moment(entry.end);
const seconds = yield call(timeleft, end);
yield call(delay, seconds * 1000);
yield put(CalendarActions.setInactiveCalendarEntry(entry));
}
}
finally {
if (yield cancelled()) {
// e.g. do something
}
}
}
export function * watchCurrentCalendarEntry() {
while (true) { // eslint-disable-line no-constant-condition
const action = yield take(ActionTypes.SET_ACTIVE_CALENDAR_ENTRY);
const watcher = yield fork(currentCalendarEntry, action);
yield take(ActionTypes.SET_INACTIVE_CALENDAR_ENTRY);
yield cancel(watcher);
}
}
function getTimeLeft(date) {
return date.diff(moment().startOf("second"), "seconds");
}
像这样?
export default function* watchNextCalendarEntry() {
takeLatest(SUBSCRIBE_CALENDAR_ENTRY, subscribeCalendarEntry);
}
function* subscribeCalendarEntry({ nextEntry }) {
const timeLeft = moment(nextEntry.start).diff(moment());
yield call(delay, timeLeft);
yield put(CalendarActions.setActiveCalendarEntry(nextEntry));
}
你需要在应用程序开始时调度{ type: SUBSCRIBE_CALENDAR_ENTRY, nextEntry }
动作,当条目改变时,计算并将nextEntry传递给动作。
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- Redux-正确调度操作
- reactJs redux:如何对用户事件调度操作并将操作与化简器和存储链接
- 我可以在没有 Redux Thunk 中间件的情况下调度多个操作吗?
- 如何从不知道 Redux 的服务调度操作
- 在 redux 中调度不同的操作
- 具有新状态的 Redux 调度操作和化简器
- 如果正在调度操作,是否会调用每个 Redux 缩减器
- 反应/Redux 应用程序操作未正确调度
- 我如何从我的API模块调度一个redux操作?
- 在Redux不工作的情况下调度操作,得到未捕获的typeError,说它不是一个函数
- 调度操作创建器语法react-redux
- 如何在redux-thunk中断言在异步操作内部调度的异步操作
- 在特定日期时间调度操作
- push()即使我推送相同的url也会调度LOCATION_CHANGE操作
- 如何在使用Redux进行异步操作调度后检索状态数据
- 如何从存储本身获取在存储上调度的最后一个操作类型
- React-Router:为什么 组件的 onClick 处理程序不会调度我的操作
- Redux thunk:从调度操作中返回承诺
- Redux:根据交叉化简器状态调度操作