-
-
Save winsonwq/111d31bb3e7a1c6137b2 to your computer and use it in GitHub Desktop.
This file contains 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 { 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