From 5afb43b5ebb0483eb317b46fd4f2c65604c68dd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Einar=20Nor=C3=B0fj=C3=B6r=C3=B0?= Date: Sun, 28 Jan 2018 15:43:02 +0000 Subject: [PATCH] Address issues brought up in code review --- README.md | 34 +++------------------------------- index.d.ts | 5 ++--- lib/index.js | 13 +------------ test/index.js | 6 +++--- 4 files changed, 9 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 61a3c07..f4370e9 100644 --- a/README.md +++ b/README.md @@ -46,9 +46,7 @@ __Other features__ * Supports the transducer protocol. You can for instance transduce streams with [Ramda](http://ramdajs.com/) and [transducers.js](https://github.com/jlongster/transducers.js). -* Complies to the [fantasy land](https://github.com/fantasyland/fantasy-land) * [Elegant support for promises](#using-promises-for-asynchronous-operations). - monad specification. * [Atomic updates](#atomic-updates). ## Examples @@ -224,17 +222,6 @@ flyd.on(function(responses) { }, responses); ``` -__Note:__ this functionality has been deprecated in favour of `flyd.fromPromise`. -The above example should idiomatically be written as: - -```javascript -var urls = flyd.stream('/something.json'); -var responses = flyd.fromPromise(requestPromise(urls())); -flyd.on(function(responses) { - console.log('Received response!'); - console.log(responses()); -}, responses); -``` ### Mapping over a stream You've now seen most of the basic building block which Flyd provides. Let's see @@ -450,7 +437,7 @@ __Example__ ```javascript var filter = flyd.stream('filter'); var search_results = flyd.chain(function(filter){ - return flyd.fromPromise(getResults(filter)); + return flyd.stream(getResults(filter)); }, filter); ``` @@ -471,7 +458,7 @@ while it can not seem useful immediately consider this example ```javascript var get_results = function (filter, sortProperty, sortDirection) { - return flyd.fromPromise(fetch(`${base_url}/search?q=${filter}&sort=${sortProperty} ${sortDirection}`)) + return flyd.stream(fetch(`${base_url}/search?q=${filter}&sort=${sortProperty} ${sortDirection}`)) }; // this would eventually be linked to an input field @@ -565,21 +552,6 @@ s1(1)(1)(2)(3)(3)(3)(4); results; // [2, 4, 6, 8] ``` -### flyd.fromPromise(promise) - -Transforms a promise into a stream. - -__Signature__ -`Promise a -> Stream a` - -__Example__ -```javascript -const urls = flyd.stream('./something.json'); -const requests = flyd.chain(function(url){ - return flyd.fromPromise(requestPromise(url)); -}, urls); -``` - ### flyd.curryN(n, fn) Returns `fn` curried to `n`. Use this function to curry functions exposed by @@ -650,7 +622,7 @@ var squaredNumbers = numbers var filter = flyd.stream('filter'); var search_results = filter .pipe(flyd.chain(function(filter){ - return flyd.fromPromise(getResults(filter)); + return flyd.stream(getResults(filter)); })); // use with a flyd module diff --git a/index.d.ts b/index.d.ts index 7282b56..0e4ad5e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -7,6 +7,7 @@ declare namespace flyd { interface Stream { (): T; (value: T): Stream; + (value: Promise | PromiseLike): Stream; /** @@ -54,12 +55,12 @@ declare namespace flyd { interface CreateStream { (): Stream; (value: T): Stream; + (value: Promise | PromiseLike): Stream; (): Stream; } interface Static { stream: CreateStream; - of: CreateStream; immediate(stream: Stream): Stream; isStream(stream: any): boolean; @@ -89,8 +90,6 @@ declare namespace flyd { transduce(mapfn: Function, stream: Stream): Stream; transduce(mapfn: Function): (stream: Stream) => Stream; - fromPromise(promise: Promise | PromiseLike): Stream; - curryN(length: number, fn: (...args: Array) => void): Function; } } diff --git a/lib/index.js b/lib/index.js index 3cf223f..796d6f4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -44,7 +44,6 @@ flyd.stream = function(initialValue) { return s; } // fantasy-land Applicative -flyd.of = flyd.stream; flyd[FL.of] = flyd.stream; /** @@ -203,7 +202,7 @@ flyd.map = curryN(2, function(f, s) { * @example * var filter = flyd.stream('who'); * var items = flyd.chain(function(filter){ - * return flyd.fromPromise(findUseres(filter)); + * return flyd.stream(findUsers(filter)); * }, filter); */ flyd.chain = curryN(2, chain); @@ -312,15 +311,6 @@ flyd.merge = curryN(2, function(s1, s2) { return s; }); -flyd.fromPromise = function(promise) { - var s = flyd.stream(); - promise.then(function onSuccess(val) { - s(val); - s.end(true); - }); - return s; -} - /** * Creates a new stream resulting from applying `transducer` to `stream`. * @@ -668,7 +658,6 @@ function flushUpdate() { function updateStreamValue(s, n) { /* istanbul ignore if */ if (n !== undefined && n !== null && isFunction(n.then)) { - console.warn('Promise swallowing has been deprecated in favour of flyd.fromPromise'); n.then(s); return; } diff --git a/test/index.js b/test/index.js index ae483f8..0af856f 100644 --- a/test/index.js +++ b/test/index.js @@ -368,16 +368,16 @@ describe('stream', function() { }); }); - describe('fromPromise', function() { + describe('promise swallowing', function() { it('pushes result of promise down the stream', function(done) { - var s = flyd.fromPromise(Promise.resolve(12)); + var s = flyd.stream(Promise.resolve(12)); combine(function(s) { assert.equal(s(), 12); done(); }, [s]); }); it('recursively unpacks promise', function(done) { - var s = flyd.fromPromise(new Promise(function(res) { + var s = flyd.stream(new Promise(function(res) { setTimeout(function() { res(new Promise(function(res) { setTimeout(res.bind(null, 12));