1 | 'use strict';
|
2 | const merge = require('./merge');
|
3 | const knownHookEvents = require('./known-hook-events');
|
4 |
|
5 | module.exports = (instances, methods) => {
|
6 | const handlers = instances.map(instance => instance.defaults.handler);
|
7 | const size = instances.length - 1;
|
8 |
|
9 | let options = {};
|
10 | const hooks = {};
|
11 | for (const instance of instances) {
|
12 | options = merge({}, options, instance.defaults.options);
|
13 |
|
14 | const instanceHooks = instance.defaults.options.hooks;
|
15 | for (const name of knownHookEvents) {
|
16 | if (hooks[name]) {
|
17 | hooks[name] = hooks[name].concat(instanceHooks[name]);
|
18 | } else {
|
19 | hooks[name] = [...instanceHooks[name]];
|
20 | }
|
21 | }
|
22 | }
|
23 |
|
24 | options.hooks = hooks;
|
25 |
|
26 | return {
|
27 | methods,
|
28 | options,
|
29 | handler: (options, next) => {
|
30 | let iteration = -1;
|
31 | const iterate = options => handlers[++iteration](options, iteration === size ? next : iterate);
|
32 |
|
33 | return iterate(options);
|
34 | }
|
35 | };
|
36 | };
|