1 | var asyncSymbol = Symbol('asyncSymbol');
|
2 | var deferConfig = require('./defer').deferConfig;
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | function asyncConfig(promiseOrFunc) {
|
10 | if (typeof promiseOrFunc === 'function') {
|
11 | return deferConfig(function (config, original) {
|
12 | var release;
|
13 | function registerRelease(resolve) { release = resolve; }
|
14 | function callFunc() { return promiseOrFunc.call(config, config, original); }
|
15 | var promise = asyncConfig(new Promise(registerRelease).then(callFunc));
|
16 | promise.release = release;
|
17 | return promise;
|
18 | });
|
19 | }
|
20 | var promise = promiseOrFunc;
|
21 | promise.async = asyncSymbol;
|
22 | promise.prepare = function(config, prop, property) {
|
23 | if (promise.release) {
|
24 | promise.release();
|
25 | }
|
26 | return function() {
|
27 | return promise.then(function(value) {
|
28 | Object.defineProperty(prop, property, {value: value});
|
29 | });
|
30 | };
|
31 | };
|
32 | return promise;
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | function resolveAsyncConfigs(config) {
|
41 | var promises = [];
|
42 | var resolvers = [];
|
43 | (function iterate(prop) {
|
44 | var propsToSort = [];
|
45 | for (var property in prop) {
|
46 | if (prop.hasOwnProperty(property) && prop[property] != null) {
|
47 | propsToSort.push(property);
|
48 | }
|
49 | }
|
50 | propsToSort.sort().forEach(function(property) {
|
51 | if (prop[property].constructor === Object) {
|
52 | iterate(prop[property]);
|
53 | }
|
54 | else if (prop[property].constructor === Array) {
|
55 | prop[property].forEach(iterate);
|
56 | }
|
57 | else if (prop[property] && prop[property].async === asyncSymbol) {
|
58 | resolvers.push(prop[property].prepare(config, prop, property));
|
59 | promises.push(prop[property]);
|
60 | }
|
61 | });
|
62 | })(config);
|
63 | return Promise.all(promises).then(function() {
|
64 | resolvers.forEach(function(resolve) { resolve(); });
|
65 | return config;
|
66 | });
|
67 | }
|
68 |
|
69 | module.exports.asyncConfig = asyncConfig;
|
70 | module.exports.resolveAsyncConfigs = resolveAsyncConfigs;
|