[Pkg-javascript-commits] [node-inflight] 01/04: Import Upstream version 1.0.6

Praveen Arimbrathodiyil praveen at moszumanska.debian.org
Tue Oct 18 10:57:11 UTC 2016


This is an automated email from the git hooks/post-receive script.

praveen pushed a commit to branch master
in repository node-inflight.

commit 246fc0dbde7541f1e2c8b8eaea93edfa1326c41d
Author: Praveen Arimbrathodiyil <praveen at debian.org>
Date:   Tue Oct 18 15:00:19 2016 +0530

    Import Upstream version 1.0.6
---
 .travis.yml  |   8 +++
 LICENSE      |  15 +++++
 README.md    |  37 +++++++++++
 inflight.js  |  54 ++++++++++++++++
 package.json |  29 +++++++++
 test.js      | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 344 insertions(+)

diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..ee45280
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,8 @@
+sudo: false
+language: node_js
+node_js:
+  - '0.10'
+  - '0.12'
+  - '4'
+  - '5'
+  - '6'
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..05eeeb8
--- /dev/null
+++ b/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/README.md b/README.md
new file mode 100644
index 0000000..6dc8929
--- /dev/null
+++ b/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/inflight.js b/inflight.js
new file mode 100644
index 0000000..48202b3
--- /dev/null
+++ b/inflight.js
@@ -0,0 +1,54 @@
+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)
+    return null
+  } else {
+    reqs[key] = [cb]
+    return makeres(key)
+  }
+}
+
+function makeres (key) {
+  return once(function RES () {
+    var cbs = reqs[key]
+    var len = cbs.length
+    var args = slice(arguments)
+
+    // XXX It's somewhat ambiguous whether a new callback added in this
+    // pass should be queued for later execution if something in the
+    // list of callbacks throws, or if it should just be discarded.
+    // However, it's such an edge case that it hardly matters, and either
+    // choice is likely as surprising as the other.
+    // As it happens, we do go ahead and schedule it for later execution.
+    try {
+      for (var i = 0; i < len; i++) {
+        cbs[i].apply(null, args)
+      }
+    } finally {
+      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(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/package.json b/package.json
new file mode 100644
index 0000000..6084d35
--- /dev/null
+++ b/package.json
@@ -0,0 +1,29 @@
+{
+  "name": "inflight",
+  "version": "1.0.6",
+  "description": "Add callbacks to requests in flight to avoid async duplication",
+  "main": "inflight.js",
+  "files": [
+    "inflight.js"
+  ],
+  "dependencies": {
+    "once": "^1.3.0",
+    "wrappy": "1"
+  },
+  "devDependencies": {
+    "tap": "^7.1.2"
+  },
+  "scripts": {
+    "test": "tap test.js --100"
+  },
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/npm/inflight.git"
+  },
+  "author": "Isaac Z. Schlueter <i at izs.me> (http://blog.izs.me/)",
+  "bugs": {
+    "url": "https://github.com/isaacs/inflight/issues"
+  },
+  "homepage": "https://github.com/isaacs/inflight",
+  "license": "ISC"
+}
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..e361b4e
--- /dev/null
+++ b/test.js
@@ -0,0 +1,201 @@
+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')
+})
+
+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'))
+})
+
+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)
+  })
+})
+
+test('throw (a)', function (t) {
+  var calleda = false
+  var a = inf('throw', function () {
+    t.notOk(calleda)
+    calleda = true
+    throw new Error('throw from a')
+  })
+  t.ok(a, 'first returned cb function')
+
+  var calledb = false
+  var b = inf('throw', function () {
+    t.notOk(calledb)
+    calledb = true
+  })
+  t.notOk(b, 'second should get falsey inflight response')
+
+  setTimeout(function () {
+    t.throws(a, { message: 'throw from a' })
+    t.ok(calleda)
+    t.notOk(calledb)
+    var calledc = false
+    var c = inf('throw', function () {
+      calledc = true
+    })
+    t.ok(c, 'third returned cb function because it cleaned up')
+    c()
+    t.ok(calledc)
+    t.end()
+  })
+})
+
+test('throw (b)', function (t) {
+  var calleda = false
+  var a = inf('throw', function () {
+    t.notOk(calleda)
+    calleda = true
+  })
+  t.ok(a, 'first returned cb function')
+
+  var calledb = false
+  var b = inf('throw', function () {
+    t.notOk(calledb)
+    calledb = true
+    throw new Error('throw from b')
+  })
+  t.notOk(b, 'second should get falsey inflight response')
+
+  setTimeout(function () {
+    t.throws(a, { message: 'throw from b' })
+    t.ok(calleda)
+    t.ok(calledb)
+    var calledc = false
+    var c = inf('throw', function () {
+      calledc = true
+    })
+    t.ok(c, 'third returned cb function because it cleaned up')
+    c()
+    t.ok(calledc)
+    t.end()
+  })
+})
+
+test('throw (zalgo)', function (t) {
+  var calleda = false
+  var calledZalgo = false
+  var a = inf('throw', function () {
+    t.notOk(calleda)
+    calleda = true
+
+    var zalgo = inf('throw', function () {
+      t.notOk(calledZalgo)
+      calledZalgo = true
+    })
+    t.notOk(zalgo, 'zalgo should get falsey inflight response')
+    throw new Error('throw from a')
+  })
+  t.ok(a, 'first returned cb function')
+
+  var calledb = false
+  var b = inf('throw', function () {
+    t.notOk(calledb)
+    calledb = true
+  })
+  t.notOk(b, 'second should get falsey inflight response')
+
+  setTimeout(function () {
+    t.throws(a, { message: 'throw from a' })
+    t.ok(calleda)
+    t.notOk(calledb)
+    t.notOk(calledZalgo)
+    process.nextTick(function () {
+      t.ok(calledZalgo)
+      var calledc = false
+      var c = inf('throw', function () {
+        calledc = true
+      })
+      t.ok(c, 'third returned cb function because it cleaned up')
+      c()
+      t.ok(calledc)
+      t.end()
+    })
+  })
+})

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-inflight.git



More information about the Pkg-javascript-commits mailing list