-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
41 lines (36 loc) · 922 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';
const { Deferred } = require('./lib/util');
module.exports = app => {
let cachedConfig = {};
let isApolloReady = false;
const apolloDeffered = new Deferred();
app.messenger.on('apollo:ConfigUpdated', config => {
cachedConfig = config;
if (!isApolloReady) {
isApolloReady = true;
apolloDeffered.resolve();
}
});
app.messenger.once('egg-ready', () => {
setTimeout(() => {
!isApolloReady && app.messenger.sendToAgent('apollo:ConfigUpdated');
}, 2000);
});
app.apollo = {
async getConfigAll () {
if (!isApolloReady) {
await apolloDeffered.promise;
}
return cachedConfig;
},
async getConfig (key) {
if (!key) {
throw new Error('Unable to query config with no key');
}
if (!isApolloReady) {
await apolloDeffered.promise;
}
return cachedConfig[key] || null;
}
};
};