39 lines
702 B
JavaScript
39 lines
702 B
JavaScript
|
import axios from 'axios'
|
||
|
const queue = []
|
||
|
const running = []
|
||
|
const max = 3
|
||
|
|
||
|
async function run () {
|
||
|
if (queue.length === 0 || running.length === max) return
|
||
|
|
||
|
let fn = queue.shift()
|
||
|
if (typeof fn !== 'function') return run()
|
||
|
|
||
|
running.push(1)
|
||
|
await fn()
|
||
|
running.pop()
|
||
|
|
||
|
run()
|
||
|
}
|
||
|
|
||
|
export const push = async (key, url, runs, cb) => {
|
||
|
let fn = async () => {
|
||
|
runs(key)
|
||
|
let success = 0
|
||
|
let mp3 = ''
|
||
|
try {
|
||
|
let result = await axios({
|
||
|
method: 'post',
|
||
|
url: '/api/youtube/download',
|
||
|
data: { url }
|
||
|
})
|
||
|
mp3 = result.data.url
|
||
|
success = 2
|
||
|
} catch (err) {
|
||
|
success = -1
|
||
|
}
|
||
|
cb(key, success, mp3)
|
||
|
}
|
||
|
queue.push(fn)
|
||
|
run()
|
||
|
}
|