From 46ef88cb06fac02fdfed79214c1969f9262af350 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)) ) ``` --- index.d.ts | 4 ++-- module/takeuntil/README.md | 6 +++--- module/takeuntil/index.js | 2 +- module/takeuntil/test/index.js | 29 ++++++++++++++++------------- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/index.d.ts b/index.d.ts index fedac97..5ce32fb 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/takeuntil/README.md b/module/takeuntil/README.md index fb0e293..f8d965b 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 b -> Stream a -> Stream a` __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..26fe551 100644 --- a/module/takeuntil/index.js +++ b/module/takeuntil/index.js @@ -1,6 +1,6 @@ var flyd = require('../../lib'); -module.exports = flyd.curryN(2, function(src, term) { +module.exports = flyd.curryN(2, function(term, src) { return flyd.endsOn(flyd.merge(term, src.end), flyd.combine(function(src, self) { self(src()); }, [src])); diff --git a/module/takeuntil/test/index.js b/module/takeuntil/test/index.js index 8bda6f5..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); - s(1)(2)(3); + 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()); });