Created
March 12, 2016 14:37
-
-
Save rzachariah/a75d17450f7440086bad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Imagine we have an observable of key counts. Each message has a key and a count, like so | |
// { | |
// key: "SomeKey", | |
// count: 16 | |
// } | |
// We use buffer to convert this to an observable of arrays of messages | |
// Now we want to reduce each array, keeping only the latest count for each key | |
var messages = getKeyCountsObservablefromSomewhere() | |
.buffer(function () { return Rx.Observable.timer(50); }) | |
.select(function(messages) { | |
if (messages.length === 0) return messages; | |
var hash = {}; | |
messages.forEach(function(m) { | |
hash[m.key] = m.count; | |
}) | |
console.log('hash', hash); | |
var uniqueMessages = []; | |
Object.keys(hash).forEach(function(key) { | |
uniqueMessages.push({key: key, count: hash[key]}); | |
}) | |
console.log('uniqueMessages', uniqueMessages); | |
return uniqueMessages; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment