Skip to content

Commit

Permalink
Docs: Acceptable uses of flatMap in blog post
Browse files Browse the repository at this point in the history
  • Loading branch information
raquo committed May 17, 2024
1 parent 20048af commit 2ca18da
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions website/blog/2024-05-14-laminar-v17.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,22 @@ Unfortunately, with `flatMap` being such a common operation on many data types,

Most of the problem with `flatMap` is its very inviting / innocuous name, as well as Scala's for-comprehensions using it invisibly under the hood, resulting in developers using it on autopilot. And so, to improve the user experience, especially for non-experts, the method called `flatMap` on Observables is now renamed into several variants, such as `flatMapSwitch`, `flatMapMerge`, and `flatMapCustom`. It is thus no longer available via for-comprehensions.

Of the new operators, `flatMapSwitch` is the **"standard"** one that matches the default behaviour of `flatMap`.

Similar changes apply to the `flatten` operator, of course.

See the rewritten [Flattening Observables](https://github.com/raquo/Airstream/#flattening-observables) section of Airstream docs.

> **UPDATE:** I would like to emphasize that using `flatMap` (now `flatMapSwitch`) to get _async_ events is perfectly fine. To put it simply, the concept of glitches basically does not apply to observables that intentionally emit their events asynchronously. So you can safely use `flatMapSwitch` to get e.g. network responses:
```scala
// This is fine.
val userS: Signal[User] = ???
val responseS: EventStream[Response] = userS.flatMapSwitch { user =>
FetchStream.get(s"/user/${user.id}")
}
```

**Migration:**
- First, see the compiler error caused by `flatMap` usage, and import `FlattenStrategy.flatMapAllowed` as necessary, to make your code compile as-is.
- Note: `flatMap` is only problematic on observables. Event props like `onClick` are `EventProcessor`-s, not `Observable`-s, so `onClick.flatMap` does not cause a problem, and does not raise any errors.
Expand Down

0 comments on commit 2ca18da

Please sign in to comment.