15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const path = require('path');
const fs = require('fs');
const RendererJob = require('./renderer');
const ChildJob = require('./child/pool');
const Ps = require('../ps');
const Loader = require('../loader');
 
class Jobs  {
  constructor() {
    this.type;
    this.dev;
    this.path;
    this.instance;
    this.child;
    this.childOptions;
    this.renderer;
    this.winOptions;
  }
 
  /**
   * 创建 job
   */
  create (name, opt = {}) {
    this.type = opt.type || 'child';
    this.dev = opt.dev || false;
    this.winOptions = opt.winOptions || {};
    this.childOptions = opt.childOptions || {};
    this.path = opt.path || null;
 
    const isAbsolute = path.isAbsolute(this.path);
    if (!isAbsolute) {
      this.path = path.join(Ps.getBaseDir(), this.path);
    }
    const filepath = Loader.resolveModule(this.path);
 
    if (!fs.existsSync(filepath)) {
      throw new Error(`[ee-core] [jobs-create] file ${this.path} not exists`);
    }
    
    this.path = filepath;
    if (this.type == 'child') {
      this.instance = new ChildJob(name, filepath, this.childOptions);
      this.child = this.instance;
    } else if (this.type == 'renderer') {
      this.instance = new RendererJob(name, filepath, this.winOptions);
      this.renderer = this.instance;
 
      if (this.dev) {
        this.openDevTools();
      }
    }
    
    return;
  }
 
  /**
   * 显示开发者工具栏(仅支持 RendererJob)
   */
  openDevTools () {
    this.instance.openDevTools();
  }
}
 
module.exports = Jobs;