src/protocolMessaging/index.js
// Optionally pass `resolve` and `reject` callbacks to handle a response
Optionally pass `resolve` and `reject` callbacks to handle a response
Optionally pass ` resolve ` and ` reject ` callbacks to handle a response
call(signerAddress, message, resolve, reject, locator, locatorType) {
    if (locatorType && _.includes(['http', 'https'], locatorType)) {
      const timeout = setTimeout(() => reject({ message: `Request timed out.`, code: -1 }), this.timeout)
      const callServer = function(request, callback) {
        const options = {
          method: 'POST',
          body: request,
          headers: {
            'Content-Type': 'application/json',
          },
        }
        fetch(locator, options)
          .then(res => res.text())
          .then(text => {
            callback(null, text)
          })
          .catch(err => {
            callback(err)
          })
      }
      const client = jaysonBrowserClient(callServer)
      client.request(message.method, message.params, (err, error, response) => {
        clearTimeout(timeout)
        if (err) {
          reject(err)
        } else if (error) {
          reject(`Maker Error: ${error.message}`)
        } else {
          resolve(response)
        }
      })
    } else if (locatorType === 'contract') {
      routeDelegateCall(locator, message, signerAddress, this.address)
        .then(resp => {
          resolve(resp)
        })
        .catch(e => {
          reject(e)
        })
    } else {
      const messageString = JSON.stringify({
        sender: this.address.toLowerCase(),
        receiver: signerAddress,
        message: JSON.stringify(message),
        id: uuid(),
      })
      this.socket.send(messageString)
      if (typeof resolve === 'function') {
        this.RESOLVERS[message.id] = resolve
      }
      if (typeof reject === 'function') {
        this.REJECTORS[message.id] = reject
      }
      this.TIMEOUTS[message.id] = setTimeout(() => {
        if (typeof reject === 'function') {
          reject({ message: `Request timed out. [${message.id}]`, code: -1 })
        }
      }, this.timeout)
    }
  }
call ( signerAddress , message , resolve , reject , locator , locatorType ) { if ( locatorType && _ . includes ( [ 'http' , 'https' ] , locatorType ) ) { const timeout = setTimeout ( ( ) => reject ( { message : ` ` , code : - 1 } ) , this . timeout ) const callServer = function ( request , callback ) { const options = { method : 'POST' , body : request , headers : { 'Content-Type' : 'application/json' , } , } fetch ( locator , options ) . then ( res => res . text ( ) ) . then ( text => { callback ( null , text ) } ) . catch ( err => { callback ( err ) } ) } const client = jaysonBrowserClient ( callServer ) client . request ( message . method , message . params , ( err , error , response ) => { clearTimeout ( timeout ) if ( err ) { reject ( err ) } else if ( error ) { reject ( ` ${ error . message } ` ) } else { resolve ( response ) } } ) } else if ( locatorType === 'contract' ) { routeDelegateCall ( locator , message , signerAddress , this . address ) . then ( resp => { resolve ( resp ) } ) . catch ( e => { reject ( e ) } ) } else { const messageString = JSON . stringify ( { sender : this . address . toLowerCase ( ) , receiver : signerAddress , message : JSON . stringify ( message ) , id : uuid ( ) , } ) this . socket . send ( messageString ) if ( typeof resolve === 'function' ) { this . RESOLVERS [ message . id ] = resolve } if ( typeof reject === 'function' ) { this . REJECTORS [ message . id ] = reject } this . TIMEOUTS [ message . id ] = setTimeout ( ( ) => { if ( typeof reject === 'function' ) { reject ( { message : ` ${ message . id } ` , code : - 1 } ) } } , this . timeout ) } }
Optionally pass `resolve` and `reject` callbacks to handle a response
Optionally pass ` resolve ` and ` reject ` callbacks to handle a response
// Set the promise resolvers and rejectors for this call // Set a timeout for this call