Skip to content

Commit

Permalink
make 'melos run client_analyze' succeed
Browse files Browse the repository at this point in the history
  • Loading branch information
Hagen Rode committed Dec 10, 2024
1 parent 29b994b commit 35fede4
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 33 deletions.
100 changes: 74 additions & 26 deletions packages/graphql/test/websocket_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,17 @@ Future<void> main() async {
operation: Operation(document: parseString('subscription {}')),
);
final waitForConnection = true;
final subscriptionDataStream = socketClient.subscribe(payload, waitForConnection);
await socketClient.connectionState.where((state) => state == SocketConnectionState.connected).first;
final subscriptionDataStream =
socketClient.subscribe(payload, waitForConnection);
await socketClient.connectionState
.where((state) => state == SocketConnectionState.connected)
.first;

// ignore: unawaited_futures
socketClient.socketChannel!.stream.where((message) => message == expectedMessage).first.then((_) {
socketClient.socketChannel!.stream
.where((message) => message == expectedMessage)
.first
.then((_) {
socketClient.socketChannel!.sink.add(jsonEncode({
'type': 'data',
'id': '01020304-0506-4708-890a-0b0c0d0e0f10',
Expand Down Expand Up @@ -230,11 +236,17 @@ Future<void> main() async {
operation: Operation(document: parseString('subscription {}')),
);
final waitForConnection = true;
final subscriptionDataStream = socketClient.subscribe(payload, waitForConnection);
await socketClient.connectionState.where((state) => state == SocketConnectionState.connected).first;
final subscriptionDataStream =
socketClient.subscribe(payload, waitForConnection);
await socketClient.connectionState
.where((state) => state == SocketConnectionState.connected)
.first;

// ignore: unawaited_futures
socketClient.socketChannel!.stream.where((message) => message == expectedMessage).first.then((_) {
socketClient.socketChannel!.stream
.where((message) => message == expectedMessage)
.first
.then((_) {
socketClient.socketChannel!.sink.add(jsonEncode({
'type': 'data',
'id': '01020304-0506-4708-890a-0b0c0d0e0f10',
Expand Down Expand Up @@ -277,7 +289,8 @@ Future<void> main() async {
operation: Operation(document: gql('subscription {}')),
);
final waitForConnection = true;
final subscriptionDataStream = socketClient.subscribe(payload, waitForConnection);
final subscriptionDataStream =
socketClient.subscribe(payload, waitForConnection);

await expectLater(
socketClient.connectionState,
Expand All @@ -301,7 +314,10 @@ Future<void> main() async {
);

// ignore: unawaited_futures
socketClient.socketChannel!.stream.where((message) => message == expectedMessage).first.then((_) {
socketClient.socketChannel!.stream
.where((message) => message == expectedMessage)
.first
.then((_) {
socketClient.socketChannel!.sink.add(jsonEncode({
'type': 'data',
'id': '01020304-0506-4708-890a-0b0c0d0e0f10',
Expand Down Expand Up @@ -340,7 +356,8 @@ Future<void> main() async {
operation: Operation(document: gql('subscription {}')),
);
final waitForConnection = true;
final subscriptionDataStream = socketClient.subscribe(payload, waitForConnection);
final subscriptionDataStream =
socketClient.subscribe(payload, waitForConnection);

await expectLater(
socketClient.connectionState,
Expand Down Expand Up @@ -371,7 +388,10 @@ Future<void> main() async {
);

// ignore: unawaited_futures
socketClient.socketChannel!.stream.where((message) => message == expectedMessage).first.then((_) {
socketClient.socketChannel!.stream
.where((message) => message == expectedMessage)
.first
.then((_) {
socketClient.socketChannel!.sink.add(jsonEncode({
'type': 'data',
'id': '01020304-0506-4708-890a-0b0c0d0e0f10',
Expand Down Expand Up @@ -491,11 +511,17 @@ Future<void> main() async {
operation: Operation(document: parseString('subscription {}')),
);
final waitForConnection = true;
final subscriptionDataStream = socketClient.subscribe(payload, waitForConnection);
await socketClient.connectionState.where((state) => state == SocketConnectionState.connected).first;
final subscriptionDataStream =
socketClient.subscribe(payload, waitForConnection);
await socketClient.connectionState
.where((state) => state == SocketConnectionState.connected)
.first;

// ignore: unawaited_futures
socketClient.socketChannel!.stream.where((message) => message == expectedMessage).first.then((_) {
socketClient.socketChannel!.stream
.where((message) => message == expectedMessage)
.first
.then((_) {
socketClient.socketChannel!.sink.add(jsonEncode({
'type': 'next',
'id': '01020304-0506-4708-890a-0b0c0d0e0f10',
Expand Down Expand Up @@ -534,7 +560,8 @@ Future<void> main() async {
operation: Operation(document: gql('subscription {}')),
);
final waitForConnection = true;
final subscriptionDataStream = socketClient.subscribe(payload, waitForConnection);
final subscriptionDataStream =
socketClient.subscribe(payload, waitForConnection);

await expectLater(
socketClient.connectionState,
Expand All @@ -560,7 +587,10 @@ Future<void> main() async {
);

// ignore: unawaited_futures
socketClient.socketChannel!.stream.where((message) => message == expectedMessage).first.then((_) {
socketClient.socketChannel!.stream
.where((message) => message == expectedMessage)
.first
.then((_) {
socketClient.socketChannel!.sink.add(jsonEncode({
'type': 'next',
'id': '01020304-0506-4708-890a-0b0c0d0e0f10',
Expand Down Expand Up @@ -599,7 +629,8 @@ Future<void> main() async {
operation: Operation(document: gql('subscription {}')),
);
final waitForConnection = true;
final subscriptionDataStream = socketClient.subscribe(payload, waitForConnection);
final subscriptionDataStream =
socketClient.subscribe(payload, waitForConnection);

await expectLater(
socketClient.connectionState,
Expand Down Expand Up @@ -632,7 +663,10 @@ Future<void> main() async {
);

// ignore: unawaited_futures
socketClient.socketChannel!.stream.where((message) => message == expectedMessage).first.then((_) {
socketClient.socketChannel!.stream
.where((message) => message == expectedMessage)
.first
.then((_) {
socketClient.socketChannel!.sink.add(jsonEncode({
'type': 'next',
'id': '01020304-0506-4708-890a-0b0c0d0e0f10',
Expand Down Expand Up @@ -667,12 +701,14 @@ Future<void> main() async {
);
});

test('resubscribe after server disconnect and resubscription not paused', () async {
test('resubscribe after server disconnect and resubscription not paused',
() async {
final payload = Request(
operation: Operation(document: gql('subscription {}')),
);
final waitForConnection = true;
final subscriptionDataStream = socketClient.subscribe(payload, waitForConnection);
final subscriptionDataStream =
socketClient.subscribe(payload, waitForConnection);

await expectLater(
socketClient.connectionState,
Expand Down Expand Up @@ -713,7 +749,10 @@ Future<void> main() async {
);

// ignore: unawaited_futures
socketClient.socketChannel!.stream.where((message) => message == expectedMessage).first.then((_) {
socketClient.socketChannel!.stream
.where((message) => message == expectedMessage)
.first
.then((_) {
socketClient.socketChannel!.sink.add(jsonEncode({
'type': 'next',
'id': '01020304-0506-4708-890a-0b0c0d0e0f10',
Expand Down Expand Up @@ -754,7 +793,8 @@ Future<void> main() async {
StreamController<dynamic> controller;
setUp(overridePrint((log) {
controller = StreamController(sync: true);
socketClient = getTestClient(controller: controller, wsUrl: wsUrl, autoReconnect: false);
socketClient = getTestClient(
controller: controller, wsUrl: wsUrl, autoReconnect: false);
}));
tearDown(overridePrint(
(log) => socketClient.dispose(),
Expand Down Expand Up @@ -788,7 +828,8 @@ Future<void> main() async {
]),
);

expect(socketClient.socketChannel!.closeCode, WebSocketStatus.normalClosure);
expect(
socketClient.socketChannel!.closeCode, WebSocketStatus.normalClosure);

expect(await isEmpty.timeout(const Duration(seconds: 1)), true);
});
Expand All @@ -813,7 +854,9 @@ Future<void> main() async {
));

test('connection', () async {
await socketClient.connectionState.where((state) => state == SocketConnectionState.connected).first;
await socketClient.connectionState
.where((state) => state == SocketConnectionState.connected)
.first;

await expectLater(
socketClient.socketChannel!.stream.map((s) {
Expand Down Expand Up @@ -844,7 +887,9 @@ Future<void> main() async {
}));

test('connection', () async {
await socketClient.connectionState.where((state) => state == SocketConnectionState.connected).first;
await socketClient.connectionState
.where((state) => state == SocketConnectionState.connected)
.first;

await expectLater(
socketClient.socketChannel!.stream.map((s) {
Expand Down Expand Up @@ -884,7 +929,8 @@ Future<void> main() async {
protocol: GraphQLProtocol.graphqlWs,
config: SocketClientConfig(
delayBetweenReconnectionAttempts: const Duration(milliseconds: 1),
initialPayload: () => {'token': getToken(), 'protocol': GraphQLProtocol.graphqlWs},
initialPayload: () =>
{'token': getToken(), 'protocol': GraphQLProtocol.graphqlWs},
onConnectionLost: (code, reason) async {
if (code == 4001) {
_token = 'mytoken2';
Expand Down Expand Up @@ -935,7 +981,9 @@ Future<void> main() async {
]),
);

await socketClient.connectionState.where((state) => state == SocketConnectionState.connected).first;
await socketClient.connectionState
.where((state) => state == SocketConnectionState.connected)
.first;

await expectLater(
socketClient.socketChannel!.stream.map((s) {
Expand Down
22 changes: 15 additions & 7 deletions packages/graphql_flutter/lib/src/widgets/hooks/subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ typedef OnSubscriptionResult<TParsed> = void Function(
GraphQLClient? client,
);

typedef SubscriptionBuilder<TParsed> = Widget Function(QueryResult<TParsed> result);
typedef SubscriptionBuilder<TParsed> = Widget Function(
QueryResult<TParsed> result);

QueryResult<TParsed> useSubscription<TParsed>(
SubscriptionOptions<TParsed> options, {
Expand Down Expand Up @@ -58,15 +59,19 @@ class _SubscriptionHook<TParsed> extends Hook<Stream<QueryResult<TParsed>>> {
required this.onSubscriptionResult,
});
@override
HookState<Stream<QueryResult<TParsed>>, Hook<Stream<QueryResult<TParsed>>>> createState() {
HookState<Stream<QueryResult<TParsed>>, Hook<Stream<QueryResult<TParsed>>>>
createState() {
return _SubscriptionHookState();
}
}

class _SubscriptionHookState<TParsed> extends HookState<Stream<QueryResult<TParsed>>, _SubscriptionHook<TParsed>> {
class _SubscriptionHookState<TParsed> extends HookState<
Stream<QueryResult<TParsed>>, _SubscriptionHook<TParsed>> {
late Stream<QueryResult<TParsed>> stream;

List<ConnectivityResult> _currentConnectivityResult = [ConnectivityResult.none];
List<ConnectivityResult> _currentConnectivityResult = [
ConnectivityResult.none
];
StreamSubscription<List<ConnectivityResult>>? _networkSubscription;

void _initSubscription() {
Expand All @@ -85,7 +90,8 @@ class _SubscriptionHookState<TParsed> extends HookState<Stream<QueryResult<TPars
void initHook() {
super.initHook();
_initSubscription();
_networkSubscription = Connectivity().onConnectivityChanged.listen(_onNetworkChange);
_networkSubscription =
Connectivity().onConnectivityChanged.listen(_onNetworkChange);
}

@override
Expand All @@ -106,15 +112,17 @@ class _SubscriptionHookState<TParsed> extends HookState<Stream<QueryResult<TPars
Future<void> _onNetworkChange(List<ConnectivityResult> results) async {
//if from offline to online
if (_currentConnectivityResult.contains(ConnectivityResult.none) &&
(results.contains(ConnectivityResult.mobile) || results.contains(ConnectivityResult.wifi))) {
(results.contains(ConnectivityResult.mobile) ||
results.contains(ConnectivityResult.wifi))) {
_currentConnectivityResult = List.from(results, growable: false);

// android connectivitystate cannot be trusted
// validate with nslookup
if (Platform.isAndroid) {
try {
final nsLookupResult = await InternetAddress.lookup('google.com');
if (nsLookupResult.isNotEmpty && nsLookupResult[0].rawAddress.isNotEmpty) {
if (nsLookupResult.isNotEmpty &&
nsLookupResult[0].rawAddress.isNotEmpty) {
_initSubscription();
}
// on exception -> no real connection, set current state to none
Expand Down

0 comments on commit 35fede4

Please sign in to comment.