Skip to content

Instantly share code, notes, and snippets.

@nickydonna
Last active May 17, 2017 12:37
Show Gist options
  • Save nickydonna/df7996f68a978b4f36dd7f50817fc1a8 to your computer and use it in GitHub Desktop.
Save nickydonna/df7996f68a978b4f36dd7f50817fc1a8 to your computer and use it in GitHub Desktop.
XStream distinct

xstream distinct

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment