diff options
author | Forrest L Norvell <forrest@npmjs.com> | 2014-10-02 04:16:41 +0400 |
---|---|---|
committer | Forrest L Norvell <forrest@npmjs.com> | 2014-10-02 04:16:41 +0400 |
commit | 00ef58025a1f52dfabf2c4dc3898621d16a6e062 (patch) | |
tree | 76e9902eb094c8dcff8ececb6926cadb8601d9e3 /node_modules/inflight | |
parent | e8dc53353798733df5fac96385ffa0aa3e5b908a (diff) |
inflight@1.0.3
We really, really, really don't want any race conditions in npm.
Diffstat (limited to 'node_modules/inflight')
-rw-r--r-- | node_modules/inflight/inflight.js | 26 | ||||
-rw-r--r-- | node_modules/inflight/package.json | 32 | ||||
-rw-r--r-- | node_modules/inflight/test.js | 42 |
3 files changed, 68 insertions, 32 deletions
diff --git a/node_modules/inflight/inflight.js b/node_modules/inflight/inflight.js index 1fe279f9a..eeaf43491 100644 --- a/node_modules/inflight/inflight.js +++ b/node_modules/inflight/inflight.js @@ -1,8 +1,9 @@ -module.exports = inflight - +var wrappy = require('wrappy') var reqs = Object.create(null) var once = require('once') +module.exports = wrappy(inflight) + function inflight (key, cb) { if (reqs[key]) { reqs[key].push(cb) @@ -14,12 +15,19 @@ function inflight (key, cb) { } function makeres(key) { - return once(res) - function res(error, data) { + return once(function RES (error, data) { var cbs = reqs[key] - delete reqs[key] - cbs.forEach(function(cb) { - cb(error, data) - }) - } + var len = cbs.length + for (var i = 0; i < len; i++) { + cbs[i](error, data) + } + 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)) + } else { + delete reqs[key] + } + }) } diff --git a/node_modules/inflight/package.json b/node_modules/inflight/package.json index f4e294aad..9dabaaa9f 100644 --- a/node_modules/inflight/package.json +++ b/node_modules/inflight/package.json @@ -1,10 +1,11 @@ { "name": "inflight", - "version": "1.0.1", + "version": "1.0.3", "description": "Add callbacks to requests in flight to avoid async duplication", "main": "inflight.js", "dependencies": { - "once": "^1.3.0" + "once": "^1.3.0", + "wrappy": "1" }, "devDependencies": { "tap": "^0.4.10" @@ -26,25 +27,10 @@ }, "homepage": "https://github.com/isaacs/inflight", "license": "ISC", - "_id": "inflight@1.0.1", - "_shasum": "01f6911821535243c790ac0f998f54e9023ffb6f", - "_from": "inflight@~1.0.1", - "_npmVersion": "1.4.9", - "_npmUser": { - "name": "isaacs", - "email": "i@izs.me" - }, - "maintainers": [ - { - "name": "isaacs", - "email": "i@izs.me" - } - ], - "dist": { - "shasum": "01f6911821535243c790ac0f998f54e9023ffb6f", - "tarball": "http://registry.npmjs.org/inflight/-/inflight-1.0.1.tgz" - }, - "directories": {}, - "_resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.1.tgz", - "readme": "ERROR: No README data found!" + "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" } diff --git a/node_modules/inflight/test.js b/node_modules/inflight/test.js index 28fc14503..402d54205 100644 --- a/node_modules/inflight/test.js +++ b/node_modules/inflight/test.js @@ -31,3 +31,45 @@ test('basic', function (t) { t.notOk(b, 'second should get falsey inflight response') }) + +test('timing', function (t) { + var expect = [ + 'method one', + 'start one', + 'end one', + 'two', + 'tick', + 'three' + ] + var i = 0 + + function log (m) { + t.equal(m, expect[i], m + ' === ' + expect[i]) + ++i + if (i === expect.length) + t.end() + } + + function method (name, cb) { + log('method ' + name) + process.nextTick(cb) + } + + var one = inf('foo', function () { + log('start one') + var three = inf('foo', function () { + log('three') + }) + if (three) method('three', three) + log('end one') + }) + + method('one', one) + + var two = inf('foo', function () { + log('two') + }) + if (two) method('one', two) + + process.nextTick(log.bind(null, 'tick')) +}) |