Git Product home page Git Product logo

wx-queue-request's Introduction

wx-queue-request

Build Status

管理微信小程序 wx.request 方法的并发数,解决请求数大于 10 时,直接不请求的问题。如果需要 wx.request 方法支持 Promise,可以使用 wx-promise-request 库哦。

下载

由于小程序不支持 npm,所以直接右键保存 index.js 文件即可。

使用

在 app.js 引入并执行即可

import { queue } from './wx-queue-request'
queue()

// 因为请求队列被管理
// 即使并发请求数超过 10 也不会报错了
wx.request({url: 'test.php'})
wx.request({url: 'test.php'})
wx.request({url: 'test.php'})
wx.request({url: 'test.php'})
wx.request({url: 'test.php'})
wx.request({url: 'test.php'})
wx.request({url: 'test.php'})
wx.request({url: 'test.php'})
wx.request({url: 'test.php'})
wx.request({url: 'test.php'})
wx.request({url: 'test.php'})

API

queue([concurrency])

直接对全局的 wx.request 进行封装,调用后 wx.request 即会管理队列。并发数默认为 10。

import { queue } from './wx-queue-request'
queue(5) // 指定并发数为 5

wx.request({url: 'test.php'})

queueRequest(request, [concurrency])

对指定请求函数进行封装,返回被管理队列的函数,并发数 concurrency 默认为 10。

import { queueRequest } from './wx-queue-request'

export default queueRequest(wx.request, 10)

License

MIT

wx-queue-request's People

Contributors

zhengjunxin avatar

Stargazers

ig avatar ShuaiKang Zhang avatar  avatar ershaoye avatar  avatar  avatar  avatar 刘阳 avatar  avatar Yenbs avatar  avatar  avatar 琚致远 / Zhiyuan Ju avatar nemo avatar  avatar Ming avatar excx.app avatar  avatar WLEX avatar 强大大 avatar  avatar  avatar Tenvi avatar 渣臻 avatar  avatar gamedevil avatar  avatar TT avatar Changsheng Zhu avatar darwen Zhao avatar Shawn Wang avatar demerfo avatar  avatar  avatar Sam Zeng avatar  avatar ufo1001 avatar Jess avatar Ray avatar zxin avatar king avatar Witcher Mo avatar JimmyLv_吕立青 avatar 紫升 avatar neewbee avatar Chen Xihong avatar Christopher Tsui avatar hbc avatar Arisono avatar peterwang avatar Kai Wu avatar vtanrun avatar  avatar RicardoZhou avatar Dylan Li avatar fnsoxt avatar wowge avatar idSong avatar nebel avatar stevensunzh avatar lemonhall avatar  avatar 吴家荣 avatar  avatar  avatar 南国老符 avatar  avatar

Watchers

James Cloos avatar  avatar 南国老符 avatar

wx-queue-request's Issues

您好问下您一些初级的问题。

const onlyOnce = (fn) => (...args) => {
    if (fn === null) {
        throw new Error('Callback was already called')
    }
    const callFn = fn
    fn = null
    return callFn(...args)
}

JS新手。。。没有看懂您这块的写法。尤其是 (fn) => (...args) =>{/code/}这段这是一个参数是fn的函数。。然后后面是在做什么?您能大概说说吗?
另外还想问下fn = null这个在这个代码块里置null不是应该只影响这个代码块吗,并不会影响外面传进来的fn吧?
之前一直在做java,js太生疏了,请您斧正。

使用中,有一些不理解

import { queueRequest } from './wx-queue-request/index.js';
export default queueRequest(wx.downloadFile, 5)

我希望用次项目管理我们的下载以不超过微信的十个总数限制,但是实际使用仍然有问题

Object {errMsg: "downloadFile:fail createDownloadTask:fail exceed max download connection count 10"}
errMsg
:
"downloadFile:fail createDownloadTask:fail exceed max download connection count 10"
__proto__
:
Object

wx.request 每次调用都会生成队列

Object.defineProperty(wx, 'request', {
get() {
return queueRequest(request, concurrency)
}
})
每次执行wx.request 都会触发queueRequest,执行const queue = q((task, callback) => task(callback), concurrency) 生成新的队列管理器

image

看起来全局调用会有这样的问题,不知是否理解正确

Error: Callback was already called

Error: Callback was already called
    at queue.js? [sm]:13
    at Object.obj.complete (index.js? [sm]:13)
    at Object.success (sound.js? [sm]:707)
    at Object.recv (common.js? [sm]:627)
    at BleManager.doCallback (bt.js? [sm]:219)
    at Function.<anonymous> (bt.js? [sm]:288)
    at [publib]:4
    at Timeout._onTimeout ([publib]:4)
    at ontimeout (timers.js:471)
    at tryOnTimeout (timers.js:306)

容易遇到这个错误,看你在别的issue中的解释,这个错误是不是可以忽略呢?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.