Add Stream.lift (#1950)

* Add stream.lift and tests

* Add docs

* Add to change-log
This commit is contained in:
spacejack 2018-11-13 18:55:45 -05:00 committed by Isiah Meadows
parent a147023f4e
commit 76e585c523
4 changed files with 180 additions and 1 deletions

View file

@ -45,6 +45,7 @@
- render/core: revamp the core diff engine, and introduce a longest-increasing-subsequence-based logic to minimize DOM operations when re-ordering keyed nodes.
- API: Introduction of `m.prop()` ([#2268](https://github.com/MithrilJS/mithril.js/pull/2268))
- docs: Emphasize Closure Components for stateful components, use them for all stateful component examples.
- stream: Add `stream.lift` as a user-friendly alternative to `merge -> map` or `combine` [#1944](https://github.com/MithrilJS/mithril.js/issues/1944)
#### Bug fixes
@ -123,6 +124,10 @@
- Fix IE bug where active element is null causing render function to throw error ([#1943](https://github.com/MithrilJS/mithril.js/pull/1943), [@JacksonJN](https://github.com/JacksonJN))
#### Ospec improvements:
- Log using util.inspect to show object content instead of "[object Object]" ([#1661](https://github.com/MithrilJS/mithril.js/issues/1661), [@porsager](https://github.com/porsager))
---
### v1.1.3

View file

@ -7,6 +7,7 @@
- [Stream.merge](#streammerge)
- [Stream.scan](#streamscan)
- [Stream.scanMerge](#streamscanmerge)
- [Stream.lift](#streamlift)
- [Stream.HALT](#streamhalt)
- [Stream["fantasy-land/of"]](#streamfantasy-landof)
- [Instance members](#instance-members)
@ -151,6 +152,37 @@ Argument | Type | Required | De
---
##### Stream.lift
Creates a computed stream that reactively updates if any of its upstreams are updated. See [combining streams](#combining-streams). Unlike `combine`, the input streams are a variable number of arguments (instead of an array) and the callback receives the stream values instead of streams. There is no `changed` parameter. This is generally a more user-friendly function for applications than `combine`.
`stream = Stream.lift(lifter, stream1, stream2, ...)`
Argument | Type | Required | Description
------------ | --------------------------- | -------- | ---
`lifter` | `(any...) -> any` | Yes | See [lifter](#lifter) argument
`streams...` | list of `Streams` | Yes | Streams to be lifted
**returns** | `Stream` | | Returns a stream
[How to read signatures](signatures.md)
---
###### lifter
Specifies how the value of a computed stream is generated. See [combining streams](#combining-streams)
`any = lifter(streams...)`
Argument | Type | Required | Description
------------ | -------------------- | -------- | ---
`streams...` | splat of `Streams` | No | Splat of zero or more streams that correspond to the streams passed to [`stream.lift`](#stream-lift)
**returns** | `any` | | Returns a computed value
[How to read signatures](signatures.md)
---
##### Stream.HALT
A special value that can be returned to stream callbacks to halt execution of downstreams
@ -372,6 +404,18 @@ var greeting = stream.merge([a, b]).map(function(values) {
console.log(greeting()) // logs "hello world"
```
Or you can use the helper function `stream.lift()`
```javascript
var a = stream("hello")
var b = stream("world")
var greeting = stream.lift(function(_a, _b) {
return _a + " " + _b
}, a, b)
console.log(greeting()) // logs "hello world"
```
There's also a lower level method called `stream.combine()` that exposes the stream themselves in the reactive computations for more advanced use cases