4

As a newcomer to bacon.js, I can't seem to understand the difference between an event stream and a property.

  • Properties are built from streams (except properties built with .constant?)
  • They have most methods in common
  • Subscribing to them works in the same way

Could someone explain the differences and when to use which?

In the example below, stream and property have the exact same behaviour. I'm afraid I fail to see beyond this.

var stream = Bacon.sequentially(250, [1, 2, 3, 4, 5, 6, 7, 8]);
var property = stream.toProperty();

stream.onValue(function (val) {
    console.log("Stream", val);
});

property.onValue(function (val) {
    console.log("Property", val);
});

1 Answers1

5

The Bacon.js properties are Behaviours in FRP literature, and EventStreams are just Events. They look very similar, but semantically they are very different.

Familiarise yourself with FRP concepts, by e.g. reading this answer.

Community
  • 1
  • 1
phadej
  • 11,947
  • 41
  • 78
  • From http://blog.flowdock.com/2013/01/22/functional-reactive-programming-with-bacon-js/ : *"Property is used as an abstraction for a time-varying value. Properties are similar to EventStreams, but they also have the notion of current value. Most of the functionality is shared with EventStreams."* What is meant by "current value"? It doesn't seem that the property value can be read directly. – Nicolas Le Thierry d'Ennequin Mar 27 '15 at 13:07
  • You cannot read the current value of Property (Behaviour), it's against the semantics. Behaviour is a time-varying value, but you cannot handle time explicitly. If we would give you ability to read *current* value, then right after you'll want to read *previous* value etc. In FRP you don't really need *current* value; e.g. if we show current time with `currentTime.assign(label, "text")`- there we handle *all* values universally, and the library takes care, that when `currentTime` changes, `label.text` is updated. – phadej Mar 29 '15 at 09:05
  • 1
    That's precisely how I figured things, but then why can't it be said that the eventStream has a current value too? Or is it that a property has *only* a current value, which differentiates it from a stream? – Nicolas Le Thierry d'Ennequin Mar 29 '15 at 10:33
  • 1
    You can talk about the *last* event occurred in EventStream, but there are no *current*. The discrete-time FRP makes it too easy to think about implementation of things (yes, Property and EventStream could be implemented using similar lower-level primitives), not about their **semantics**. Read about continuos-time FRP: even you'll never need one, it will make distinction more clear. E.g. animations are continuos-time, but the real world restricts us, so we sample it say 60 times per second. – phadej Mar 29 '15 at 11:16
  • 2
    There are *previous* values if we consider sampled values, but inside the (continuos) system there aren't. The difference culminates in how we (can) combine Properties and Events, e.g. `animationProperty.sampledBy(renderTicksStream)`. – phadej Mar 29 '15 at 11:16