Re: Futures in WebRTC

On 03/06/2013 5:37 PM, Jan-Ivar Bruaroey wrote:
>
> Let me admit that I read about DOM Futures for the first time today at 
> https://github.com/slightlyoff/DOMFuture so it's still a bit of a 
> head-rush, but a welcome one, as I'd been scratching my head about how 
> our api could be made easier.
>
> I like it! - TL;DR: Success/failure callbacks encapsulated in 
> primary-return object let you write robust asynchronous code linearly.

     I've recently migrated my own application from callbacks to DOM 
Futures and I must say it's a pleasure to work with. There are some 
cosmetic issues which I hope will be improved (see [1] and [2]) but one 
major issue I want to draw light to is: 
https://github.com/slightlyoff/Futures/issues/57

[1] done() method shows up in specification but is being removed in the 
implementation: https://github.com/slightlyoff/Futures/issues/33
[2] API method names should be more intuitive: 
https://github.com/slightlyoff/Futures/issues/55

> Thanks! An example would help me, so I tried to write one from this. 
> Please correct me if I get this wrong of if it can be made simpler. 
> Let me use this dense "simple test" I have lying around. First the old 
> way:
>
> function simpletest_oldapi() {
>   var pc1 = new RTCPeerConnection();
>   var pc2 = new RTCPeerConnection();
>
>   navigator.GetUserMedia({video:true}, function(video1) {
>     pc1.addStream(video1);
>     pc1.createOffer(function(offer) {
>       pc1.setLocalDescription(offer, function() {
>         pc2.setRemoteDescription(offer, function() {
>           pc2.createAnswer(function(answer) {
>             pc2.setLocalDescription(answer, function() {
>               pc1.setRemoteDescription(answer, function() {
>                 finish();
>               }, fail);
>             }, fail);
>           }, fail);
>         }, fail);
>       }, fail);
>     }, fail);
>   }, fail);
> }
>
> function simpletest_newapi() {
>   var pc1 = new RTCPeerConnection();
>   var pc2 = new RTCPeerConnection();
>
>   navigator.GetUserMedia({video:true})
>     .then(function(video1) {
>       pc1.addStream(video1);
>       return pc1.createOffer();
>     })
>     .then(function(offer) {
>       return pc1.setLocalDescription(offer)
>         .then(pc2.setRemoteDescription(offer););
>     })
>     .then(pc2.createAnswer())
>     .then(function(answer) {
>       return pc2.setLocalDescription(answer)
>         .then(pc1.setRemoteDescription(answer));
>     })
>     .then(finish, fail);
> }
>
> Is that about right?

     Almost. This code doesn't make sense to me: 
"then(pc2.setRemoteDescription(offer););"

     then() expects you to pass in a callback. 
"pc2.setRemoteDescription(offer)" returns a Future, not a callback. I 
think you'd have to surround the call with "function(){}" to fix the code.

Gili

Received on Monday, 3 June 2013 22:10:38 UTC