File size: 1,136 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict';
const toJSONCallback = (m => /* c8 ignore start */ m.__esModule ? m.default : m /* c8 ignore stop */)(require('to-json-callback'));

const { dedent } = require('../utils.js');
const { js: jsHooks, code: codeHooks } = require('../hooks.js');

// REQUIRES INTEGRATION TEST
/* c8 ignore start */
module.exports = class Hook {
    constructor(interpreter, hooks = {}) {
        const { main, worker } = hooks;
        this.interpreter = interpreter;
        this.onWorker = main?.onWorker;
        // ignore onWorker as that's main only
        for (const key of jsHooks.slice(1))
            this[key] = worker?.[key];
        for (const key of codeHooks)
            this[key] = worker?.[key];
    }
    toJSON() {
        const hooks = {};
        // ignore onWorker as that's main only
        for (const key of jsHooks.slice(1)) {
            if (this[key]) hooks[key] = toJSONCallback(this[key]);
        }
        // code related: exclude `onReady` callback
        for (const key of codeHooks) {
            if (this[key]) hooks[key] = dedent(this[key]());
        }
        return hooks;
    }
}
/* c8 ignore stop */