Skip to content

Commit

Permalink
Add async url resolve support
Browse files Browse the repository at this point in the history
  • Loading branch information
porsager committed Aug 8, 2022
1 parent 9977247 commit 654ca17
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pws.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function pws(url, protocols, WebSocket, options) {
, binaryType = null
, closed = false
, reconnectDelay = 0
, attempts = 0

const listeners = {}
const listenerHandlers = {}
Expand Down Expand Up @@ -116,12 +117,12 @@ function pws(url, protocols, WebSocket, options) {
pws.off = off('off', ons, onHandlers)
pws.once = (event, fn) => pws.on(event, fn, { once: true })

if (url)
connect()
url && Promise.resolve().then(connect)

return pws

function connect(url) {
async function connect(url) {
const attempt = ++attempts
closed = reconnecting = false
clearTimeout(reconnectTimer)

Expand All @@ -132,9 +133,12 @@ function pws(url, protocols, WebSocket, options) {

url && (pws.url = url)
url = typeof pws.url === 'function'
? pws.url(pws)
? (await pws.url(pws))
: pws.url

if (attempt !== attempts)
return

connection = browser
? protocols
? new WebSocket(url, protocols)
Expand Down

0 comments on commit 654ca17

Please sign in to comment.