peerjs icon indicating copy to clipboard operation
peerjs copied to clipboard

feat: Promise-like API for PeerJS

Open jonasgloning opened this issue 2 years ago • 3 comments

Hey @WofWca, you recently proposed a Promise-based API in https://github.com/peers/peerjs/issues/924#issuecomment-1694381633.

This is a quick draft of how this could look like. Is this roughly how you would imagine it?

(Only for Peer, connections are not included yet).

### Implementation
- [x] `private then: (onfulfilled?: (value: IDataConnection) => any, onrejected?: (reason: PeerError<DataConnectionErrorType>) => any,) => void;`
- [ ] Mediastream
### Documentation
- [ ] Inline 
- [ ] Website

jonasgloning avatar Aug 28 '23 10:08 jonasgloning

Hey @WofWca, I just added add await peer.connect(...):

const peer = new Peer();
peer.once("open", id => {
  const conn = peer.connect(remote_id)
  conn.once("open, () => {
   /* connection established */
  })
})

should be equivalent to

const peer = await new Peer();
const conn = await peer.connect(remote_id);
/* connection established */

Example with error handling: https://github.com/peers/peerjs/blob/bee2bdbcfc4c11dfd41f8e6f10dd6d225d03f105/e2e/peer/peer-unavailable.async.html#L28-L38


I'd love a quick review, if you have the time, to see if this meets your expectations.

jonasgloning avatar Sep 03 '23 09:09 jonasgloning

Thank you for your feedback! Please let me know if there's anything I can do to incentivize you to review future pull-requests.

I favor making Peer and DataConnection thenable on their own instead of creating a new method that returns a Promise, as it enables the setting of event listeners in a single step:

const peer = await new Peer().on("call", ...).on("disconnected", ...).once("open", ...)
const conn = await peer.connect().on("data", ...)

jonasgloning avatar Sep 07 '23 14:09 jonasgloning

Sorry for such a late response. You asked a tough question.

if there's anything I can do to incentivize you to review future pull-requests

I don't think there is anything specific. I'm not that involved in the PeerJS project. I don't use it too much, and I don't know the codebase and the domain well, so I probably won't be the most productive person for most of the MRs.

WofWca avatar Nov 20 '23 10:11 WofWca