Skip to content

Instantly share code, notes, and snippets.

@plemarquand
Created February 27, 2015 20:21
Show Gist options
  • Save plemarquand/54cab3bb6c9bacfcf087 to your computer and use it in GitHub Desktop.
Save plemarquand/54cab3bb6c9bacfcf087 to your computer and use it in GitHub Desktop.
Bacon.fromBifurcatedCallback
/**
* Similar to Bacon.fromNodeCallback except instead of a single callback,
* the value and error result are split to two different callbacks.
* Works for the form method(...args, successCallback, errorCallback);
* This style of API is used by https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getUserMedia
*/
Bacon.fromBifurcatedCallback = function(method, ...args) {
return Bacon.fromBinder(sink => {
args.push(res => {
sink(res);
sink(new Bacon.End());
});
args.push(err => sink(new Bacon.Error(err)));
method.apply(undefined, args);
});
};
// Example using navigator.getUserMedia
var stream = Bacon.fromBifurcatedCallback(navigator.getUserMedia.bind(navigator), {video: true})
stream.onValue(stream => document.querySelector('video').src = window.URL.createObjectURL(stream));
stream.onError(err => console.error('Error getting stream, maybe the user denied permissions.'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment