-
-
Save mdobson/b673672c6e558c4db764 to your computer and use it in GitHub Desktop.
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
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