Dart Documentationredis_clientReceiver

Receiver class

Class that handles responses from the redis socket, and serves the replies.

This class gets returned when calling RedisConnection.send and serves as a proxy for the actual RedisReply object.

class Receiver {

 /// Gets set when received.
 RedisReply _reply;

 RedisReply get reply => _reply;

 /// Will automatically resolve all futures requested with this response.
 void set reply(reply) {
   _reply = reply;
   _receivedCompleter.complete(reply);
 }

 Future<RedisReply> _received;

 Completer<RedisReply> _receivedCompleter = new Completer<RedisReply>();

 /**
  * You should never need to create a [Receiver] instance yourself.
  */
 Receiver() {
   _received = _receivedCompleter.future;
 }

 /**
  * Returns a [Future] that gets resolved and doesn't check the return value.
  *
  * Note that **all** redis replies are checked for valid syntax and format.
  * This reply just doesn't check for a specific reply type.
  */
 Future<RedisReply> receive() => _received;

 /**
  * Checks that the received reply is of type [IntegerReply].
  */
 Future<int> receiveInteger() {
   return _received.then((reply) {
     if (reply is! IntegerReply) {
       throw new RedisClientException("The returned reply was not of type IntegerReply.");
     }
     return reply.integer;
   });
 }


 /**
  * Checks that the received reply is of type [ErrorReply].
  */
 Future<String> receiveError() {
   return _received.then((reply) {
     if (reply is! ErrorReply) {
       throw new RedisClientException("The returned reply was not of type ErrorReply.");
     }
     return reply.error;
   });
 }


 /**
  * Checks that the received reply is of type [IntegerReply] and returns `true`
  * if `1` and `false` otherwise.
  */
 Future<bool> receiveBool() {
   return receiveInteger().then((int value) => value == 1 ? true : false);
 }


 /**
  * Checks that the received reply is of type [StatusReply].
  */
 Future<String> receiveStatus([ String expectedStatus ]) {
   return _received.then((reply) {
     if (reply is! StatusReply) {
       var error = "";
       if (reply is ErrorReply) {
         error = " Error: ${reply.error}";
       }
       throw new RedisClientException("The returned reply was not of type StatusReply but ${reply.runtimeType}.${error}");
     }
     if (?expectedStatus && (expectedStatus != reply.status)) {
       throw new RedisClientException("The returned status was not $expectedStatus but ${reply.status}.");
     }
     return reply.status;
   });
 }

 /**
  * Checks that the received reply is of type [BulkReply] and returns the byte
  * list.
  */
 Future<List<int>> receiveBulkData() {
   return _received.then((reply) {
     if (reply is! BulkReply) {
       throw new RedisClientException("The returned reply was not of type BulkReply but ${reply.runtimeType}.");
     }
     return reply.bytes;
   });
 }

 /**
  * Checks that the received reply is of type [BulkReply] and returns a [String].
  */
 Future<String> receiveBulkString() {
   return _received.then((reply) {
     if (reply is! BulkReply) {
       throw new RedisClientException("The returned reply was not of type BulkReply but ${reply.runtimeType}.");
     }
     return reply.string;
   });
 }

 /**
  * Returns the data received by this bulk reply deserialized.
  */
 Future<Object> receiveBulkDeserialized(RedisSerializer serializer) {
   return receiveBulkData().then(serializer.deserialize);
 }

 /**
  * Checks that the received reply is of type [MultiBulkReply].
  */
 Future<MultiBulkReply> receiveMultiBulk() {
   return _received.then((reply) {
     if (reply is! MultiBulkReply) {
       throw new RedisClientException("The returned reply was not of type MultiBulkReply but ${reply.runtimeType}.");
     }
     return reply;
   });
 }

 /**
  * Checks that the received reply is of type [MultiBulkReply] and returns a list
  * of strings.
  */
 Future<List<String>> receiveMultiBulkStrings() {
   return receiveMultiBulk().then((MultiBulkReply reply) {
     return reply.replies.map((BulkReply reply) => reply.string).toList(growable: false);
   });
 }

 /**
  * Checks that the received reply is of type [MultiBulkReply] and returns a list
  * of deserialized objects.
  */
 Future<List<Object>> receiveMultiBulkDeserialized(RedisSerializer serializer) {
   return receiveMultiBulk().then((MultiBulkReply reply) {
     return reply.replies.map((BulkReply reply) => serializer.deserialize(reply.bytes)).toList(growable: false);
   });
 }




