Re: [presentation-api] Specify behavior when multiple controlling pages are connected to the session

I haven't found any examples of using getRegistration() and 
getRegistrations() on HTML5Rocks or MDN or in the API draft. From the 
algorithm description linked it seems that getRegistration() is used 
to get a registration for ServiceWorker with a known scope URL, while 
getRegistrations() is an enumeration method to query all registrations
 available. There's also no event to notify the page when a new 
registration is available. IMHO, it doesn't match what's needed by the
 Presentation API... (get the session for the initial controller page 
and get the rest of them as they connect).

Answers to the questions:
1. the UA could reject the Promise returned by a consequently called 
method or queue the promises and resolve them one by one when getting 
new connections.
2. in the example there's an // assert controller.state == 
"connecting"; meaning that the page waits for the state to become 
"connected" before appending the controller to its list
3. yes (it's related to the issues #34 and #32 )
4. it seems to be redundant

The questions I have about the proposed resolution:
- ```getSession().then(setSession)``` seems to be redundant for the 
simple use case since can be expressed as 
```getSessions().then(function (sessions) { setSession(sessions[0]); 
})``` - why is it needed then? it could be useful to distinguish 
between the first controller (e.g. game master) from the rest - then 
the page should call both ```getSession()``` and ```getSessions()``` 
and filter the result of the former from the results of the latter.
- what does getSession() return if the initial controller has 
disconnected but there're some other controllers still connected?
- the page will have to subscribe to the ```onsessionavailable``` 
after ```getSessions()``` is resolved; having a promise and an event 
seems to be uncommon; as discussed over availability a few times it's 
usually a Promise or a property (e.g. ```sessions``` and an event); we
 could consider an approach similar to availability in #81
```idl
partial interface NavigatorPresentation {
  Promise<PresentationContext> presentationContext();
}

interface PresentationContext : EventTarget {
  readonly attribute PresentationSession[] sessions;
  attribute EventHandler onsessionavailable;
};
```
- the API seems to unnecessarily push a concept of a uniform array of 
the controllers to the page; which may not be convenient

My example using the proposed resolution from F2F would look something
 like:

```javascript
// controlling pages that successfully connected
var mSessions = [];
// maximum number of sessions to join the presentation
var MAX_SESSIONS = 5;

// announce that the page is ready to become a presentation and 
receive the controllers that are 
// already connected or connecting
// NOTE: if knowning the initial controller is important, the page 
must first call getSession() and execute 
// the call below in the promise resolution handler:
// var initialSession = null;
// navigator.presentation.getSession().then(function (firstSession) {
//     initialSession = firstSession;
navigator.presentation.getSessions().then(handleSessionsAdded);

var handleSessionsAdded = function(sessions) {
    // page could get more than the maximum supported number of 
sessions received in getSessions()
    for (var i = 0; i < min(sessions.length, MAX_SESSIONS); i++) 
        handleSessionAdded(sessions[i]);
    navigator.presentation.onsessionavailable = function(evt) {
        if (mSessions.length == MAX_SESSIONS) evt.preventDefault();
        handleSessionAdded(evt.session);
    };
};

var handleSessionAdded = function(session) {
    showMessage("Session added: " + session.id);
    // assert session.state == "connecting";
    session.onstatechange = function(e) {
        if (e.state == "connected") {
            handleSessionConnection(this)
        } else if (e.state == "disconnected" || e.state == 
"terminated") {
            handleSessionDisconnection(this);
        }
    };
};

var handleSessionConnection = function(session) {
    if (mControllers.empty()) {
        setUp();
    }   
    mSessions.add(sessions);
    session.onmessage = function(e) {
        showMessage("Received message " + e.msg + " from " + this.id);
    };
};

var handleSessionDisconnection = function(session) {
    mSessions.remove(session);
    if (mSessions.empty()) {
        shutDown();
    }
};

var setUp = function() {
    showMessage("The presentation is connected");
};

var shutDown = function() {
    showMessage("No controllers, please connect.");
};
```


-- 
GitHub Notif of comment by avayvod
See 
https://github.com/w3c/presentation-api/issues/19#issuecomment-109818596

Received on Monday, 8 June 2015 00:39:11 UTC