# request-event

> 防抖插件

Latest version **0.0.3** (published 2021-01-25) · ISC license · 0 weekly downloads

## Install

```sh
npm install request-event
pnpm add request-event
yarn add request-event
bun add request-event
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2021-01-25 |
| First published | 2021-01-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 22.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | jzyong |
| Maintainers | jzyong |
| Keywords | js |

## Links

- npm: https://www.npmjs.com/package/request-event
- npm.io page: https://npm.io/package/request-event

## Dependencies (2)

- [webpack-cli](https://npm.io/package/webpack-cli.md) ^4.4.0
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.12.5

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.3 (latest) — 2021-01-25
- 0.0.2 — 2021-01-25
- 0.0.1 — 2021-01-25

## README

### 前端请求防抖工具

相对于anti-shake-and-per这个包，移去了没啥用的权限控制

#### 模式
request-event中配置了两个模式：queue与timeout
<br/><br/>
queue: 排队模式，类似与锁机制，当一个请求发出后，只有调用了done函数标记结束，下一个请求才可以发起。
<br/><br/>
timeout: 倒计时模式，用户多次点击时，只有最后一次有效。与常见的setTimeout防抖机制一直

#### 演示两种模式的使用方式

~~~javascript
const queue = new RequestEvent('queue')
const timeout = new RequestEvent('timeout')

// 公共方法，模拟请求事件
const getArticleById = function() {
    return new Promise(function (resolve, reject) { 
        setTimeout(() => {
            resolve({code: '0001', data: []})                      
        }, 1000)     
    })
}

// 定义一个点击事件，演示queue模式
const click = function () {
    // 相对于anti-shake-and-per采用事件驱动模式反映运行状态，request-event采用try catch捕获异常，
    // request函数会返回一个Promise，所以使用catch函数也是可行的
    // 正常情况下你的请求代码没问题，那么抛出的异常只有The request event is running!
    try{
        queue.request(async done => {
            const result = await getArticleById()
            done()
        })
    } catch (e) {
        throw e
    }
}

click() // 此处会成功发出请求
click() // 此处不会发出请求，且抛出异常The request event is running!

// timeout模式
const click1 = function () {
    try{
        timeout.request(async done => {
            const result = await getArticleById()
            done()
        })
    } catch (e) {
        throw e
    }
}

click1()
click1() 
click1()
click1() 
click1()
click1() 
click1()
click1() 
click1()
click1() // 虽然请求多次，但只有这个请求是有效的
~~~

#### 其他
虽然request-event的目的是解决请求数据时的点击抖动的情况，但你完全可以用在请求数据之外一切需要避免抖动发送的情况

---
_Source: https://npm.io/package/request-event · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
