11cb0ef41Sopenharmony_ci'use strict'
21cb0ef41Sopenharmony_cimodule.exports = inflight
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cilet Bluebird
51cb0ef41Sopenharmony_citry {
61cb0ef41Sopenharmony_ci  Bluebird = require('bluebird')
71cb0ef41Sopenharmony_ci} catch (_) {
81cb0ef41Sopenharmony_ci  Bluebird = Promise
91cb0ef41Sopenharmony_ci}
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst active = {}
121cb0ef41Sopenharmony_ciinflight.active = active
131cb0ef41Sopenharmony_cifunction inflight (unique, doFly) {
141cb0ef41Sopenharmony_ci  return Bluebird.all([unique, doFly]).then(function (args) {
151cb0ef41Sopenharmony_ci    const unique = args[0]
161cb0ef41Sopenharmony_ci    const doFly = args[1]
171cb0ef41Sopenharmony_ci    if (Array.isArray(unique)) {
181cb0ef41Sopenharmony_ci      return Bluebird.all(unique).then(function (uniqueArr) {
191cb0ef41Sopenharmony_ci        return _inflight(uniqueArr.join(''), doFly)
201cb0ef41Sopenharmony_ci      })
211cb0ef41Sopenharmony_ci    } else {
221cb0ef41Sopenharmony_ci      return _inflight(unique, doFly)
231cb0ef41Sopenharmony_ci    }
241cb0ef41Sopenharmony_ci  })
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  function _inflight (unique, doFly) {
271cb0ef41Sopenharmony_ci    if (!active[unique]) {
281cb0ef41Sopenharmony_ci      active[unique] = (new Bluebird(function (resolve) {
291cb0ef41Sopenharmony_ci        return resolve(doFly())
301cb0ef41Sopenharmony_ci      }))
311cb0ef41Sopenharmony_ci      active[unique].then(cleanup, cleanup)
321cb0ef41Sopenharmony_ci      function cleanup() { delete active[unique] }
331cb0ef41Sopenharmony_ci    }
341cb0ef41Sopenharmony_ci    return active[unique]
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci}
37