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

adds filter method to streams #1128

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions util/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ function createStream() {
return stream._state.value
}
initStream(stream, arguments)

if (arguments.length > 0) updateStream(stream, arguments[0], undefined)

return stream
}
function initStream(stream, args) {
Expand All @@ -18,7 +18,8 @@ function initStream(stream, args) {
stream.map = map, stream.ap = ap, stream.of = createStream
stream.valueOf = valueOf
stream.catch = doCatch

stream.filter = filter

Object.defineProperties(stream, {
error: {get: function() {
if (!stream._state.errorStream) {
Expand Down Expand Up @@ -119,10 +120,10 @@ function initDependency(dep, streams, derive, recover) {
state.derive = derive
state.recover = recover
state.parents = streams.filter(notEnded)

registerDependency(dep, state.parents)
updateDependency(dep, true)

return dep
}
function registerDependency(stream, parents) {
Expand All @@ -149,6 +150,12 @@ function map(fn) {return combine(function(stream) {return fn(stream())}, [this])
function ap(stream) {return combine(function(s1, s2) {return s1()(s2())}, [this, stream])}
function valueOf() {return this._state.value}

function filter(fn) {
var stream = createStream()
this.map(function(value){ if(fn(value)) stream(value) })
return stream
}

function active(stream) {return stream._state.state === 1}
function changed(stream) {return stream._state.changed}
function notEnded(stream) {return stream._state.state !== 2}
Expand Down
Loading