Skip to content

Instantly share code, notes, and snippets.

@mdobson
Created July 11, 2014 13:46
Show Gist options
  • Save mdobson/b673672c6e558c4db764 to your computer and use it in GitHub Desktop.
Save mdobson/b673672c6e558c4db764 to your computer and use it in GitHub Desktop.
Runtime.prototype.observe = function(queries, cb) {
var self = this;
var filters = [];
queries.forEach(function(query){
var filter = self._observable.filter(function(device) {
return query.match(device);
});
filters.push(filter);
});
var source = null;
if(filters.length > 1) {
filters.push(function() {
return Array.prototype.slice.call(arguments);
});
source = Rx.Observable.zip.apply(null, filters);
} else {
source = filters[0];
}
if(this._jsDevices.length){
var alreadyDiscoveredSource = Rx.Observable.create(function(observer){
Object.keys(self._jsDevices).forEach(function(device){
observer.onNext(self._jsDevices[device]);
});
observer.onCompleted();
});
this._observable.merge(alreadyDiscoveredSource);
}
source
.subscribe(function(args){
if (Array.isArray(args)) {
cb.apply(null, args);
} else {
cb.apply(null, [args]);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment