Skip to content

Instantly share code, notes, and snippets.

@calvinfroedge
Created April 21, 2016 15:43
Show Gist options
  • Save calvinfroedge/2a313ed525ba0c136284075b79640428 to your computer and use it in GitHub Desktop.
Save calvinfroedge/2a313ed525ba0c136284075b79640428 to your computer and use it in GitHub Desktop.
Example notifications middleware with saga
import { takeLatest } from 'redux-saga'
import { EVENT1, EVENT2, EVENTETC } from '../constants'
import { put } from 'redux-saga/effects'
import { addAlert } from '../actions/alerts'
import t from '../../i18n/helper'
function* dispatchAlert(action) {
let message;
try {
message = t('alerts.'+action.type);
yield put(addAlert({message}));
} catch(e){
if(e.name != 'SagaCancellationException') throw(e);
}
}
//Check every action to see if we should dispatch an alert
export default function* alerts() {
yield* takeLatest([
EVENT1,
EVENT2,
EVENTETC
], dispatchAlert);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment