platform/graphql_server/lib/src/apollo/remote_client.dart

33 lines
895 B
Dart
Raw Normal View History

2019-04-18 02:02:27 +00:00
import 'dart:async';
import 'package:stream_channel/stream_channel.dart';
import 'transport.dart';
class RemoteClient extends StreamChannelMixin<OperationMessage> {
final StreamChannel<Map> channel;
final StreamChannelController<OperationMessage> _ctrl =
StreamChannelController();
RemoteClient.withoutJson(this.channel) {
2019-04-18 04:12:52 +00:00
_ctrl.local.stream
.map((m) => m.toJson())
.cast<Map>()
.forEach(channel.sink.add);
2019-04-18 02:02:27 +00:00
channel.stream.listen((m) {
_ctrl.local.sink.add(OperationMessage.fromJson(m));
});
}
RemoteClient(StreamChannel<String> channel)
: this.withoutJson(jsonDocument.bind(channel).cast<Map>());
@override
StreamSink<OperationMessage> get sink => _ctrl.foreign.sink;
@override
Stream<OperationMessage> get stream => _ctrl.foreign.stream;
void close() {
channel.sink.close();
_ctrl.local.sink.close();
}
}