2016-09-26 36 views
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"); 
} 

回答

0

像這樣的事情?

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

您需要調度的應用程序的啓動和然後時候進入改變計算和nextEntry傳入到動作{ type: SUBSCRIBE_CALENDAR_ENTRY, nextEntry }行動。

+0

訂閱下一個條目的好主意 - 所以如果另一個條目進來,我們數數它。只需要確保「下一個」條目也是及時的下一個條目。 – janhartmann

+0

是的,所以nextEntry需要成爲下一個條目。如果這是有道理的,你可以在傳奇中計算,但是必須使用「選擇」效果 – yjcxy12

相關問題