A function to use with xstream#compose to filter repeated events
Example
stream
.compose(distinct((a, b) => a.id === b.id)
.map(uniqueValue => { //... })
| /** | |
| * A Function to use with xstream$XStream#compose | |
| * filters the stream to allow allow new events. | |
| * Uses a compareFn to check if the event is new or not | |
| * @param {Function} compareFn - a function that receives two elements and compares return whether they are equal or not | |
| */ | |
| const distinct = (compareFn) => { | |
| const buffer = []; | |
| return (stream) => { | |
| return stream | |
| .filter(val => { | |
| const found = buffer.find(b => compareFn(val, b)) | |
| if (found) return false; | |
| buffer.push(val); | |
| return true; | |
| }); | |
| }; | |
| }; | |
| export default distinct; |