Skip to content

Commit 26852a9

Browse files
committed
device retry improvement
1 parent e3096cb commit 26852a9

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

src/library/wrapper.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ class Wrapper extends EventEmitter {
121121
if (this.#lastDeviceId)
122122
throw new constants.NoDeviceError('No device specified.')
123123

124-
const activeDevices = this.#lastDevices.filter(device => device.type !== 'Speaker')
124+
const devices = this.#lastDevices.filter(device => device.type !== 'Speaker')
125125

126-
if (activeDevices.length > 0)
127-
this.#setDevices(activeDevices[0].id, activeDevices)
126+
if (devices.length > 0)
127+
this.#setDevices(devices.find(device => device.is_active)?.id, devices)
128128
}
129129

130130
path = `${path}${path.includes('?') ? '&' : '?'}`
@@ -140,11 +140,12 @@ class Wrapper extends EventEmitter {
140140
if (response === constants.API_NOT_FOUND_RESPONSE)
141141
throw new constants.NoDeviceError('No device available.')
142142
} else if (!retried) {
143-
await new Promise(resolve => setTimeout(resolve, 3000))
143+
await new Promise(resolve => setTimeout(resolve, 5000))
144144

145-
const freshDevices = (await connector.callSpotifyApi('me/player/devices')).devices ?? []
145+
const freshDevices = ((await connector.callSpotifyApi('me/player/devices')).devices ?? []).filter(device => device.type !== 'Speaker')
146146

147-
this.#setDevices(this.#lastDeviceId, freshDevices)
147+
if (freshDevices.length > 0)
148+
this.#setDevices(freshDevices.find(device => device.id === this.#lastDeviceId)?.id ?? freshDevices.find(device => device.is_active)?.id, freshDevices)
148149

149150
return this.#deviceCall(path.slice(0, -1), options, freshDevices.find(device => device.is_active)?.id ?? deviceId, true)
150151
} else

0 commit comments

Comments
 (0)