Skip to content

Instantly share code, notes, and snippets.

@Raynos Raynos/accumulate.js Secret
Created Mar 25, 2013

Embed
What would you like to do?
var foldp = require("signal/foldp")
var merge = require("signal/merge")
// accumulate :: [Signal<Function(S -> S)>] -> S -> Signal<S>
module.exports = accumulate
// accumulate take a list of signals of functions that take previous
// state and returns some new state and also takes the initial
// state. It will return a signal of the current state as applied
// by those functions
function accumulate(args, initial) {
return foldp(merge(args), function (state, fn) {
return fn(state)
}, initial)
}
var addMember = map(memberEvents, function (event) {
return function (state) {
var members = extend(state.members)
var value = event.value
var widget = Member(value, input)
membersHash[value.id] = value
members[value.id] = {
value: value
, elem: widget.view
}
return {
members: members
, currentWidget: widget
}
}
})
var updateMember = map(modifyEvents, function (event) {
return function (state) {
var value = event.value
var members = extend(state.members)
var orig = members[value.id]
if (!orig) {
return { members: members }
}
members[value.id] = {
value: extend(orig.value, value)
, elem: orig.elem
}
return { members: members }
}
})
var state = accumulate([ addMember, updateMember ], {
members: {}
, currentWidget: null
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.