Add notion of top level `unit` that creates stream

of application changes itself.
This commit is contained in:
Irakli Gozalishvili 2012-10-28 21:08:30 -07:00
Родитель fdcc172bd3
Коммит cace2bae08
1 изменённых файлов: 14 добавлений и 3 удалений

17
unit.js
Просмотреть файл

@ -1,5 +1,7 @@
"use strict";
var channel = require("reducers/channel")
var pipe = require("reducers/pipe")
var filter = require("reducers/filter")
var map = require("reducers/map")
var flatten = require("reducers/flatten")
@ -27,13 +29,20 @@ function unit(mapping) {
count: component(null, itemCountWriter)
})
**/
return function reactor(changes) {
return function reactor(source) {
/**
Function takes stream of state changes for the unit it's responsible
and distributes change across nested components / units. As a return value
it returns joined steam of changes caused by interactions on all nested
components in same structure as stream of changes it reads from.
If `
**/
// If `source` stream of state changes is not provided this means this
// is reactor for top level unit. So all the change events generated by
// it's entities will be all the application state changes, there for
// channel for that changes is created.
var changes = source || channel()
var inputs = keys(mapping).map(function forkChanges(id) {
var reactor = mapping[id]
// Filter stream of changes to a stream of changes to the entity
@ -49,8 +58,10 @@ function unit(mapping) {
})
// Return joined stream of all inputs from all the entities of this
// unit.
return flatten(inputs)
// unit. Also if it's top level unit (`source` was not passed) pipe
// state changes back to the application state changes as all the changes
// are caused by it's entities.
return source ? flatten(inputs) : pipe(flatten(inputs), changes), changes
}
}