summaryrefslogtreecommitdiff
path: root/WebInterface/src/js/modules/networking/commands/login/listServers.js
blob: 2c2bc112444a5cfdb650ccf6a2bd5da7ba726b7d (plain)
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
import Command from '../_command';

/**
 * Handles serverList commands
 */
export default class ListServers extends Command {
  /**
   * Registers interface for communication with other objects
   * @param {Interface} iface
   */
  constructor(iface) {
    super(iface);
    this.registerPublic('listServers', 'listServers');
    this.refreshing = false;
  }

  /**
   * Requests server list from the server
   */
  listServers() {
    if (this.refreshing) return; // If already refreshing, no new request

    let listFn = (groups) => {
      // Populate server listing
      this.iface.callMethod('serverListing', 'flushElements');
      this.iface.callMethod('serverListing', 'addElements', groups, this.iface);
      // Unbind network function
      this.iface.callMethod('networker', 'removeHandler', 'ListGroups');
      this.refreshing = false;
    };
    let errorHandler = (err) => {
      this.refreshing = false;
      console.error(err.toString());
    };

    this.iface.callMethod('networker', 'registerHandler',
        'ListGroups', listFn);
    this.iface.callMethod('networker', 'sendRequest',
        'GetGroups', errorHandler);

    this.refreshing = true;
  }
}