Skip to content

Instantly share code, notes, and snippets.

@felipeblassioli
Created June 27, 2015 02:54
Show Gist options
  • Save felipeblassioli/d949f1c9157bcdfb2cea to your computer and use it in GitHub Desktop.
Save felipeblassioli/d949f1c9157bcdfb2cea to your computer and use it in GitHub Desktop.
Hmm, bacon
var usersStream = Bacon.interval(3000)
.filter(function(){ return this.state.mode === "users" }.bind(this))
.flatMapLatest(function(){
var store = this.state.selectedStore;
return Bacon.fromPromise( $.ajax({
type: 'GET',
url: '/users/',
data: {store_id: store.id}
}) );
}.bind(this))
.map(function(resp){ return resp.data });
usersStream.filter(function(users){
var currentUsers = this.state.users;
var somethingChanged = false;
var oldIds = _.map(currentUsers, function(u){ return u.id });
var newIds = _.map(users, function(u){ return u.id });
//_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
//=> [1, 3, 4]
/* Find those missing (left) */
var missing = _.difference(newIds, oldIds);
console.log("missing: "+missing);
/* Find those added (enter) */
var added = _.difference(oldIds, newIds);
console.log("added: "+added);
/* Find state changes */
var same = _.intersection(oldIds, newIds);
var changed = _.filter(same, function(id){
var currentUser = currentUsers[_.find(
var candidateUser = users[_.find(curIds, {id:id})]
});
console.log("same: "+same);
console.log(missing.length);
return somethingChanged;
}.bind(this))
.log();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment