Created
June 27, 2015 02:54
-
-
Save felipeblassioli/d949f1c9157bcdfb2cea to your computer and use it in GitHub Desktop.
Hmm, bacon
This file contains hidden or 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
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