constructor()

in device/core/src/internal_client.ts [64:127]


  constructor(transport: DeviceTransport, connStr?: string) {
    /*Codes_SRS_NODE_INTERNAL_CLIENT_05_001: [The Client constructor shall throw ReferenceError if the transport argument is falsy.]*/
    if (!transport) throw new ReferenceError('transport is \'' + transport + '\'');

    super();
    this._userRegisteredMethodListener = false;

    if (connStr) {
      throw new errors.InvalidOperationError('the connectionString parameter of the constructor is not used - users of the SDK should be using the `fromConnectionString` factory method.');
    }

    this._transport = transport;

    this._transport.on('error', (err) => {
      // errors right now bubble up through the disconnect handler.
      // ultimately we would like to get rid of that disconnect event and rely on the error event instead
      debugErrors('Transport error: ' + err);
    });

    /*Codes_SRS_NODE_INTERNAL_CLIENT_41_001: [A `connect` event will be emitted when a `connected` event is received from the transport.]*/
    this._transport.on('connected', () => this.emit('connect'));

    this._methodCallbackMap = {};

    this._disconnectHandler = (err) => {
      if (err) {
        debugErrors('transport disconnect event: ' + err);
      } else {
        debug('transport disconnect event: no error');
      }
      if (err && this._retryPolicy.shouldRetry(err)) {
        /*Codes_SRS_NODE_INTERNAL_CLIENT_16_098: [If the transport emits a `disconnect` event while the client is subscribed to direct methods the retry policy shall be used to reconnect and re-enable the feature using the transport `enableMethods` method.]*/
        if (this._userRegisteredMethodListener) {
          debug('re-enabling Methods link');
          this._enableMethods((err) => {
            if (err) {
              debugErrors('Error enabling methods: ' + err);
              /*Codes_SRS_NODE_INTERNAL_CLIENT_16_100: [If the retry policy fails to reestablish the direct methods functionality a `disconnect` event shall be emitted with a `results.Disconnected` object.]*/
              this.emit('disconnect', new results.Disconnected(err));
            }
          });
        }

        /*Codes_SRS_NODE_INTERNAL_CLIENT_16_099: [If the transport emits a `disconnect` event while the client is subscribed to desired properties updates the retry policy shall be used to reconnect and re-enable the feature using the transport `enableTwinDesiredPropertiesUpdates` method.]*/
        if (this._twin && this._twin.userRegisteredDesiredPropertiesListener) {
          debug('re-enabling Twin');
          this._twin.enableTwinDesiredPropertiesUpdates((err) => {
            if (err) {
              debugErrors('Error enabling twin: ' + err);
              /*Codes_SRS_NODE_INTERNAL_CLIENT_16_101: [If the retry policy fails to reestablish the twin desired properties updates functionality a `disconnect` event shall be emitted with a `results.Disconnected` object.]*/
              this.emit('disconnect', new results.Disconnected(err));
            }
          });
        }
      } else {
        this.emit('disconnect', new results.Disconnected(err));
      }
    };
    /*Codes_SRS_NODE_INTERNAL_CLIENT_16_045: [If the transport successfully establishes a connection the `open` method shall subscribe to the `disconnect` event of the transport.]*/
    this._transport.on('disconnect', this._disconnectHandler);

    this._retryPolicy = new ExponentialBackOffWithJitter();
    this._maxOperationTimeout = MAX_OPERATION_TIMEOUT;
  }