Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite stream #2207

Merged
merged 16 commits into from
Nov 27, 2018
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'MithrilJS/next' into streams-rewrite
# Conflicts:
#	docs/stream.md
#	stream/stream.js
  • Loading branch information
porsager committed Nov 22, 2018
commit f84ac07aff83104145309240d201bcf9f0844f75
33 changes: 33 additions & 0 deletions docs/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
- [Stream.merge](#streammerge)
- [Stream.scan](#streamscan)
- [Stream.scanMerge](#streamscanmerge)
- [Stream.lift](#streamlift)
- [Stream.SKIP](#streamskip)
>>>>>>> MithrilJS/next
- [Stream["fantasy-land/of"]](#streamfantasy-landof)
- [Instance members](#instance-members)
- [stream.map](#streammap)
Expand Down Expand Up @@ -151,6 +153,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.SKIP

A special value that can be returned to stream callbacks to skip execution of downstreams
Expand Down
11 changes: 10 additions & 1 deletion stream/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"use strict"
/* eslint-enable */
Stream.SKIP = {}
Stream.lift = lift
Stream.scan = scan
Stream.merge = merge
Stream.combine = combine
Expand Down Expand Up @@ -82,7 +83,7 @@ function Stream(value) {
function combine(fn, streams) {
var ready = streams.every(function(s) {
if (s.constructor !== Stream)
throw new Error("Ensure that each item passed to stream.combine/stream.merge is a stream")
throw new Error("Ensure that each item passed to stream.combine/stream.merge/lift is a stream")
return s.state === "active"
})
var stream = ready
Expand Down Expand Up @@ -137,6 +138,14 @@ function scanMerge(tuples, seed) {
return stream
}

function lift() {
var fn = arguments[0]
var streams = Array.prototype.slice.call(arguments, 1)
return merge(streams).map(function(streams) {
return fn.apply(undefined, streams)
})
}

function open(s) {
return s.state === "pending" || s.state === "active" || s.state === "changing"
}
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.