Last change
on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
842 bytes
|
Line | |
---|
1 | '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 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.