Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to use the promise-based API #91

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions src/RTCPeerConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,48 +179,69 @@ module.exports = function (daemon, wrtc) {
return dc
}

createOffer (cb, errCb, options) {
if (this._offer) return cb(this._offer)
_getCreateArgs (p1, p2, p3) {
if (p1 && p2) { // old API
return {
cb: p1,
errCb: p2,
options: p3
}
} else { // new Promise API
return {
options: p1
}
}
}
createOffer (p1, p2, p3) {
let {cb, errCb, options} = this._getCreateArgs(p1, p2, p3)
if (this._offer) {
if (cb) cb(this._offer)
Promise.resolve(this._offer)
}
return this._callRemote(
'createOffer',
`onSuccess, onFailure, ${JSON.stringify(options)}`,
(offer) => {
this._offer = offer
cb(offer)
if (cb) cb(offer)
}, errCb
)
}

createAnswer (cb, errCb, options) {
if (this._answer) return cb(this._answer)
createAnswer (p1, p2, p3) {
let {cb, errCb, options} = this._getCreateArgs(p1, p2, p3)
if (this._answer) {
if (cb) cb(this._answer)
Promise.resolve(this._answer)
}
return this._callRemote(
'createAnswer',
`onSuccess, onFailure, ${JSON.stringify(options)}`,
(offer) => {
this._answer = offer
cb(offer)
(answer) => {
this._answer = answer
if (cb) cb(answer)
}, errCb
)
}

setLocalDescription (desc, cb, errCb) {
this.localDescription = desc
this._callRemote(
return this._callRemote(
'setLocalDescription',
`new RTCSessionDescription(${JSON.stringify(desc)}), onSuccess, onFailure`,
cb, errCb)
}

setRemoteDescription (desc, cb, errCb) {
this.remoteDescription = desc
this._callRemote(
return this._callRemote(
'setRemoteDescription',
`new RTCSessionDescription(${JSON.stringify(desc)}), onSuccess, onFailure`,
cb, errCb)
}

addIceCandidate (candidate, cb, errCb) {
this._callRemote(
return this._callRemote(
'addIceCandidate',
`new RTCIceCandidate(${JSON.stringify(candidate)}), onSuccess, onFailure`,
cb, errCb)
Expand Down Expand Up @@ -270,11 +291,11 @@ module.exports = function (daemon, wrtc) {
})
var reqId = hat()
daemon.once(reqId, (res) => {
if (res.err && errCb) {
errCb(res.err)
if (res.err) {
if (errCb) errCb(res.err)
_reject(res.err)
} else if (!res.err && cb) {
cb(res.res)
} else {
if (cb) cb(res.res)
_resolve(res.res)
}
})
Expand Down