 /**
  * Checks that the received reply is either [ErrorReply], [StatusReply] or
  * [BulkReply] and returns the [String] of it.
  */
 // I think this function should not be implemented.
 // Getting an error instead of an expected string should not be default behavior.
//  Future<String> receiveString() {
//    return _received.then((reply) {
//      if (reply is ErrorReply) {
//        return reply.error;
//      }
//      else if (reply is StatusReply) {
//        return reply.status;
//      }
//      else if (reply is BulkReply) {
//        return reply.string;
//      }
//      else {
//        throw new RedisClientException("Couldn't get a string of type ${reply.runtimeType}.");
//      }
//    });
//  }

}

Constructors

new Receiver() #

You should never need to create a Receiver instance yourself.

Receiver() {
 _received = _receivedCompleter.future;
}

Properties

RedisReply reply #

Will automatically resolve all futures requested with this response.

RedisReply get reply => _reply;
void set reply(reply) {
 _reply = reply;
 _receivedCompleter.complete(reply);
}

Methods

Future<RedisReply> receive() #

Returns a Future that gets resolved and doesn't check the return value.

Note that all redis replies are checked for valid syntax and format. This reply just doesn't check for a specific reply type.

Future<RedisReply> receive() => _received;

Future<bool> receiveBool() #

Checks that the received reply is of type IntegerReply and returns true if 1 and false otherwise.

Future<bool> receiveBool() {
 return receiveInteger().then((int value) => value == 1 ? true : false);
}

Future<List<int>> receiveBulkData() #

Checks that the received reply is of type BulkReply and returns the byte list.

Future<List<int>> receiveBulkData() {
 return _received.then((reply) {
   if (reply is! BulkReply) {
     throw new RedisClientException("The returned reply was not of type BulkReply but ${reply.runtimeType}.");
   }
   return reply.bytes;
 });
}

Future<Object> receiveBulkDeserialized(RedisSerializer serializer) #

Returns the data received by this bulk reply deserialized.

Future<Object> receiveBulkDeserialized(RedisSerializer serializer) {
 return receiveBulkData().then(serializer.deserialize);
}

Future<String> receiveBulkString() #

Checks that the received reply is of type BulkReply and returns a String.

Future<String> receiveBulkString() {
 return _received.then((reply) {
   if (reply is! BulkReply) {
     throw new RedisClientException("The returned reply was not of type BulkReply but ${reply.runtimeType}.");
   }
   return reply.string;
 });
}

Future<String> receiveError() #

Checks that the received reply is of type ErrorReply.

Future<String> receiveError() {
 return _received.then((reply) {
   if (reply is! ErrorReply) {
     throw new RedisClientException("The returned reply was not of type ErrorReply.");
   }
   return reply.error;
 });
}

Future<int> receiveInteger() #

Checks that the received reply is of type IntegerReply.

Future<int> receiveInteger() {
 return _received.then((reply) {
   if (reply is! IntegerReply) {
     throw new RedisClientException("The returned reply was not of type IntegerReply.");
   }
   return reply.integer;
 });
}

Future<MultiBulkReply> receiveMultiBulk() #

Checks that the received reply is of type MultiBulkReply.

Future<MultiBulkReply> receiveMultiBulk() {
 return _received.then((reply) {
   if (reply is! MultiBulkReply) {
     throw new RedisClientException("The returned reply was not of type MultiBulkReply but ${reply.runtimeType}.");
   }
   return reply;
 });
}

Future<List<Object>> receiveMultiBulkDeserialized(RedisSerializer serializer) #

Checks that the received reply is of type MultiBulkReply and returns a list of deserialized objects.

Future<List<Object>> receiveMultiBulkDeserialized(RedisSerializer serializer) {
 return receiveMultiBulk().then((MultiBulkReply reply) {
   return reply.replies.map((BulkReply reply) => serializer.deserialize(reply.bytes)).toList(growable: false);
 });
}

Future<List<String>> receiveMultiBulkStrings() #

Checks that the received reply is of type MultiBulkReply and returns a list of strings.

Future<List<String>> receiveMultiBulkStrings() {
 return receiveMultiBulk().then((MultiBulkReply reply) {
   return reply.replies.map((BulkReply reply) => reply.string).toList(growable: false);
 });
}

Future<String> receiveStatus([String expectedStatus]) #

Checks that the received reply is of type StatusReply.

Future<String> receiveStatus([ String expectedStatus ]) {
 return _received.then((reply) {
   if (reply is! StatusReply) {
     var error = "";
     if (reply is ErrorReply) {
       error = " Error: ${reply.error}";
     }
     throw new RedisClientException("The returned reply was not of type StatusReply but ${reply.runtimeType}.${error}");
   }
   if (?expectedStatus && (expectedStatus != reply.status)) {
     throw new RedisClientException("The returned status was not $expectedStatus but ${reply.status}.");
   }
   return reply.status;
 });
}