-
-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support AbortSignal to listen to execution abortion #48
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,7 +5,7 @@ export class AbortError extends Error { | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
export default function pThrottle({limit, interval, strict, onDelay}) { | ||||||||||
export default function pThrottle({limit, interval, strict, onDelay, signal}) { | ||||||||||
if (!Number.isFinite(limit)) { | ||||||||||
throw new TypeError('Expected `limit` to be a finite number'); | ||||||||||
} | ||||||||||
|
@@ -109,6 +109,10 @@ export default function pThrottle({limit, interval, strict, onDelay}) { | |||||||||
}, | ||||||||||
}); | ||||||||||
|
||||||||||
if (signal) { | ||||||||||
signal.addEventListener('abort', throttled.abort); | ||||||||||
} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You also need to use
Comment on lines
+112
to
+114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
return throttled; | ||||||||||
}; | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,38 @@ await throttled(); | |
//=> Executing... | ||
``` | ||
|
||
##### signal | ||
|
||
Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | ||
|
||
An optional signal to listen for abort events. If the signal becomes aborted, all pending and future calls are rejected with a `pThrottle.AbortError` error. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the correct behavior. It should reject with |
||
|
||
You can abort the promises using an [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController): | ||
|
||
```js | ||
import pThrottle from 'p-throttle'; | ||
|
||
const abortController = new AbortController(); | ||
const throttle = pThrottle({ | ||
limit: 2, | ||
interval: 1000, | ||
signal: abortController.signal | ||
}); | ||
|
||
const throttled = throttle(() => { | ||
console.log('Executing...'); | ||
}); | ||
|
||
await throttled(); | ||
await throttled(); | ||
abortController.abort(); | ||
let promise = throttled(); | ||
await promise; | ||
//=> Executing... | ||
//=> Executing... | ||
//=> Promise rejected with AbortError (DOMException) | ||
``` | ||
|
||
### throttle(function_) | ||
|
||
Returns a throttled version of `function_`. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,6 +151,30 @@ test('can be aborted', async t => { | |
t.true(end() < 100); | ||
}); | ||
|
||
test('can listen to AbortSignal to abort execution', async t => { | ||
const limit = 1; | ||
const interval = 10_000; // 10 seconds | ||
const end = timeSpan(); | ||
const abortController = new AbortController(); | ||
const throttled = pThrottle({limit, interval, signal: abortController.signal})(async x => x); | ||
|
||
const one = await throttled(1); | ||
const promise = throttled(2); | ||
abortController.abort(); | ||
let error; | ||
let endValue; | ||
try { | ||
endValue = await promise; | ||
} catch (error_) { | ||
error = error_; | ||
} | ||
Comment on lines
+166
to
+170
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
|
||
t.true(error instanceof AbortError); | ||
t.true(end() < 100); | ||
t.true(one === 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
t.true(endValue === undefined); | ||
}); | ||
|
||
test('can be disabled', async t => { | ||
let counter = 0; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs import statement