diff options
author | Forrest L Norvell <forrest@npmjs.com> | 2014-10-03 10:50:55 +0400 |
---|---|---|
committer | Forrest L Norvell <forrest@npmjs.com> | 2014-10-04 07:17:05 +0400 |
commit | 7bee0429066fedcc9e6e962c043eb740b3792809 (patch) | |
tree | c909623f8dc6b550f5894dd2fb2ffff242ffb110 /node_modules/inflight | |
parent | 3bff494f4abf17d6d7e0e4a3a76cf7421ecec35a (diff) |
inflight@1.0.4
callbacks can take an arbitrary number of parameters
Diffstat (limited to 'node_modules/inflight')
-rw-r--r-- | node_modules/inflight/inflight.js | 19 | ||||
-rw-r--r-- | node_modules/inflight/package.json | 10 | ||||
-rw-r--r-- | node_modules/inflight/test.js | 22 |
3 files changed, 42 insertions, 9 deletions
diff --git a/node_modules/inflight/inflight.js b/node_modules/inflight/inflight.js index eeaf43491..8bc96cbd3 100644 --- a/node_modules/inflight/inflight.js +++ b/node_modules/inflight/inflight.js @@ -14,20 +14,31 @@ function inflight (key, cb) { } } -function makeres(key) { - return once(function RES (error, data) { +function makeres (key) { + return once(function RES () { var cbs = reqs[key] var len = cbs.length + var args = slice(arguments) for (var i = 0; i < len; i++) { - cbs[i](error, data) + cbs[i].apply(null, args) } if (cbs.length > len) { // added more in the interim. // de-zalgo, just in case, but don't call again. cbs.splice(0, len) - process.nextTick(RES.bind(this, error, data)) + process.nextTick(function () { + RES.apply(null, args) + }) } else { delete reqs[key] } }) } + +function slice (args) { + var length = args.length + var array = [] + + for (var i = 0; i < length; i++) array[i] = args[i] + return array +} diff --git a/node_modules/inflight/package.json b/node_modules/inflight/package.json index 9dabaaa9f..e0b63729c 100644 --- a/node_modules/inflight/package.json +++ b/node_modules/inflight/package.json @@ -1,6 +1,6 @@ { "name": "inflight", - "version": "1.0.3", + "version": "1.0.4", "description": "Add callbacks to requests in flight to avoid async duplication", "main": "inflight.js", "dependencies": { @@ -29,8 +29,8 @@ "license": "ISC", "readme": "# inflight\n\nAdd callbacks to requests in flight to avoid async duplication\n\n## USAGE\n\n```javascript\nvar inflight = require('inflight')\n\n// some request that does some stuff\nfunction req(key, callback) {\n // key is any random string. like a url or filename or whatever.\n //\n // will return either a falsey value, indicating that the\n // request for this key is already in flight, or a new callback\n // which when called will call all callbacks passed to inflightk\n // with the same key\n callback = inflight(key, callback)\n\n // If we got a falsey value back, then there's already a req going\n if (!callback) return\n\n // this is where you'd fetch the url or whatever\n // callback is also once()-ified, so it can safely be assigned\n // to multiple events etc. First call wins.\n setTimeout(function() {\n callback(null, key)\n }, 100)\n}\n\n// only assigns a single setTimeout\n// when it dings, all cbs get called\nreq('foo', cb1)\nreq('foo', cb2)\nreq('foo', cb3)\nreq('foo', cb4)\n```\n", "readmeFilename": "README.md", - "gitHead": "a10cb02457ed415c9019d185902ec3db85b03984", - "_id": "inflight@1.0.3", - "_shasum": "70374be8ef3316248f37fa81276b6b329b95ff49", - "_from": "inflight@>=1.0.3 <1.1.0" + "gitHead": "c7b5531d572a867064d4a1da9e013e8910b7d1ba", + "_id": "inflight@1.0.4", + "_shasum": "6cbb4521ebd51ce0ec0a936bfd7657ef7e9b172a", + "_from": "inflight@>=1.0.4 <1.1.0" } diff --git a/node_modules/inflight/test.js b/node_modules/inflight/test.js index 402d54205..2bb75b388 100644 --- a/node_modules/inflight/test.js +++ b/node_modules/inflight/test.js @@ -73,3 +73,25 @@ test('timing', function (t) { process.nextTick(log.bind(null, 'tick')) }) + +test('parameters', function (t) { + t.plan(8) + + var a = inf('key', function (first, second, third) { + t.equal(first, 1) + t.equal(second, 2) + t.equal(third, 3) + }) + t.ok(a, 'first returned cb function') + + var b = inf('key', function (first, second, third) { + t.equal(first, 1) + t.equal(second, 2) + t.equal(third, 3) + }) + t.notOk(b, 'second should get falsey inflight response') + + setTimeout(function () { + a(1, 2, 3) + }) +}) |