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
105 stars 7 forks source link

API Design: EventHandler vs ReadableStream #30

Closed backkem closed 10 months ago

backkem commented 11 months ago

While working on the LP2PQuicTransport interface in #28 I noticed that the WebTransport spec doesn't define a single EventHandler, instead a ReadableStream is used in many cases. As an expertise, here is a comparison based on the LP2PQuicTransportEvent event:

EventHandler (As currently defined)

interface LP2PReceiver : EventTarget  {
  attribute EventHandler ontransport;
  Promise<undefined> start();
};

Usage:

const receiver = new LP2PReceiver();

receiver.ontransport = e => {
    const transport = e.transport;
    await transport.ready;
}

await receiver.start();

ReadableStream (hypothetical)

interface LP2PReceiver {
  readonly attribute Promise<undefined> ready;
  /* a ReadableStream of LP2PQuicTransport objects */
  readonly attribute ReadableStream incomingTransports;
};

Usage:

const receiver = new LP2PReceiver();

for await (const transport of receiver.incomingTransports) {
    await transport.ready;
}

Note that this example assumes a LP2PReceiver is started on creation. The usage example assumes support for Async iterable using for await ... of in the absence of which the code becomes slightly more involved.

I opened this issue to discuss what kind of design we prefer and/or if mixed use seems appropriate. E.g.: combining onconnection with incomingTransports to match the precedent in their respective API, see #29.

backkem commented 10 months ago

33 aligns the WebTransport part of the API as described above; closing.