222
schangxiang@126.com
2025-06-13 6a8393408d8cefcea02b7a598967de8dc1e565c2
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
65
66
67
68
69
70
71
72
73
74
'use strict';
 
const cluster = require('./');
const utils = require('./utils');
const Base = require('sdk-base');
const is = require('is-type-of');
 
class APIClientBase extends Base {
  constructor(options) {
    options = options || {};
    super(options);
 
    const wrapper = (options.cluster || cluster)(
      this.DataClient, this.clusterOptions
    );
    for (const from in this.delegates) {
      const to = this.delegates[from];
      wrapper.delegate(from, to);
    }
    this._client = wrapper.create(options);
    utils.delegateEvents(this._client, this);
 
    if (!options.initMethod) {
      this._client.ready(err => {
        this.ready(err ? err : true);
      });
    }
  }
 
  get isClusterClientLeader() {
    return this._client.isClusterClientLeader;
  }
 
  close() {
    if (is.function(this._client.close)) {
      return this._client.close();
    }
    return cluster.close(this._client);
  }
 
  get DataClient() {
    /* istanbul ignore  next */
    throw new Error('[APIClient] DataClient is required');
  }
 
  /**
   * the cluster options
   *
   * @property {Object} APIClientBase#clusterOptions
   */
  get clusterOptions() {
    /* istanbul ignore  next */
    return {};
  }
 
  /**
   * the delegates map
   *
   * @example
   * ---------------------------
   * delegates => {
   *    subscribe: 'subscribe',
   *    foo: 'invoke',
   * }
   *
   * @property {Object} APIClientBase#delegates
   */
  get delegates() {
    /* istanbul ignore  next */
    return {};
  }
}
 
module.exports = APIClientBase;