Skip to content

Commit b9d4b67

Browse files
committed
Rename dropIfQueueFull to dropWhenReachLimit and uddate README
1 parent ab4a14e commit b9d4b67

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ Wrap an [async](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc
1818
8. Well tested.
1919
9. Optional parameter, `inheritPreErr`. If current task is waiting for previous tasks, set it as `true` to inherit the error of the previous task and the task will not be executed and throw a custom error `new PreviousError(task.preError.message ?? task.preError)`. If this parameter is omitted or set as `false`, the task will continue whether previous tasks happen errors or not.
2020
10. Optional parameter, `noBlockCurr`. Set it as `true` to forcibly execute the current task in the another (microtask) execution of the event loop. This is useful if you pass a sync function as the first task but do not want it to block the current event loop.
21+
11. Optional parameter, `dropWhenReachLimit`. Set it as `true`. Then it will be dropped when it is called but the system detects the queue concurrency limit is reached. It is like a kind of throttle mechanism but not time interval based. The dropped function call will not be really executed and will throw a execption whose message is `QueueIsFull` and you need to catch it.
2122

2223
## Installation
2324

2425
This package includes two builds.
2526

2627
- ES6 build (ES2015) with CommonJS module for `main` build in package.json.
27-
- ES6 build (ES2015) with ES6 module for `module` build. Some tools will follow the `module` field in `package.json`, like Rollup, Webpack, or Parcel. It is good to let build tools can tree-shake this module build to import only the code they need.
28+
- ES6 build (ES2015) with ES6 module for `module` build. Some tools will follow the `module` field in `package.json`, like Rollup, Webpack, or Parcel.
2829

2930
Either `npm install d4c-queue` or `yarn add d4c-queue`. Then import this package.
3031

@@ -126,18 +127,18 @@ d4c.apply(syncFun, { args: ['syncFun_arg1'] })
126127

127128
#### Concurrency mode
128129

129-
Is it useful for rate-limiting tasks. For example, setup some concurrency limit to avoid send GitHub GraphQL API requests too fast, since it has rate limits control.
130+
Is it useful for rate-limiting or throttling tasks. For example, setup some concurrency limit to avoid send GitHub GraphQL API requests too fast, since it has rate limits control.
130131

131132
Default concurrency limit of D4C instance is `1` in this library.
132133

133134
Usage:
134135

135136
```ts
136137
/** change concurrency limit applied on default queues */
137-
const d4c = new D4C([{ concurrency: { limit: 100 }}])
138+
const d4c = new D4C([{ concurrency: { limit: 100 } }])
138139

139140
/** setup concurrency for specific queue: "2" */
140-
const d4c = new D4C([{ concurrency: { limit: 100, tag: '2' }}])
141+
const d4c = new D4C([{ concurrency: { limit: 100, tag: '2' } }])
141142
```
142143

143144
You can adjust concurrency via `setConcurrency`.
@@ -151,6 +152,8 @@ d4c.setConcurrency([{ limit: 10 }])
151152
d4c.setConcurrency([{ limit: 10, tag: 'queue2' }])
152153
```
153154

155+
When this async task function is called and the system detects the concurrency limit is reached, this tasks will not be really executed and will be enqueued. If you want to drop this task function call, you can set `dropWhenReachLimit` option when wrapping/applying the task function. e.g. `d4c.wrap(taskFun, { dropWhenReachLimit: true })`
156+
154157
### Decorators usage
155158

156159
#### Synchronization mode
@@ -188,7 +191,7 @@ class TestController {
188191
@concurrent
189192
static async fetchData(url: string) {}
190193

191-
@concurrent({ tag: '2' })
194+
@concurrent({ tag: '2', dropWhenReachLimit: true })
192195
async fetchData2(url: string) {}
193196

194197
/** You can still use @synchronized, as long as
@@ -453,6 +456,7 @@ function concurrent(option?: {
453456
tag?: string | symbol
454457
inheritPreErr?: boolean
455458
noBlockCurr?: boolean
459+
dropWhenReachLimit?: boolean
456460
}) {}
457461
```
458462
@@ -467,7 +471,7 @@ Example:
467471
@concurrent
468472
@concurrent()
469473
@concurrent({ tag: "world", inheritPreErr: true })
470-
@concurrent({ inheritPreErr: true, noBlockCurr: true })
474+
@concurrent({ inheritPreErr: true, noBlockCurr: true, dropWhenReachLimit: true })
471475

472476
```
473477
@@ -490,10 +494,10 @@ usage:
490494
const d4c = new D4C()
491495

492496
/** concurrency limit 500 applied on default queues */
493-
const d4c = new D4C([{ concurrency: { limit: 500 }}])
497+
const d4c = new D4C([{ concurrency: { limit: 500 } }])
494498

495499
/** setup concurrency for specific queue: "2" */
496-
const d4c = new D4C([{ concurrency: { limit: 100, tag: '2' }}])
500+
const d4c = new D4C([{ concurrency: { limit: 100, tag: '2' } }])
497501
```
498502
499503
- setConcurrency
@@ -513,6 +517,7 @@ public wrap<T extends IAnyFn>(
513517
tag?: string | symbol;
514518
inheritPreErr?: boolean;
515519
noBlockCurr?: boolean;
520+
dropWhenReachLimit?: boolean;
516521
}
517522
)
518523
```
@@ -530,6 +535,7 @@ public apply<T extends IAnyFn>(
530535
tag?: string | symbol;
531536
inheritPreErr?: boolean;
532537
noBlockCurr?: boolean;
538+
dropWhenReachLimit?: boolean;
533539
args?: Parameters<typeof func>;
534540
}
535541
)

src/lib/D4C.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,9 +846,9 @@ test("Instance usage: option inheritPreErr enable: task2 inherit task1's error i
846846
t.is(error.message, 'some_error')
847847
})
848848

849-
test('Instance usage: test option dropIfQueueFull', async (t) => {
849+
test('Instance usage: test option dropWhenReachLimit', async (t) => {
850850
const d4c = new D4C([{ concurrency: { limit: 2 } }])
851-
const fn1 = d4c.wrap(timeout, { dropIfQueueFull: true })
851+
const fn1 = d4c.wrap(timeout, { dropWhenReachLimit: true })
852852

853853
let error = null
854854
try {

src/lib/D4C.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ function checkIfDecoratorOptionObject(obj: any): boolean {
214214
(typeof obj.inheritPreErr === 'boolean' ||
215215
obj.inheritPreErr === undefined) &&
216216
(typeof obj.noBlockCurr === 'boolean' || obj.noBlockCurr === undefined) &&
217-
(typeof obj.dropIfQueueFull === 'boolean' ||
218-
obj.dropIfQueueFull === undefined) &&
217+
(typeof obj.dropWhenReachLimit === 'boolean' ||
218+
obj.dropWhenReachLimit === undefined) &&
219219
checkTag(obj.tag)
220220
) {
221221
return true
@@ -243,7 +243,7 @@ export function concurrent(option?: {
243243
tag?: string | symbol
244244
inheritPreErr?: boolean
245245
noBlockCurr?: boolean
246-
dropIfQueueFull?: boolean
246+
dropWhenReachLimit?: boolean
247247
}): MethodDecoratorType
248248
export function concurrent(
249249
targetOrOption?: any,
@@ -333,7 +333,7 @@ function _q<T extends IAnyFn>(
333333
tag?: QueueTag
334334
inheritPreErr?: boolean
335335
noBlockCurr?: boolean
336-
dropIfQueueFull?: boolean
336+
dropWhenReachLimit?: boolean
337337
}
338338
): (...args: Parameters<typeof func>) => Promise<UnwrapPromise<typeof func>> {
339339
return async function (...args: any[]): Promise<any> {
@@ -386,7 +386,7 @@ function _q<T extends IAnyFn>(
386386
let err: Error
387387
let task: Task
388388
if (taskQueue.runningTask === taskQueue.concurrency) {
389-
if (!option?.dropIfQueueFull) {
389+
if (!option?.dropWhenReachLimit) {
390390
const promise = new Promise(function (resolve) {
391391
task = {
392392
unlock: resolve,
@@ -538,7 +538,7 @@ export class D4C {
538538
tag?: string | symbol
539539
inheritPreErr?: boolean
540540
noBlockCurr?: boolean
541-
dropIfQueueFull?: boolean
541+
dropWhenReachLimit?: boolean
542542
args?: Parameters<typeof func>
543543
}
544544
): Promise<UnwrapPromise<typeof func>> {
@@ -553,7 +553,7 @@ export class D4C {
553553
tag?: string | symbol
554554
inheritPreErr?: boolean
555555
noBlockCurr?: boolean
556-
dropIfQueueFull?: boolean
556+
dropWhenReachLimit?: boolean
557557
}
558558
): (...args: Parameters<typeof func>) => Promise<UnwrapPromise<typeof func>> {
559559
if (!option || checkTag(option.tag)) {

0 commit comments

Comments
 (0)