Created
October 23, 2017 19:46
-
-
Save acr13/9987cb7b9409e76c737a57df0e0f948f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { delay } from 'redux-saga'; | |
| import { call, put, select, take } from 'redux-saga/effects'; | |
| import { getFlashQueue } from './getters'; | |
| import { ADD_FLASH_MESSAGE, SHOW_FLASH, CLEAR_FLASH } from './actions'; | |
| const FIVE_SECONDS = 5000; | |
| export function* showFlashMessage() { | |
| yield put({ type: SHOW_FLASH }); | |
| yield call(delay, FIVE_SECONDS); // wait 5 secs | |
| yield put({ type: CLEAR_FLASH }); | |
| } | |
| export function* onAddFlashMessage() { | |
| while (true) { | |
| const flashQueue = yield select(getFlashQueue); | |
| // empty whats in the queue first | |
| if (flashQueue.length > 0) { | |
| while (true) { | |
| const queue = yield select(getFlashQueue); | |
| if (queue.length === 0) { | |
| break; | |
| } | |
| yield showFlashMessage(); | |
| } | |
| } | |
| // wait until we get a new ADD_FLASH_MESSAGE action | |
| yield take(ADD_FLASH_MESSAGE); | |
| yield showFlashMessage(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment