Skip to content

Instantly share code, notes, and snippets.

@Willmo36
Last active August 29, 2015 14:16
Show Gist options
  • Save Willmo36/011788a1d0eef2467f26 to your computer and use it in GitHub Desktop.
Save Willmo36/011788a1d0eef2467f26 to your computer and use it in GitHub Desktop.
Kefir flux store example
let Kefir = require("kefir");
function BlogStore(actions, initialState) {
//map the action stream to a stream which returns a function which modifies the state.
let createPost = actions.Post.create.map((newPost) => {
//the scan method will call this with the current vaule (allPosts)
return (allPosts) => {
allPosts.push(newPost);
return allPosts;
}
});
//alt way of writing.
//let createPost = actions.Posts.create.map((newPost) => (allPosts) => {
// allPosts.push(newPost);
// return allPosts;
//});
let posts = Kefir
.merge([createPost])
.scan((allPosts, mod) => mod(allPosts), initialState)
.toProperty();
return {
posts
}
}
module.exports = BlogStore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment