Skip to content

Commit

Permalink
Issue #56: Move parser and serialiser from class Connection to Command
Browse files Browse the repository at this point in the history
  • Loading branch information
ra1u committed Feb 7, 2022
1 parent 027364c commit 6d2c0f6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
11 changes: 10 additions & 1 deletion lib/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@ part of redis;

class Command {
/*RedisConnection*/ var _connection;
// parser is somthing that transfer data from redis database to object
Parser parser = Parser();
// serialiser is somehing that transform object to redis
Serialiser serialise = Serialiser();

Command(this._connection) {}
Command.from(Command other) {
this._connection = other._connection;
this.parser = other.parser;
this.serialise = other.serialise;
}

/// Serialise and send data to server
///
Expand All @@ -24,7 +33,7 @@ class Command {
/// send_object(["SET","key","value"]);
Future send_object(Object obj) {
try {
return _connection._sendraw(RedisSerialise.Serialise(obj)).then((v) {
return _connection._sendraw(parser,serialise.serialise(obj)).then((v) {
// turn RedisError into exception
if (v is RedisError) {
return Future.error(v);
Expand Down
8 changes: 4 additions & 4 deletions lib/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class RedisConnection {
//this doesnt send anything
//it just wait something to come from socket
//it parse it and execute future
Future _senddummy() {
Future _senddummy(Parser parser) {
_future = _future.then((_) {
return RedisParser.parseredisresponse(_stream!);
return parser.parse(_stream!);
});
return _future;
}
Expand All @@ -71,9 +71,9 @@ class RedisConnection {
}

// ignore: unused_element
Future _sendraw(List<int> data) {
Future _sendraw(Parser parser,List<int> data) {
_socket!.add(data);
return _senddummy();
return _senddummy(parser);
}

void disable_nagle(bool v) {
Expand Down
4 changes: 2 additions & 2 deletions lib/pubsub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class PubSub {
StreamController<List> _stream_controler = StreamController<List>();

PubSub(Command command) {
_command = Command(command._connection);
_command = Command.from(command);
command.send_nothing()!.then((_) {
//override socket with warrning
command._connection = _WarrningPubSubInProgress();
// listen and process forever
return Future.doWhile(() {
return _command._connection._senddummy().then<bool>((var data) {
return _command._connection._senddummy(_command.parser).then<bool>((var data) {
_stream_controler.add(data);
return true;
});
Expand Down
6 changes: 6 additions & 0 deletions lib/redisparser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

part of redis;

class Parser {
Future parse(LazyStream s){
return RedisParser.parseredisresponse(s);
}
}

class RedisParser {
static final UTF8 = const Utf8Codec();
static const int CR = 13;
Expand Down
7 changes: 7 additions & 0 deletions lib/redisserialise.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class RedisBulk {

typedef Consumer = void Function(Iterable<int> s);


class Serialiser {
List<int> serialise(Object? object){
return RedisSerialise.Serialise(object);
}
}

class RedisSerialise {
static final ASCII = const AsciiCodec();
static final UTF8 = const Utf8Codec();
Expand Down

0 comments on commit 6d2c0f6

Please sign in to comment.