123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- 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)
-
-
-
-
-
-
-
- try {
- for (var i = 0; i < len; i++) {
- cbs[i].apply(null, args)
- }
- } finally {
- if (cbs.length > len) {
-
-
- 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
- }
|