1 | 'use strict'
|
2 |
|
3 | const { check } = require('./mapping')
|
4 | const {
|
5 | $configuration,
|
6 | $configurationRequests,
|
7 | $mappingChecked,
|
8 | $requestPromise
|
9 | } = require('./symbols')
|
10 |
|
11 | async function checkMappings (configuration, mappings) {
|
12 | for await (const mapping of mappings) {
|
13 | if (!mapping[$mappingChecked]) {
|
14 | await check(configuration, mapping)
|
15 | }
|
16 | }
|
17 | }
|
18 |
|
19 | module.exports = class IConfiguration {
|
20 | constructor (configuration) {
|
21 | this[$configuration] = configuration
|
22 | }
|
23 |
|
24 | get handlers () {
|
25 | return Object.assign({}, this[$configuration].handlers)
|
26 | }
|
27 |
|
28 | get mappings () {
|
29 | return [].concat(this[$configuration].mappings)
|
30 | }
|
31 |
|
32 | async setMappings (mappings, request) {
|
33 | const configuration = this[$configuration]
|
34 | await checkMappings(configuration, mappings)
|
35 | const configurationRequests = configuration[$configurationRequests]
|
36 | const requestPromise = request[$requestPromise]
|
37 | const otherRequestsPromises = configurationRequests.promises.filter(promise => promise !== requestPromise)
|
38 | configurationRequests.hold = Promise.all(otherRequestsPromises)
|
39 | .then(() => {
|
40 | configuration.mappings = mappings
|
41 | })
|
42 | return configurationRequests.hold
|
43 | }
|
44 | }
|