-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
111 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
<template> | ||
<h1>所有 demo 以网络请求为例,请打开 Devtool -> Network 查看效果</h1> | ||
<DebounceAsync /> | ||
<ThrottleAsync /> | ||
<ConcurrentAsync /> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent } from 'vue'; | ||
import DebounceAsync from './components/debounceAsync.vue'; | ||
import ThrottleAsync from './components/throttleAsync.vue'; | ||
import ConcurrentAsync from './components/concurrentAsync.vue'; | ||
export default defineComponent({ | ||
name: 'App', | ||
components: { | ||
DebounceAsync, | ||
ThrottleAsync | ||
} | ||
ThrottleAsync, | ||
ConcurrentAsync, | ||
}, | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<script lang="ts"> | ||
import { defineComponent } from 'vue'; | ||
import { concurrentAsync } from '../../src'; | ||
function rawApi<T>(data: T): Promise<T> { | ||
const delay = (1 + Math.random() * 3).toFixed(3); | ||
console.log('fetch start:', data); | ||
return fetch(`https://httpbin.org/delay/${delay}?t=${delay}`, { | ||
method: 'GET', | ||
mode: 'cors', | ||
}).then(() => data); | ||
} | ||
const concurrentedApi = concurrentAsync(rawApi, 3); | ||
export default defineComponent({ | ||
setup() { | ||
return { | ||
onRequest() { | ||
for (let i = 0; i < 7; i++) { | ||
rawApi(i).then(r => console.log('rez:', i, r)); | ||
} | ||
}, | ||
onConcurrentRequest() { | ||
for (let i = 0; i < 7; i++) { | ||
concurrentedApi(i).then(r => console.log('rez:', i, r)); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); | ||
</script> | ||
|
||
<template> | ||
<fieldset> | ||
<legend> | ||
concurrentAsync | ||
</legend> | ||
<p>一个限制最大并发的高阶函数</p> | ||
<button @click="onRequest">无限制并发请求</button> | ||
<button @click="onConcurrentRequest">限制并发请求</button> | ||
</fieldset> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
# async-utils | ||
# async-utils | ||
|
||
一个异步工具库,风格以高阶函数为主。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @description 限制最大并发数 | ||
*/ | ||
export default function concurrentAsync<T, P extends any[], R>( | ||
fn: (this: T, ...p: P) => Promise<R>, | ||
maxCount = 3, | ||
) { | ||
let that: T; | ||
|
||
type Resolve = (arg: R) => void; | ||
type Reject = (...ers: unknown[]) => void; | ||
type Holding = [P, Resolve, Reject]; | ||
|
||
const holding: Array<Holding> = []; | ||
|
||
let executing = 0; | ||
|
||
function drain() { | ||
if (/* holding.length === 0 || */ executing >= maxCount) return; | ||
const nextHolding = holding.shift(); | ||
if (nextHolding) { | ||
// console.log('\ndrain', executing); | ||
execTask(nextHolding); | ||
} | ||
} | ||
|
||
function execTask([rawParams, resolve, reject]: Holding) { | ||
executing++; | ||
// console.log('execTask', executing); | ||
fn.apply(that, rawParams) | ||
.then((...args) => { | ||
resolve(...args); | ||
executing--; | ||
drain(); | ||
}) | ||
.catch((...args) => { | ||
reject(...args); | ||
executing--; | ||
drain(); | ||
}); | ||
} | ||
|
||
return function concurrented(this: T, ...rawParams: P): Promise<R> { | ||
that = this; | ||
const defer = new Promise((r: Resolve, j: Reject) => { | ||
holding.push([rawParams, r, j]); | ||
}); | ||
drain(); | ||
return defer; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as debounceAsync } from './debounceAsync'; | ||
export { default as throttleAsync } from './throttleAsync'; | ||
export { default as concurrentAsync } from './concurrentAsync'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters