<commit_before>define(['backend/port', 'darkwallet_gateway'],
function(Port) {
  'use strict';
  function ObeliskService(core) {
      var self = this;
      this.client = null;
      this.connected = false;
      this.connecting = false;
    
      // Port for communication with the frontend
      Port.listen('obelisk', function() {
        }, function(port) {
            // Connected
            console.log('[bus] obelisk client connected');
            var client = self.client;
            if (client && client.connected) {
                port.postMessage({'type': 'connected'});
            } else if (client && client.connecting) {
                port.postMessage({'type': 'connecting'});
            }
      }, function() {
          console.log('[bus] obelisk client disconnected');
      });
  }

  ObeliskService.prototype.connect = function(connectUri, handleConnect) {
      if (this.connected || this.connecting) {
          // wait for connection
      } else {
          console.log("[obelisk] Connecting");
          Port.post('obelisk', {'type': 'connecting'});
          this.connectClient(connectUri, function(err) {
              if (!err) {
                  console.log("[obelisk] Connected");
              }
              handleConnect ? handleConnect(err) : null;
              if (err) {
                  console.log("[obelisk] Error connecting");
                  Port.post('obelisk', {'type': 'connectionError', 'error': 'Error connecting'});
              } else {
                  Port.post('obelisk', {'type': 'connected'});
              }
          });
      }
  }

  ObeliskService.prototype.connectClient = function(connectUri, handleConnect) {
      var self = this;
      this.connecting = true;
      this.client = new GatewayClient(connectUri, function(err) {
          if (!err) {
              self.client.connected = true;
              self.connected = true;
          }
          self.connecting = false;
          handleConnect ? handleConnect(err) : null;
      });
  }

  ObeliskService.prototype.getClient = function() {
    return this.client;
  }

  return ObeliskService;

});<commit_msg>Add a function for the obelisk service to disconnect, and start receiving disconnect notifications from the gateway client.<commit_after>define(['backend/port', 'darkwallet_gateway'],
function(Port) {
  'use strict';


  /**
   * Obelisk service class
   */
  function ObeliskService(core) {
      var self = this;
      this.client = null;
      this.connected = false;
      this.connecting = false;
    
      // Port for communication with the frontend
      Port.listen('obelisk', function() {
        }, function(port) {
            // Connected
            console.log('[bus] obelisk client connected');
            var client = self.client;
            if (client && client.connected) {
                port.postMessage({'type': 'connected'});
            } else if (client && client.connecting) {
                port.postMessage({'type': 'connecting'});
            }
      }, function() {
          console.log('[bus] obelisk client disconnected');
      });
  }


  /**
   * Connect to obelisk
   */
  ObeliskService.prototype.connect = function(connectUri, handleConnect) {
      var self = this;
      if (this.connected || this.connecting) {
          // wait for connection
      } else {
          console.log("[obelisk] Connecting");
          Port.post('obelisk', {'type': 'connecting'});
          self.connecting = true;
          this.connectClient(connectUri, function(err) {
              // Connected
              if (!err) {
                  console.log("[obelisk] Connected");
                  self.connected = true;
              }
              self.connecting = false;
              handleConnect ? handleConnect(err) : null;
              if (err) {
                  console.log("[obelisk] Error connecting");
                  Port.post('obelisk', {'type': 'connectionError', 'error': 'Error connecting'});
              } else {
                  Port.post('obelisk', {'type': 'connected'});
              }
          }, function(evt) {
              // Disconnected
              console.log("[obelisk] Disconnected", evt);
              Port.post('obelisk', {'type': 'disconnected'});
              self.connected = false;
          }, function(evt) {
              // Error
              console.log("[obelisk] websocket error", evt)
          });
      }
  }


  /**
   * Disconnect from obelisk
   */
  ObeliskService.prototype.disconnect = function() {
      if (this.client && this.connected) {
          this.client.websocket.close();
      }
  }


  /**
   * Start the gateway client
   */
  ObeliskService.prototype.connectClient = function(connectUri, handleConnect) {
      var self = this;
      this.connecting = true;
      this.client = new GatewayClient(connectUri, handleConnect);
  }


  /**
   * Get the gateway client
   */
  ObeliskService.prototype.getClient = function() {
    return this.client;
  }

  return ObeliskService;

});
{'fraction_non_alphanumeric': 0.13174603174603175, 'fraction_numerical': 0.0, 'mean_word_length': 1.8904816513761469, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7295392', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1363, 'n_words': 289}