Skip to content

Commit a9e1471

Browse files
author
Einar Norðfjörð
committed
Flip argument order of takeUntil module
The takeUntil module violates one of the first principles of functional programming. Its argument order was (src, end) which is a data first argument order. This commit flips the argument order to (end, src) allowing us to better use takeUntil in pipelines such as: ```js const query = stream(''); // Start fetching the results but if they haven't // arrived when a new query is emitted // stop listening to the stream const results = query .chain(q => fromPromise(getResults(q)) .pipe(takeUntil(query)) ) ```
1 parent 0b1a435 commit a9e1471

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ declare module 'flyd/module/switchlatest' {
226226

227227
declare module 'flyd/module/takeuntil' {
228228
interface takeuntil {
229-
<T, V>(source: flyd.Stream<T>, end: flyd.Stream<V>): flyd.Stream<T>;
230-
<T>(source: flyd.Stream<T>): <V>(end: flyd.Stream<V>) => flyd.Stream<T>;
229+
<T, V>(end: flyd.Stream<T>, src: flyd.Stream<V>): flyd.Stream<V>;
230+
<T>(end: flyd.Stream<T>): <V>(src: flyd.Stream<V>) => flyd.Stream<V>;
231231
}
232232
const _takeuntil: takeuntil;
233233
export = _takeuntil;

module/takeuntil/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ __Graph__
66
```
77
a: {---1---2---3---4}
88
b: {---------x------}
9-
takeUntil(a, b): {---1---2--------}
9+
takeUntil(b, a): {---1---2--------}
1010
```
1111

1212
__Signature__
1313

14-
`Stream a -> Stream b -> Stream a`
14+
`Stream a -> Stream b -> Stream b`
1515

1616
__Usage__
1717

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

2121
const source = flyd.stream()
2222
const end = flyd.stream()
23-
const result = takeUntil(source, end)
23+
const result = takeUntil(end, source)
2424

2525
source(1)(2)
2626
result() // 2

module/takeuntil/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var flyd = require('../../lib');
22

3-
module.exports = flyd.curryN(2, function(src, term) {
3+
module.exports = flyd.curryN(2, function(term, src) {
44
return flyd.endsOn(flyd.merge(term, src.end), flyd.combine(function(src, self) {
55
self(src());
66
}, [src]));

module/takeuntil/test/index.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,39 @@ var assert = require('assert');
55
var takeUntil = require('../index');
66

77
describe('takeUntil', function() {
8-
it('emits values from first stream', function() {
8+
it('emits values from source stream', function() {
99
var result = [];
1010
var source = stream();
1111
var terminator = stream();
12-
var s = takeUntil(source, terminator);
13-
flyd.map(function(v) { result.push(v); }, s);
12+
source
13+
.pipe(takeUntil(terminator))
14+
.map(function(v) { result.push(v); });
1415
source(1)(2)(3);
1516
assert.deepEqual(result, [1, 2, 3]);
1617
});
17-
it('ends when value emitted from second stream', function() {
18+
it('ends when value emitted from terminator stream', function() {
1819
var result = [];
1920
var source = stream();
2021
var terminator = stream();
21-
var s = takeUntil(source, terminator);
22-
flyd.map(function(v) { result.push(v); }, s);
23-
s(1);
22+
var s = source
23+
.pipe(takeUntil(terminator))
24+
.map(function(v) { result.push(v); });
25+
source(1);
2426
terminator(true);
25-
s(2);
27+
source(2);
2628
assert.deepEqual(result, [1]);
2729
assert(s.end());
2830
});
2931
it('ends if source stream ends', function() {
3032
var result = [];
3133
var source = stream();
3234
var terminator = stream();
33-
var s = takeUntil(source, terminator);
34-
flyd.map(function(v) { result.push(v); }, s);
35-
s(1);
35+
var s = source
36+
.pipe(takeUntil(terminator))
37+
.map(function(v) { result.push(v); });
38+
source(1);
3639
source.end(true);
37-
s(2);
40+
source(2);
3841
assert.deepEqual(result, [1]);
3942
assert(s.end());
4043
});

0 commit comments

Comments
 (0)