-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathappLoader.js
47 lines (40 loc) · 985 Bytes
/
appLoader.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
42
43
44
45
46
47
const BaseServiceLoader = require('./serviceLoader')
module.exports = class AppLoader {
constructor({ app, services = {}, ...loaderOptions }) {
this.options = {
app,
services,
loaderOptions,
loaders: new Map()
}
}
service(path) {
const { app } = this.options
const { ServiceLoader, ...loaderOptions } = {
ServiceLoader: BaseServiceLoader,
...this.options.loaderOptions,
...(this.options.services[path] || {})
}
const cachedLoader = this.options.loaders.get(path)
if (cachedLoader) {
return cachedLoader
}
const loader = new ServiceLoader({
...loaderOptions,
path,
app
})
this.options.loaders.set(path, loader)
return loader
}
async clear() {
const { loaders } = this.options
const promises = []
loaders.forEach((loader) => {
promises.push(loader.clear())
})
await Promise.all(promises)
loaders.clear()
return this
}
}