Weather Report Guide (Advanced)

This guides you through extending the Simple Weather Report Guide to remove imperative code and automatically look up the user’s location using the
browser’s geolocation API. Both of these will be done with event streams.

This guide continues where the Simple Weather Report Guide left off. It takes about 25 minutes to complete. It was written with CanJS 3.8.

The problem

This is imperative code.
It uses side-effects to change the value
of place when location is changed. The rules for how place behaves are not
defined in one place, which makes the code harder to follow.

Instead, we want to completely define the behavior of place within the place definition, which looks like
this:

Notice that the can-define-stream-kefir module is used as a mixin. When called on a type (like Person), the mixin
looks for PropDefinitions with stream
property definition functions. It uses the stream instance returned by the stream property definition function as the value of the property.

Stream properties, like asynchronous getters, only have a value when
bound to. To read the nameChangeCount, first use .on like:

Typically, you listen to sources and emit values in the setup function
and stop listening to sources in the teardown function. For example,
the following might listen to where the user’s mouse is on the page:

Kefir’s stream.withHandler( handler(emitter, event) ) is able to convert one stream’s events to another stream. All other stream methods like stream.map and stream.scan can be implemented with stream.withHandler. For example, the following maps the cursorPosition stream to a cursorDistance stream: