Skip to content

Commit

Permalink
Bad internet no longer throws, but allows loading in all of the things
Browse files Browse the repository at this point in the history
  • Loading branch information
Sterling Long committed Aug 11, 2023
1 parent f6c90f1 commit fb2a199
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 92 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.1.2

- No internet connection/invalid URL errors will no longer throw, but the relay's `onRelayClientError` will emit them.
- Oher relay client bug fixes

## 2.1.1

- Fixed issue with relayUrl not being used correctly
Expand Down
7 changes: 3 additions & 4 deletions lib/apis/core/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,9 @@ class Core implements ICore {
// If the relay URL is the default, try both it and the backup (.org)
if (_relayUrl == WalletConnectConstants.DEFAULT_RELAY_URL) {
_relayUrl = WalletConnectConstants.FALLBACK_RELAY_URL;
await relayClient.init();
} else {
// Otherwise, just rethrow the error
rethrow;
try {
await relayClient.init();
} catch (_) {}
}
}

Expand Down
49 changes: 33 additions & 16 deletions lib/apis/core/relay_client/relay_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class RelayClient implements IRelayClient {

bool _initialized = false;
bool _active = true;
bool _connecting = false;
Future _connectingFuture = Future.value();
bool _handledClose = false;

// late WebSocketChannel socket;
Expand Down Expand Up @@ -94,7 +96,8 @@ class RelayClient implements IRelayClient {
await topicMap.init();

// Setup the json RPC server
await _createJsonRPCProvider();
_connectingFuture = _createJsonRPCProvider();
await _connectingFuture;
_startHeartbeat();

_initialized = true;
Expand Down Expand Up @@ -178,7 +181,8 @@ class RelayClient implements IRelayClient {
if (_active) {
await disconnect();
}
await _createJsonRPCProvider();
_connectingFuture = _createJsonRPCProvider();
await _connectingFuture;
if (_heartbeatTimer == null) {
_startHeartbeat();
}
Expand All @@ -204,6 +208,7 @@ class RelayClient implements IRelayClient {
/// PRIVATE FUNCTIONS ///
Future<void> _createJsonRPCProvider() async {
_connecting = true;
_active = true;
var auth = await core.crypto.signJWT(core.relayUrl);
core.logger.v('Signed JWT: $auth');
Expand All @@ -222,16 +227,6 @@ class RelayClient implements IRelayClient {
jsonRPC = null;
}

// if (socket != null) {
// await socket!.close();
// socket = null;
// }

// socket = WebSocketHandler(
// url: url,
// httpClient: httpClient,
// );

core.logger.v('Initializing WebSocket with $url');
await socketHandler.setup(
url: url,
Expand Down Expand Up @@ -284,6 +279,7 @@ class RelayClient implements IRelayClient {
);
rethrow;
}
_connecting = false;
}

Future<void> _handleRelayClose(int? code, String? reason) async {
Expand Down Expand Up @@ -391,15 +387,25 @@ class RelayClient implements IRelayClient {
]) async {
dynamic response;

try {
// If we are connected and we know it send the message!
if (isConnected) {
response = await jsonRPC!.sendRequest(
method,
parameters,
id,
);
}
// If we are connecting, then wait for the connection to establish and then send the message
else if (_connecting) {
await _connectingFuture;
response = await jsonRPC!.sendRequest(
method,
parameters,
id,
);
} on StateError catch (_) {
// Reconnect to the websocket
core.logger.v('StateError, reconnecting: $_');
}
// If we aren't connected but should be (active), try to (re)connect and then send the message
else if (!isConnected && _active) {
await connect();
response = await jsonRPC!.sendRequest(
method,
Expand All @@ -408,6 +414,17 @@ class RelayClient implements IRelayClient {
);
}

// try {} on StateError catch (_) {
// // Reconnect to the websocket
// core.logger.v('StateError, reconnecting: $_');
// await connect();
// response = await jsonRPC!.sendRequest(
// method,
// parameters,
// id,
// );
// }

return response;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/apis/utils/constants.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class WalletConnectConstants {
static const SDK_VERSION = '2.1.1';
static const SDK_VERSION = '2.1.2';

static const CORE_PROTOCOL = 'wc';
static const CORE_VERSION = 2;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: walletconnect_flutter_v2
description: WalletConnect v2 client made in dart for flutter.
version: 2.1.1
version: 2.1.2
repository: https://github.com/WalletConnect/WalletConnectFlutterV2

environment:
Expand Down
28 changes: 16 additions & 12 deletions test/core_api/core_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ void main() {
topicMap: getTopicMap(core: core),
socketHandler: mockWebSocketHandler,
);
int errorCount = 0;
core.relayClient.onRelayClientError.subscribe((args) {
errorCount++;
expect(args!.error.message, 'No internet connection: test');
});

try {
await core.start();
expect(true, false);
} on WalletConnectError catch (e) {
expect(e.message, 'No internet connection: test');
}
await core.start();
expect(errorCount, 2);

verifyInOrder([
mockWebSocketHandler.setup(
Expand All @@ -56,6 +57,8 @@ void main() {
mockWebSocketHandler.connect(),
]);

core.relayClient.onRelayClientError.unsubscribeAll();

const testRelayUrl = 'wss://relay.test.com';
core = Core(
projectId: 'abc',
Expand All @@ -68,13 +71,13 @@ void main() {
topicMap: getTopicMap(core: core),
socketHandler: mockWebSocketHandler,
);
errorCount = 0;
core.relayClient.onRelayClientError.subscribe((args) {
errorCount++;
expect(args!.error.message, 'No internet connection: test');
});

try {
await core.start();
expect(true, false);
} on WalletConnectError catch (e) {
expect(e.message, 'No internet connection: test');
}
await core.start();

// Check that setup was called once for custom URL
verify(
Expand All @@ -86,6 +89,7 @@ void main() {
),
).called(1);
verify(mockWebSocketHandler.connect()).called(1);
expect(errorCount, 1);
});
});
}
64 changes: 6 additions & 58 deletions test/core_api/relay_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ void main() {
topicMap: getTopicMap(core: core),
socketHandler: mockWebSocketHandler,
);
int errorCounter = 0;
core.relayClient.onRelayClientError.subscribe((args) {
errorCounter++;
expect(args!.error.message, 'No internet connection: test');
});
await core.storage.init();
await core.crypto.init();
try {
await core.relayClient.init();
expect(true, false);
} on WalletConnectError catch (e) {
expect(e.message, 'No internet connection: test');
}
Expand All @@ -66,63 +70,7 @@ void main() {
),
)).called(1);
verify(mockWebSocketHandler.connect()).called(1);

// core = Core(
// projectId: 'abc',
// memoryStore: true,
// relayUrl: 'wss://relay.test.com',
// );
// core.relayClient = RelayClient(
// core: core,
// messageTracker: getMessageTracker(core: core),
// topicMap: getTopicMap(core: core),
// socketHandler: mockWebSocketHandler,
// );

// expect(
// () async => await core.start(),
// throwsA(
// isA<WalletConnectError>().having(
// (e) => e.message,
// 'No internet connection',
// 'No internet connection: test',
// ),
// ),
// );

// // Check that setup was called once for custom URL
// TODO: Figure out why the mocked class isn't counting the number of times it's called
// verify(mockWebSocketHandler.setup(url: anyNamed('url'))).called(1);
});

test('on init if the url fails', () async {
final MockWebSocketHandler mockWebSocketHandler = MockWebSocketHandler();
when(mockWebSocketHandler.connect()).thenThrow(const WalletConnectError(
code: -1,
message: 'No internet connection: test',
));

final ICore core = Core(
projectId: 'abc',
memoryStore: true,
);
core.relayClient = RelayClient(
core: core,
messageTracker: getMessageTracker(core: core),
topicMap: getTopicMap(core: core),
socketHandler: mockWebSocketHandler,
);

expect(
() async => await core.start(),
throwsA(
isA<WalletConnectError>().having(
(e) => e.message,
'No internet connection',
'No internet connection: test',
),
),
);
expect(errorCounter, 1);
});

test('when connection parameters are invalid', () async {
Expand Down

0 comments on commit fb2a199

Please sign in to comment.