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
const Consts = require("./consts");
const algorithm = require('./algorithm');
 
/**
 * 算法调度器
 */
class Scheduler {
  constructor(algorithm) {
    this.algorithm = algorithm || Consts.polling;
  }
 
  /**
   * 计算
   */
  calculate(tasks, params) {
    const results = algorithm[this.algorithm](tasks, ...params);
    return results;
  }
 
  /**
   * 设置算法
   */
  setAlgorithm = (algorithm) => {
    if (algorithm in Consts) {
      this.algorithm = algorithm;
    } else {
      throw new Error(`Invalid algorithm: ${algorithm}, pick from ${Object.keys(Consts).join('|')}`);
    }
  }
}
 
module.exports = Scheduler;