Projekt

Obecné

Profil

Stáhnout (842 Bajtů) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
'use strict'
2
module.exports = inflight
3
4
let Bluebird
5
try {
6
  Bluebird = require('bluebird')
7
} catch (_) {
8
  Bluebird = Promise
9
}
10
11
const active = {}
12
inflight.active = active
13
function 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
}