2
我目前正在編寫一個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傳奇處理這個。
目前我正與一個終極版佐賀監聽工作,如:
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
之後的第一次嘗試退出。我需要讓它不斷傾聽國家的聲音。以上並不像我想要的那樣有效。
歡迎任何建議,想法或代碼示例。
更新1,2,3,4
我還是盜號了一下,有點接近:
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");
}
訂閱下一個條目的好主意 - 所以如果另一個條目進來,我們數數它。只需要確保「下一個」條目也是及時的下一個條目。 – janhartmann
是的,所以nextEntry需要成爲下一個條目。如果這是有道理的,你可以在傳奇中計算,但是必須使用「選擇」效果 – yjcxy12