Skip to content

Commit

Permalink
Address issues brought up in code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Einar Norðfjörð committed Jan 28, 2018
1 parent b10cb31 commit 5afb43b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 49 deletions.
34 changes: 3 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
```
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ declare namespace flyd {
interface Stream<T> {
(): T;
(value: T): Stream<T>;
(value: Promise<T> | PromiseLike<T>): Stream<T>;


/**
Expand Down Expand Up @@ -54,12 +55,12 @@ declare namespace flyd {
interface CreateStream {
<T>(): Stream<T>;
<T>(value: T): Stream<T>;
<T>(value: Promise<T> | PromiseLike<T>): Stream<T>;
(): Stream<void>;
}

interface Static {
stream: CreateStream;
of: CreateStream;

immediate<T>(stream: Stream<T>): Stream<T>;
isStream(stream: any): boolean;
Expand Down Expand Up @@ -89,8 +90,6 @@ declare namespace flyd {
transduce<T, V>(mapfn: Function, stream: Stream<T>): Stream<V>;
transduce<T, V>(mapfn: Function): (stream: Stream<T>) => Stream<V>;

fromPromise<T>(promise: Promise<T> | PromiseLike<T>): Stream<T>;

curryN(length: number, fn: (...args: Array<any>) => void): Function;
}
}
Expand Down
13 changes: 1 addition & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ flyd.stream = function(initialValue) {
return s;
}
// fantasy-land Applicative
flyd.of = flyd.stream;
flyd[FL.of] = flyd.stream;

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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`.
*
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 5afb43b

Please sign in to comment.