diff options
author | Forrest L Norvell <forrest@npmjs.com> | 2014-05-01 02:22:33 +0400 |
---|---|---|
committer | Forrest L Norvell <forrest@npmjs.com> | 2014-05-08 05:00:50 +0400 |
commit | 0e5c561cef9dfe9c057dc3deef58ee2291084ae1 (patch) | |
tree | 2c14794ca8a1f7e6f0cfb72087aad93f946554c4 /node_modules/inflight | |
parent | 6c49e2680c7644de7a15a6560f414a579f2ab461 (diff) |
break inflight management out into a module
Diffstat (limited to 'node_modules/inflight')
-rw-r--r-- | node_modules/inflight/LICENSE | 15 | ||||
-rw-r--r-- | node_modules/inflight/README.md | 37 | ||||
-rw-r--r-- | node_modules/inflight/inflight.js | 25 | ||||
-rw-r--r-- | node_modules/inflight/package.json | 35 | ||||
-rw-r--r-- | node_modules/inflight/test.js | 33 |
5 files changed, 145 insertions, 0 deletions
diff --git a/node_modules/inflight/LICENSE b/node_modules/inflight/LICENSE new file mode 100644 index 000000000..05eeeb88c --- /dev/null +++ b/node_modules/inflight/LICENSE @@ -0,0 +1,15 @@ +The ISC License + +Copyright (c) Isaac Z. Schlueter + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR +IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/node_modules/inflight/README.md b/node_modules/inflight/README.md new file mode 100644 index 000000000..6dc892917 --- /dev/null +++ b/node_modules/inflight/README.md @@ -0,0 +1,37 @@ +# inflight + +Add callbacks to requests in flight to avoid async duplication + +## USAGE + +```javascript +var inflight = require('inflight') + +// some request that does some stuff +function req(key, callback) { + // key is any random string. like a url or filename or whatever. + // + // will return either a falsey value, indicating that the + // request for this key is already in flight, or a new callback + // which when called will call all callbacks passed to inflightk + // with the same key + callback = inflight(key, callback) + + // If we got a falsey value back, then there's already a req going + if (!callback) return + + // this is where you'd fetch the url or whatever + // callback is also once()-ified, so it can safely be assigned + // to multiple events etc. First call wins. + setTimeout(function() { + callback(null, key) + }, 100) +} + +// only assigns a single setTimeout +// when it dings, all cbs get called +req('foo', cb1) +req('foo', cb2) +req('foo', cb3) +req('foo', cb4) +``` diff --git a/node_modules/inflight/inflight.js b/node_modules/inflight/inflight.js new file mode 100644 index 000000000..1fe279f9a --- /dev/null +++ b/node_modules/inflight/inflight.js @@ -0,0 +1,25 @@ +module.exports = inflight + +var reqs = Object.create(null) +var once = require('once') + +function inflight (key, cb) { + if (reqs[key]) { + reqs[key].push(cb) + return null + } else { + reqs[key] = [cb] + return makeres(key) + } +} + +function makeres(key) { + return once(res) + function res(error, data) { + var cbs = reqs[key] + delete reqs[key] + cbs.forEach(function(cb) { + cb(error, data) + }) + } +} diff --git a/node_modules/inflight/package.json b/node_modules/inflight/package.json new file mode 100644 index 000000000..263cc9872 --- /dev/null +++ b/node_modules/inflight/package.json @@ -0,0 +1,35 @@ +{ + "name": "inflight", + "version": "1.0.1", + "description": "Add callbacks to requests in flight to avoid async duplication", + "main": "inflight.js", + "dependencies": { + "once": "^1.3.0" + }, + "devDependencies": { + "tap": "^0.4.10" + }, + "scripts": { + "test": "tap test.js" + }, + "repository": { + "type": "git", + "url": "git://github.com/isaacs/inflight" + }, + "author": { + "name": "Isaac Z. Schlueter", + "email": "i@izs.me", + "url": "http://blog.izs.me/" + }, + "bugs": { + "url": "https://github.com/isaacs/inflight/issues" + }, + "homepage": "https://github.com/isaacs/inflight", + "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", + "_id": "inflight@1.0.1", + "_shasum": "01f6911821535243c790ac0f998f54e9023ffb6f", + "_from": "inflight@", + "_resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.1.tgz" +} diff --git a/node_modules/inflight/test.js b/node_modules/inflight/test.js new file mode 100644 index 000000000..28fc14503 --- /dev/null +++ b/node_modules/inflight/test.js @@ -0,0 +1,33 @@ +var test = require('tap').test +var inf = require('./inflight.js') + + +function req (key, cb) { + cb = inf(key, cb) + if (cb) setTimeout(function () { + cb(key) + cb(key) + }) + return cb +} + +test('basic', function (t) { + var calleda = false + var a = req('key', function (k) { + t.notOk(calleda) + calleda = true + t.equal(k, 'key') + if (calledb) t.end() + }) + t.ok(a, 'first returned cb function') + + var calledb = false + var b = req('key', function (k) { + t.notOk(calledb) + calledb = true + t.equal(k, 'key') + if (calleda) t.end() + }) + + t.notOk(b, 'second should get falsey inflight response') +}) |