WICG / local-peer-to-peer

↔️ Proposal for local communication between browsers without the aid of a server.
https://wicg.github.io/local-peer-to-peer/
Other
62 stars 6 forks source link

Order of operations in API design #14

Closed backkem closed 7 months ago

backkem commented 9 months ago

In the current API design, many objects are constructed as follows:

await peerA.connect({ /* ... */ });
peerA.addEventListener("message", function (event) { /* ... */ });

An active constructor is called and only afterwards the event listeners are attached. Technically, it is possible for a message to arrive before the corresponding listener is attached.

One way to avoid this is to specify that the user agent should buffer incoming events until the event listener is attached. However, it seems advisable to avoid this altogether and redesign the API as in the following sudo-code example:

let pc = new LP2PConnection( peerA );
pc.addEventListener("message", function (event) { /* ... */ });
await pc.start({ /* ... */ });

Examples of related APIs that do this include the Presentation, ORTC and p2p-webtransport APIs.

backkem commented 7 months ago

Considered closed by #23.