/ lib / modules / blockchain_process / blockchainProcess.js
blockchainProcess.js
 1  const ProcessWrapper = require('../../core/processes/processWrapper');
 2  const BlockchainClient = require('./blockchain');
 3  const i18n = require('../../core/i18n/i18n.js');
 4  const constants = require('../../constants');
 5  
 6  let blockchainProcess;
 7  
 8  class BlockchainProcess extends ProcessWrapper {
 9    constructor(options) {
10      super();
11      this.blockchainConfig = options.blockchainConfig;
12      this.client = options.client;
13      this.env = options.env;
14      this.isDev = options.isDev;
15  
16      i18n.setOrDetectLocale(options.locale);
17  
18      this.blockchainConfig.silent = true;
19      this.blockchain = BlockchainClient(
20        this.blockchainConfig,
21        this.client,
22        this.env,
23        this.blockchainReady.bind(this),
24        this.blockchainExit.bind(this)
25      );
26  
27      this.blockchain.run();
28    }
29  
30    blockchainReady() {
31      blockchainProcess.send({result: constants.blockchain.blockchainReady});
32    }
33  
34    blockchainExit() {
35      // tell our parent process that ethereum client has exited
36      blockchainProcess.send({result: constants.blockchain.blockchainExit});
37    }
38  
39    kill() {
40      this.blockchain.kill();
41    }
42  }
43  
44  process.on('message', (msg) => {
45    if (msg === 'exit') {
46      return blockchainProcess.kill();
47    }
48    if (msg.action === constants.blockchain.init) {
49      blockchainProcess = new BlockchainProcess(msg.options);
50      return blockchainProcess.send({result: constants.blockchain.initiated});
51    }
52  });