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

Flip argument order of takeUntil module #175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
logs
*.log

# vscode settings
.vscode

# Dependency directory
node_modules
bower_components
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ declare module 'flyd/module/switchlatest' {

declare module 'flyd/module/takeuntil' {
interface takeuntil {
<T, V>(source: flyd.Stream<T>, end: flyd.Stream<V>): flyd.Stream<T>;
<T>(source: flyd.Stream<T>): <V>(end: flyd.Stream<V>) => flyd.Stream<T>;
<T, V>(end: flyd.Stream<T>, src: flyd.Stream<V>): flyd.Stream<V>;
<T>(end: flyd.Stream<T>): <V>(src: flyd.Stream<V>) => flyd.Stream<V>;
}
const _takeuntil: takeuntil;
export = _takeuntil;
Expand Down
5 changes: 1 addition & 4 deletions module/switchlatest/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
var flyd = require('../../lib');
var takeUntil = require('../takeuntil');
var drop = require('ramda/src/drop');

var dropCurrentValue = flyd.transduce(drop(1));

module.exports = function(s) {
return flyd.combine(function(stream$, self) {
var value$ = stream$();
flyd.on(self, takeUntil(value$, dropCurrentValue(stream$)));
flyd.on(self, takeUntil(stream$, value$));
}, [s]);
};
6 changes: 3 additions & 3 deletions module/takeuntil/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ __Graph__
```
a: {---1---2---3---4}
b: {---------x------}
takeUntil(a, b): {---1---2--------}
takeUntil(b, a): {---1---2--------}
```

__Signature__

`Stream a -> Stream b -> Stream a`
`Stream a -> Stream b -> Stream b`

__Usage__

Expand All @@ -20,7 +20,7 @@ const takeUntil = require('flyd/module/takeuntil')

const source = flyd.stream()
const end = flyd.stream()
const result = takeUntil(source, end)
const result = takeUntil(end, source)

source(1)(2)
result() // 2
Expand Down
9 changes: 7 additions & 2 deletions module/takeuntil/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
var flyd = require('../../lib');
var drop = require('ramda/src/drop');

module.exports = flyd.curryN(2, function(src, term) {
return flyd.endsOn(flyd.merge(term, src.end), flyd.combine(function(src, self) {
var drop1 = flyd.transduce(drop(1));

module.exports = flyd.curryN(2, function(term, src) {
var end$ = flyd.merge(term.hasVal ? drop1(term) : term, src.end);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To maintain the behaviour of takeUntil as it was before #180 we need to drop a single value from the terminator stream if it already has a value, otherwise the stream will end immediately.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an example of code that wouldn't work without it would be:

const s = stream();
const result$ = s.chain(q => fromPromise(getSearchResults(q)))
  .pipe(takeUntil(s))
  .map(results => h('ul', results.content.map(res => h('li', res))))
  .map(render('#results'));

In the above example you would only be able to search once. Any subsequent search would be ended immediately and not return a value.


return flyd.endsOn(end$, flyd.combine(function(src, self) {
self(src());
}, [src]));
});
27 changes: 15 additions & 12 deletions module/takeuntil/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,39 @@ var assert = require('assert');
var takeUntil = require('../index');

describe('takeUntil', function() {
it('emits values from first stream', function() {
it('emits values from source stream', function() {
var result = [];
var source = stream();
var terminator = stream();
var s = takeUntil(source, terminator);
flyd.map(function(v) { result.push(v); }, s);
source
.pipe(takeUntil(terminator))
.map(function(v) { result.push(v); });
source(1)(2)(3);
assert.deepEqual(result, [1, 2, 3]);
});
it('ends when value emitted from second stream', function() {
it('ends when value emitted from terminator stream', function() {
var result = [];
var source = stream();
var terminator = stream();
var s = takeUntil(source, terminator);
flyd.map(function(v) { result.push(v); }, s);
s(1);
var s = source
.pipe(takeUntil(terminator))
.map(function(v) { result.push(v); });
source(1);
terminator(true);
s(2);
source(2);
assert.deepEqual(result, [1]);
assert(s.end());
});
it('ends if source stream ends', function() {
var result = [];
var source = stream();
var terminator = stream();
var s = takeUntil(source, terminator);
flyd.map(function(v) { result.push(v); }, s);
s(1);
var s = source
.pipe(takeUntil(terminator))
.map(function(v) { result.push(v); });
source(1);
source.end(true);
s(2);
source(2);
assert.deepEqual(result, [1]);
assert(s.end());
});
Expand Down