Comparing version 1.0.1 to 2.0.0
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) |
27
test.js
@@ -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) |
4472
63
51