Created
February 27, 2015 20:21
-
-
Save plemarquand/54cab3bb6c9bacfcf087 to your computer and use it in GitHub Desktop.
Bacon.fromBifurcatedCallback
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
| /** | |
| * 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