From 96c915c38c3ac9dda5a9068c3b8d2bad923b2c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Einar=20Nor=C3=B0fj=C3=B6r=C3=B0?= Date: Wed, 25 Apr 2018 17:58:03 +0000 Subject: [PATCH] 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)) ) ``` This also fixes an issue with takeUntil where if the new endStream had a value the dependent stream wouldn't update. --- .gitignore | 3 +++ index.d.ts | 4 ++-- module/switchlatest/index.js | 5 +---- module/takeuntil/README.md | 6 +++--- module/takeuntil/index.js | 9 +++++++-- module/takeuntil/test/index.js | 27 +++++++++++++++------------ 6 files changed, 31 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index f2e1cb0..03045b0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ logs *.log +# vscode settings +.vscode + # Dependency directory node_modules bower_components diff --git a/index.d.ts b/index.d.ts index eb0a31e..2b3852d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -226,8 +226,8 @@ declare module 'flyd/module/switchlatest' { declare module 'flyd/module/takeuntil' { interface takeuntil { - (source: flyd.Stream, end: flyd.Stream): flyd.Stream; - (source: flyd.Stream): (end: flyd.Stream) => flyd.Stream; + (end: flyd.Stream, src: flyd.Stream): flyd.Stream; + (end: flyd.Stream): (src: flyd.Stream) => flyd.Stream; } const _takeuntil: takeuntil; export = _takeuntil; diff --git a/module/switchlatest/index.js b/module/switchlatest/index.js index 699f73e..36629fa 100644 --- a/module/switchlatest/index.js +++ b/module/switchlatest/index.js @@ -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]); }; diff --git a/module/takeuntil/README.md b/module/takeuntil/README.md index fb0e293..875e7d8 100644 --- a/module/takeuntil/README.md +++ b/module/takeuntil/README.md @@ -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__ @@ -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 diff --git a/module/takeuntil/index.js b/module/takeuntil/index.js index 9f378c6..0ef564a 100644 --- a/module/takeuntil/index.js +++ b/module/takeuntil/index.js @@ -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); + + return flyd.endsOn(end$, flyd.combine(function(src, self) { self(src()); }, [src])); }); diff --git a/module/takeuntil/test/index.js b/module/takeuntil/test/index.js index b5be138..5452776 100644 --- a/module/takeuntil/test/index.js +++ b/module/takeuntil/test/index.js @@ -5,24 +5,26 @@ 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()); }); @@ -30,11 +32,12 @@ describe('takeUntil', 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()); });