Olli Mahlamäki / Hubchat
var messages = [
{ from: "olli", to: "pekka", text: "Hi Pekka" },
{ from: "pekka", to: "olli", text: "What's up?" },
{ from: "olli", to: "pekka", text: "Doing a tech an beer!" },
{ from: "pekka", to: "olli", text: "Ok cool" },
]
messages
.filter(function(message) { return isDirectedToMe(message) })
.map(function(message) { return message.text })
.forEach(show)
var messages = [
{ from: "olli", to: "pekka", text: "Hi Pekka" },
{ from: "pekka", to: "olli", text: "What's up?" },
{ from: "olli", to: "pekka", text: "Doing a tech an beer!" },
{ from: "pekka", to: "olli", text: "Ok cool" },
]
// Simulate messages coming from server
var messagesE = Bacon.sequentially(1000, messages)
messagesE
.filter(function(message) { return isDirectedToMe(message) })
.map(function(message) { return message.text })
.onValue(show)
var mappedStream = stream.map(f)
var filteredStream = stream.filter(f)
Merges events from both streams a and b into the result stream.
Result stream ends when both streams have ended.
myStream.onValue(function(value) {
// Called every time myStream gets a value
$("#result").text(value)
})
// Standard jquery
$("#myButton").on("click", handlerFunction)
// Bacon extension
var clickStream = $("#myButton").asEventStream("click")
clickStream.onValue(handlerFunction)
Combines the latest values of property a and stream or property b using a given function. In this example, a self-explanatory plus function is used.
First output event is generated when both a and b have produced a value. The combined stream ends when both the inputs have ended or one has ended without having produced a value.
Creates a new stream for each value in the source stream, using the given function f. Merges the events from all created streams into the result stream.
Result stream ends when both the source stream and all the so-far created streams have ended.
Like flatMap, flatMapLatest creates new streams for each source event. Instead of merging all created streams, it "switches" (hence the former name "switch") between them so that when a new stream is created, the earlierly created stream is no longer listened to.
Result stream ends when source stream has ended and the latest created stream has ended.