Skip to content

Instantly share code, notes, and snippets.

@winsonwq
Forked from morlay/ListStore.js
Last active October 13, 2017 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winsonwq/111d31bb3e7a1c6137b2 to your computer and use it in GitHub Desktop.
Save winsonwq/111d31bb3e7a1c6137b2 to your computer and use it in GitHub Desktop.
import { createStore } from 'reflux'
// if you don't mind
import { pipe, of } from 'ramda'
import GalleryActions from '../actions/GalleryActions'
export default createStore({
init(){
this.galleryMap = {}
this.listenTo(GalleryActions.listGalleries.completed, this.reduce)
this.listenTo(GalleryActions.getGallery.completed, pipe(of, this.reduce))
},
// most of Stores in Flux architecture just follow a reduce transform
reduce(galleryList) {
// TODO: try to avoid mutation, but here is fine
// reduce could be replaced by merge or extend but also think about immutation
this.galleryMap = galleryList.reduce(function(sofar, curr) {
if (curr.id) {
sofar[curr.id] = curr
}
return sofar
}, this.galleryMap)
// trigger after mutation immediately
this.trigger()
},
getGalleryById(galleryId){
return this.galleryMap[galleryId]
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment