New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

auto-abort

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

auto-abort - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

8

index.js
var assert = require('assert')
module.exports = function (fn) {
module.exports = function (fn, abortHandler) {
var instance
if (abortHandler == null) abortHandler = function (inst) { return inst.abort() }
assert.strictEqual(typeof abortHandler, 'function', 'abortHandler should be a function')
return function () {
if (instance) instance.abort()
if (instance) abortHandler(instance)
instance = fn.apply(this, arguments)
assert.strictEqual(typeof instance.abort, 'function', 'returned instance does not have an abort function')
return instance
}
}
{
"name": "auto-abort",
"version": "1.0.1",
"description": "Only keep a single instance, `.abort()`ing previous instances on subsequent calls",
"version": "2.0.0",
"description": "Only keep a single instance, aborting previous instances on subsequent calls",
"main": "index.js",

@@ -24,3 +24,6 @@ "scripts": {

"singleton",
"http"
"http",
"cancel",
"requestAnimationFrame",
"raf"
],

@@ -27,0 +30,0 @@ "author": "Emil Bay <github@tixz.dk>",

# `auto-abort`
> Only keep a single instance, `.abort()`ing previous instances on subsequent calls
> Only keep a single instance, aborting previous instances on subsequent calls

@@ -28,8 +28,8 @@ ## Install

### `autoXhr(fn)`
### `autoAbort(fn, [abortHandler])`
Wraps `fn` with a function that will only keep a single instance of whatever `fn`
returns around, calling `.abort()` on the previous instance before calling
`fn` again. Unless you use something like `unassertify`, the return value of `fn`
will also be checked for an `.abort()` function on each call.
`fn` again. If you need to call another function than `.abort()`, use the
`abortHandler`.

@@ -39,7 +39,14 @@ #### `fn`

The function to wrap. The function MUST return an object with an `.abort()`
method, examples being `xhr` and `d3.json`
The function to wrap. Examples being `xhr` and `d3.json`
#### `abortHandler`
Type: `Function`<br>
Default: `function (instance) { return instance.abort() }`
Allows you to abort the instance manually, eg. if you need to do some other
logic around it or have an abort function of another name, eg. `.cancel()` like
on `requestAnimationFrame`
## License
[ISC](LICENSE.md)

@@ -30,13 +30,30 @@ 'use strict'

test('doesn\'t have abort function', function (assert) {
var auto = autoAbort(mock)
test('abortHandler', function (assert) {
assert.plan(1)
assert.throws(_ => auto())
assert.end()
var auto = autoAbort(mock, instance => instance.cancel())
auto(function () {
assert.pass()
})
auto(function () {
assert.pass()
})
function mock (cb) {
return {abort: true}
var hasAborted = false
setTimeout(function () {
if (!hasAborted) cb()
}, 0)
return {cancel: function () { hasAborted = true }}
}
})
test('throws', function (assert) {
assert.throws(_ => autoAbort(_ => {}, 'hello'))
assert.end()
})
test('real-world test', function (assert) {

@@ -43,0 +60,0 @@ assert.plan(1)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc