I'm a newbie to the Bacon.js, usually write programs in Haskell. By my experience with Haskell, I want to describe some situations in Bacon.js as purely-functional-like approach.
Here is an example situation.
triggerStream
is a source stream.resultStream
tries ajax access whentriggerStream
's events occur.resultStream2
also tries ajax access after completion ofresultStream
's ajax access.
This is my approach:
### =======
# Streams
# ======= ###
triggerStream = () ->
Bacon.fromArray([1,2,3])
resultStream =
triggerStream()
.flatMap((n) -> Bacon.fromPromise($.ajax(toAjax n)))
.zip(triggerStream(), (r,t) ->
{result: r, trigger: t}
resultStream2 =
resultStream # (*)
.flatMap((o) -> Bacon.fromPromise($.ajax(toAjax2 o)))
.zip(resultStream, (r2,r1) ->
{result: r2, trigger: r1.trigger}
### =======
# Assignments
# ======= ###
triggerStream()
.onValue(beforeAjax1) # (a)
resultStream
.onValue(afterAjax1) # (b)
resultStream2
.onValue(afterAjax2) # (c)
(a) is supporsed to be executed after each trigger event, in other words it's executed before resultStream
's ajax access.
(b) is supporsed to be fired after resultStream
's ajax access.
(c) is supporsed to be start after resultStream2
's ajax access.
I know Bacon.js's streams or properties have side effects on themselves, and so my code can't work well.
In (b), resultStream
's events are removed from resultStream
object, which causes the empty stream in (*).
The approach which changes resultStream
to a function (like triggerStream
) seams work well, but it causes independent two times resultStream
's ajax access when (b) and (c).
Is there any idea to realize my approach?