Skip to content

Commit fb2124f

Browse files
authored
Merge pull request #168 from nordfjord/remove-promise-swallowing
Break things (followup from #166)
2 parents 082cc86 + f3a899b commit fb2124f

File tree

7 files changed

+83
-117
lines changed

7 files changed

+83
-117
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ __Other features__
4646

4747
* Supports the transducer protocol. You can for instance transduce streams with
4848
[Ramda](http://ramdajs.com/) and [transducers.js](https://github.com/jlongster/transducers.js).
49+
* Conforms to the fantasy land [monad](https://github.com/fantasyland/fantasy-land#monad) specification
4950
* [Atomic updates](#atomic-updates).
5051

5152
## Examples
@@ -647,7 +648,7 @@ var evenNumbers = numbers
647648
.pipe(filter(isEven));
648649
```
649650
650-
### stream.map(f) __Deprecated__
651+
### stream.map(f)
651652
652653
Returns a new stream identical to the original except every
653654
value will be passed through `f`.
@@ -666,7 +667,7 @@ var numbers = flyd.stream(0);
666667
var squaredNumbers = numbers.map(function(n) { return n*n; });
667668
```
668669
669-
### stream1.ap(stream2) __Deprecated__
670+
### stream1.ap(stream2)
670671
671672
`stream1` must be a stream of functions.
672673

examples/drag-and-drop/script.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var flyd = require('flyd');
2-
var flatMap = require('flyd/module/flatmap');
32
var takeUntil = require('flyd/module/takeuntil');
43

54
document.addEventListener('DOMContentLoaded', function() {
@@ -13,7 +12,7 @@ document.addEventListener('DOMContentLoaded', function() {
1312
document.addEventListener('mousemove', mousemove);
1413
document.addEventListener('mouseup', mouseup);
1514

16-
var mousedrag = flatMap(function(md) {
15+
var mousedrag = flyd.chain(function(md) {
1716
var startX = md.offsetX, startY = md.offsetY;
1817

1918
return takeUntil(flyd.map(function(mm) {

index.d.ts

-10
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,6 @@ declare module 'flyd/module/filter' {
128128
export = _Filter;
129129
}
130130

131-
declare module 'flyd/module/flatmap' {
132-
type projection<T, V> = (val: T) => flyd.Stream<V>;
133-
interface flatMap {
134-
<T, V>(project: projection<T, V>, source: flyd.Stream<T>): flyd.Stream<V>;
135-
<T, V>(project: projection<T, V>): (source: flyd.Stream<T>) => flyd.Stream<V>;
136-
}
137-
const _flatMap: flatMap;
138-
export = _flatMap;
139-
}
140-
141131
declare module 'flyd/module/forwardto' {
142132
interface ForwardTo {
143133
<T, V>(stream: flyd.Stream<V>, project: (value: V) => T): flyd.Stream<T>;

lib/index.js

-7
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ flyd.fromPromise = function fromPromise(p) {
438438
return s;
439439
}
440440

441-
/* istanbul ignore next */
442441
flyd.flattenPromise = function flattenPromise(s) {
443442
return combine(function(s, self) {
444443
s().then(self);
@@ -676,12 +675,6 @@ function flushUpdate() {
676675
* @param {*} value
677676
*/
678677
function updateStreamValue(s, n) {
679-
/* istanbul ignore if */
680-
if (n !== undefined && n !== null && isFunction(n.then)) {
681-
console.warn('flyd: Promise swallowing has been deprecated, please see https://github.com/paldepind/flyd#promises for more info');
682-
n.then(s);
683-
return;
684-
}
685678
s.val = n;
686679
s.hasVal = true;
687680
if (inStream === undefined) {

module/flatmap/README.md

-54
This file was deleted.

module/flatmap/index.js

-23
This file was deleted.

test/index.js

+79-19
Original file line numberDiff line numberDiff line change
@@ -369,26 +369,86 @@ describe('stream', function() {
369369
});
370370
});
371371

372-
describe('promise swallowing', function() {
373-
it('pushes result of promise down the stream', function(done) {
374-
var s = flyd.fromPromise(Promise.resolve(12));
375-
combine(function(s) {
376-
assert.equal(s(), 12);
377-
done();
378-
}, [s]);
372+
describe('Promises', function() {
373+
describe('fromPromise', function() {
374+
it('pushes result of promise down the stream', function(done) {
375+
var s = flyd.fromPromise(Promise.resolve(12));
376+
combine(function(s) {
377+
assert.equal(s(), 12);
378+
done();
379+
}, [s]);
380+
});
381+
it('recursively unpacks promise', function(done) {
382+
var s = flyd.fromPromise(new Promise(function(res) {
383+
setTimeout(function() {
384+
res(new Promise(function(res) {
385+
setTimeout(res.bind(null, 12));
386+
}));
387+
}, 20);
388+
}));
389+
combine(function(s) {
390+
assert.equal(s(), 12);
391+
done();
392+
}, [s]);
393+
});
394+
395+
it('does not process out of order promises', function(done) {
396+
var promises = [];
397+
var delay = function(ms, val) {
398+
var p = new Promise(function(res) {
399+
setTimeout(function() {
400+
res(val);
401+
}, ms)
402+
});
403+
promises.push(p);
404+
return p;
405+
};
406+
407+
var s = stream();
408+
var res = s.chain(function(val) {
409+
return flyd.fromPromise(delay(val, val));
410+
})
411+
.pipe(flyd.scan(function(acc, v) {
412+
return acc + v;
413+
}, 0));
414+
s(100)(50)(70)(200);
415+
416+
Promise.all(promises).then(function() {
417+
assert.equal(res(), 200);
418+
done();
419+
});
420+
421+
});
379422
});
380-
it('recursively unpacks promise', function(done) {
381-
var s = flyd.fromPromise(new Promise(function(res) {
382-
setTimeout(function() {
383-
res(new Promise(function(res) {
384-
setTimeout(res.bind(null, 12));
385-
}));
386-
}, 20);
387-
}));
388-
combine(function(s) {
389-
assert.equal(s(), 12);
390-
done();
391-
}, [s]);
423+
describe('flattenPromise', function() {
424+
it('processes out of order promises', function(done) {
425+
var promises = [];
426+
var delay = function(ms, val) {
427+
var p = new Promise(function(res) {
428+
setTimeout(function() {
429+
res(val);
430+
}, ms)
431+
});
432+
promises.push(p);
433+
return p;
434+
};
435+
436+
var s = stream();
437+
var res = s.map(function(val) {
438+
return delay(val, val);
439+
})
440+
.pipe(flyd.flattenPromise)
441+
.pipe(flyd.scan(function(acc, v) {
442+
return acc + v;
443+
}, 0));
444+
s(100)(50)(70)(200);
445+
446+
Promise.all(promises).then(function() {
447+
assert.equal(res(), 420);
448+
done();
449+
});
450+
451+
});
392452
});
393453
});
394454

0 commit comments

Comments
 (0)