Created
March 9, 2016 20:50
-
-
Save johnlindquist/49b2a49cab6ef0c9e46d 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 {REQUEST_POSTS, RECEIVE_POSTS, INVALIDATE_REDDIT, SELECT_REDDIT, INIT} from '../reducers/reddit'; | |
import {Observable} from 'rxjs/Observable'; | |
import {Reddit} from '../services/reddit'; | |
//surrounding func for deps | |
export const redditPreMiddleware = function (reddit:Reddit) { | |
//return an Obs carrying the type/payload to pass to reducers | |
return (o$:Observable<{type:string, payload:any}>)=> { | |
return o$ | |
.mergeMap(({type, payload})=> { | |
console.log(`Triggering middleware with ${type} and ${payload}`) | |
switch (type) { | |
case REQUEST_POSTS: | |
return o$; | |
case INVALIDATE_REDDIT: | |
return o$; | |
case SELECT_REDDIT: | |
console.log(`case...`); | |
return o$ | |
.switchMap<{url:string, data:any}>( | |
({payload:url})=>reddit.fetchPosts(url), | |
({payload:url}, {data}):{url:string, data:any}=> ({url, data})) | |
.map(({url, data})=> ({ | |
type: RECEIVE_POSTS, | |
payload: { | |
reddit: url, | |
data | |
} | |
})) | |
; | |
default: | |
return o$; | |
} | |
}); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment