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

30 lines
861 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) {
_ctrl.local.stream.map((m) => m.toJson()).cast<Map>().pipe(channel.sink);
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();
}
}