Skip to content

Commit

Permalink
s/streams/sources
Browse files Browse the repository at this point in the history
rename (`pull-notify` `.listen`) `streams` returned by `start` to `sources`.

also rename `effectActionStreams` to `effectActionsSources`.
  • Loading branch information
ahdinosaur committed Jun 23, 2016
1 parent f339622 commit 4faecd8
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ streams is an object with the following keys:
- `models`: a function that returns a [pull source stream](https://pull-stream.github.io) for models
- `views`: a function that returns a [pull source stream](https://pull-stream.github.io) for views
- `effects`: a function that returns a [pull source stream](https://pull-stream.github.io) for effects
- `effectActionStreams`: a function that returns a [pull source stream](https://pull-stream.github.io) for any streams of next actions caused by effects
- `effectActionsSources`: a function that returns a [pull source stream](https://pull-stream.github.io) for any streams of next actions caused by effects

![streams flow diagram](https://rawgit.com/ahdinosaur/inu/master/assets/flow-diagram.dot.svg)

Expand Down
4 changes: 2 additions & 2 deletions assets/flow-diagram.dot
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ digraph app {
states -> effects
models -> views [label="view()"]
views -> actions [label="dispatch()"]
effects -> effectActionStreams [label="run()"]
effectActionStreams -> actions
effects -> effectActionsSources [label="run()"]
effectActionsSources -> actions
}
42 changes: 20 additions & 22 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var notify = require('pull-notify')
module.exports = start

/*
┌────── effectActionStreams ◀───────┐
┌────── effectActionsSources ◀───────┐
▼ |
actions ─▶ states ─▶ effects ─────────┘
▲ |
Expand Down Expand Up @@ -68,47 +68,45 @@ function start (app) {
pull.drain(effects)
)

var effectActionStreams = notify()
var effectActionsSources = notify()

var streams = {
actions: actions.listen,
states: states.listen,
models: models.listen,
views: views.listen,
effects: effects.listen,
effectActionStreams: effectActionStreams.listen
var notifys = {
actions: actions,
states: states,
models: models,
views: views,
effects: effects,
effectActionsSources: effectActionsSources
}

var sources = {}
Object.keys(notifys).forEach(function (name) {
sources[name] = notifys[name].listen
})

pull(
effects.listen(),
pull.map(function (effect) {
return run.call(app, effect, streams)
return run.call(app, effect, sources)
}),
pull.filter(isNotNil),
pull.drain(effectActionStreams)
pull.drain(effectActionsSources)
)

pull(
effectActionStreams.listen(),
effectActionsSources.listen(),
drainMany(actions)
)

process.nextTick(function () {
states(initialState)
})

return Object.assign({}, streams, { stop: stop })
return Object.assign({}, sources, { stop: stop })

function stop () {
;[
actions,
states,
models,
views,
effects,
effectActionStreams
].forEach(function (stream) {
stream.end()
Object.keys(notifys).forEach(function (name) {
notifys[name].end()
})
}
}
Expand Down
16 changes: 8 additions & 8 deletions test/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('defaultUpdate', function (t) {
model: expectedModel,
effect: 'INITIALIZE'
}
var streams = inu.start({
var sources = inu.start({
init: function () { return initialState },
run: function (effect) {
t.equal(effect, initialState.effect, 'effect received')
Expand All @@ -35,7 +35,7 @@ test('defaultUpdate', function (t) {
}
})
pull(
streams.models(),
sources.models(),
pull.drain(function (model) {
t.equal(model, expectedModel, 'initial model is expected')
process.nextTick(function () {
Expand All @@ -47,9 +47,9 @@ test('defaultUpdate', function (t) {
})

test('defaultView', function (t) {
var streams = inu.start({})
var sources = inu.start({})
pull(
streams.views(),
sources.views(),
pull.drain(function (view) {
t.notOk(true, 'did not expect to receive default empty view')
})
Expand All @@ -70,14 +70,14 @@ test('defaultRun', function (t) {
model: true,
effect: 'INITIALIZE'
}
var streams = inu.start({
var sources = inu.start({
init: function () { return initialState },
view: function (model, dispatch) {
expectedActions.forEach(dispatch)
}
})
pull(
streams.actions(),
sources.actions(),
pull.take(3),
pull.collect(function (err, actions) {
t.error(err)
Expand All @@ -88,7 +88,7 @@ test('defaultRun', function (t) {
})

test('defaultApp', function (t) {
var streams = inu.start()
t.ok(streams, 'undefined app has streams')
var sources = inu.start()
t.ok(sources, 'undefined app has sources')
t.end()
})
4 changes: 2 additions & 2 deletions test/dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ test('Calling dispatch emits actions on the action stream.', function (t) {
return inu.html`<div></div>`
}
}
var streams = inu.start(app)
pull(streams.actions(), pull.take(1), pull.drain(function (action) {
var sources = inu.start(app)
pull(sources.actions(), pull.take(1), pull.drain(function (action) {
t.equal(action, expectedAction)
t.end()
}))
Expand Down
4 changes: 2 additions & 2 deletions test/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ test('returning an effect in init emits the effect on the effects stream', funct
},
run: function () {}
}
var streams = inu.start(app)
pull(streams.effects(), pull.take(1), pull.drain(function (effect) {
var sources = inu.start(app)
pull(sources.effects(), pull.take(1), pull.drain(function (effect) {
t.equal(effect, expectedEffect)
t.end()
}))
Expand Down
8 changes: 4 additions & 4 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ test('returning an action from effect emits actions on the action stream.', func
return pull.values([expectedAction])
}
}
var streams = inu.start(app)
pull(streams.actions(), pull.take(1), pull.drain(function (action) {
var sources = inu.start(app)
pull(sources.actions(), pull.take(1), pull.drain(function (action) {
t.equal(action, expectedAction)
t.end()
}))
Expand All @@ -123,8 +123,8 @@ test('actions stream passed to run emits actions', function (t) {
view: function (model, dispatch) {
return inu.html`<div></div>`
},
run: function (effect, streams) {
const actions = streams.actions
run: function (effect, sources) {
const actions = sources.actions
pull(actions(), pull.take(1), pull.drain(function (action) {
t.equal(action, expectedAction)
t.end()
Expand Down
30 changes: 15 additions & 15 deletions test/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ test('calling stop() ends actions stream', function (t) {
return inu.html`<div></div>`
}
}
var streams = inu.start(app)
pull(streams.actions(), pull.collect(function (err, actions) {
var sources = inu.start(app)
pull(sources.actions(), pull.collect(function (err, actions) {
t.error(err)
t.equal(actions.length, 0, 'Actions stream ends with no emitted actions')
t.end()
}))
streams.stop()
sources.stop()
})

test('calling stop() ends effects stream', function (t) {
Expand All @@ -42,13 +42,13 @@ test('calling stop() ends effects stream', function (t) {
return inu.html`<div></div>`
}
}
var streams = inu.start(app)
pull(streams.effects(), pull.collect(function (err, effects) {
var sources = inu.start(app)
pull(sources.effects(), pull.collect(function (err, effects) {
t.error(err)
t.equal(effects.length, 0, 'Effects stream ends with no emitted actions')
t.end()
}))
streams.stop()
sources.stop()
})

test('calling stop() ends views stream', function (t) {
Expand All @@ -66,13 +66,13 @@ test('calling stop() ends views stream', function (t) {
return inu.html`<div></div>`
}
}
var streams = inu.start(app)
pull(streams.views(), pull.collect(function (err, views) {
var sources = inu.start(app)
pull(sources.views(), pull.collect(function (err, views) {
t.error(err)
t.equal(views.length, 0, 'Views stream ends with no emitted actions')
t.end()
}))
streams.stop()
sources.stop()
})

test('calling stop() ends models stream', function (t) {
Expand All @@ -90,13 +90,13 @@ test('calling stop() ends models stream', function (t) {
return inu.html`<div></div>`
}
}
var streams = inu.start(app)
pull(streams.models(), pull.collect(function (err, models) {
var sources = inu.start(app)
pull(sources.models(), pull.collect(function (err, models) {
t.error(err)
t.equal(models.length, 0, 'Models stream ends with no emitted actions')
t.end()
}))
streams.stop()
sources.stop()
})
test('calling stop() ends effectAction stream', function (t) {
var initialModel = {initial: true}
Expand All @@ -113,11 +113,11 @@ test('calling stop() ends effectAction stream', function (t) {
return inu.html`<div></div>`
}
}
var streams = inu.start(app)
pull(streams.effectActionStreams(), pull.collect(function (err, effectActions) {
var sources = inu.start(app)
pull(sources.effectActionsSources(), pull.collect(function (err, effectActions) {
t.error(err)
t.equal(effectActions.length, 0, 'EffectActions stream ends with no emitted actions')
t.end()
}))
streams.stop()
sources.stop()
})
4 changes: 2 additions & 2 deletions test/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ test('models returned by update are emitted by the model stream', function (t) {
return inu.html`<div></div>`
}
}
var streams = inu.start(app)
var sources = inu.start(app)

pull(streams.models(), pull.take(2), pull.collect(function (err, models) {
pull(sources.models(), pull.take(2), pull.collect(function (err, models) {
t.error(err)
t.equal(models[0], initialModel)
t.equal(models[1], expectedModel)
Expand Down
4 changes: 2 additions & 2 deletions test/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ test('newly created app renders initial state', function (t) {
return inu.html`<div></div>`
}
}
var streams = inu.start(app)
var sources = inu.start(app)
pull(
streams.views(),
sources.views(),
pull.drain(function (view) {
t.ok(view)
t.end()
Expand Down

0 comments on commit 4faecd8

Please sign in to comment.