Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Created February 21, 2019 23:34
Show Gist options
  • Save tzkmx/d82789cabb1c635fc10f5233ace72171 to your computer and use it in GitHub Desktop.
Save tzkmx/d82789cabb1c635fc10f5233ace72171 to your computer and use it in GitHub Desktop.
dropRepeats operator for mithril stream
/**
* Creates a dependent stream that only updates when the source stream value differs from the previous
* @see https://github.com/spacejack/mithril-stream-extra
* @param function a custom function to validate equals, default is '===' check
* @returns The resulting dependent stream
* @author Jesus E. Franco Martinez <tezcatl@fedoraproject.org> 2019
*/
stream.dropRepeats = function dropRepeats (customEquality) {
var oldVal // undefined for start
if (typeof customEquality !== 'function') {
customEquality = function (oldVal, newVal) {
return oldVal === newVal
}
}
return stream.map(function (newVal) {
if (customEquality(oldVal, newVal)) {
return Stream.SKIP
}
oldVal = newVal
return newVal
})
}
// this should be pushed as PR to mithril streams eventually
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment