W3C home > Mailing lists > Public > public-webrtc@w3.org > November 2012

[ACTION-59] Introduce lookup by id structures for streams and tracks.

From: Adam Bergkvist <adam.bergkvist@ericsson.com>
Date: Mon, 5 Nov 2012 14:36:18 +0100
Message-ID: <5097C0D2.7000705@ericsson.com>
To: "public-webrtc@w3.org" <public-webrtc@w3.org>
ACTION-59 is about moving away from a model where index is the only way 
to look up streams and tracks in 
PeerConnection.localStreams/remoteStreams and 
MediaStream.audioTracks/videoTracks.

I see two common cases how these stream and track collections will be used.
1) Lookup by id.
2) Iterate/loop through all the items in the collection.

Item 1) is easily solved by a getStreamById()/getTrackById() method. 
Item 2) could be achieved in a few ways and I would like to get some 
feedback on which way people in group prefer.

I don't think the "for (var p in collection)"-approach is something we 
want to use here. It won't give us the result we want since it includes 
additional properties added with a "collection.someProp = ..." 
assignment as well as inherited constants, methods and attributes.

Here are two options on how to solve item 2) (iteration) from the list 
above. I you have a better approach, feel free to suggest it.

A) forEachStream()/forEachTrack() method

You iterate through all the items by providing a callback to the 
forEachStream() method. The callback will be called synchronously for 
each item.

Example:
pc.localStreams.forEachStream(function (stream) {
     // use stream
});

We could have the callback return a boolean value which could be used to 
"break" the iteration if the desired item was found before the end was 
reached.


B) Keep the possibility to use indices for looping (but not guarantee 
the order)

This kind of looping may be more familiar to developers, but it exposes 
the index that may be problematic if used outside of the "looping 
context". The reason for proposing this as an alternative is that the 
video element does this for its track lists.

Example:
for (var i = 0; i < pc.localStreams.length; i++) {
     // use pc.localStreams[i]
}

What do people think?

/Adam
Received on Monday, 5 November 2012 13:36:42 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 5 November 2012 13:36:43 GMT