1'use strict' 2module.exports = inflight 3 4let Bluebird 5try { 6 Bluebird = require('bluebird') 7} catch (_) { 8 Bluebird = Promise 9} 10 11const active = {} 12inflight.active = active 13function inflight (unique, doFly) { 14 return Bluebird.all([unique, doFly]).then(function (args) { 15 const unique = args[0] 16 const doFly = args[1] 17 if (Array.isArray(unique)) { 18 return Bluebird.all(unique).then(function (uniqueArr) { 19 return _inflight(uniqueArr.join(''), doFly) 20 }) 21 } else { 22 return _inflight(unique, doFly) 23 } 24 }) 25 26 function _inflight (unique, doFly) { 27 if (!active[unique]) { 28 active[unique] = (new Bluebird(function (resolve) { 29 return resolve(doFly()) 30 })) 31 active[unique].then(cleanup, cleanup) 32 function cleanup() { delete active[unique] } 33 } 34 return active[unique] 35 } 36} 37