Skip to content

Commit

Permalink
Merge pull request zino-hofmann#534 from kulture-rob/beta
Browse files Browse the repository at this point in the history
allow initPayload to be a future
  • Loading branch information
mainawycliffe authored Jan 14, 2020
2 parents ff3738f + 614279d commit 07132bc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
13 changes: 8 additions & 5 deletions packages/graphql/lib/src/socket_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ class SocketClientConfig {

/// The initial payload that will be sent to the server upon connection.
/// Can be null, but must be a valid json structure if provided.
final dynamic initPayload;
final FutureOr<dynamic> initPayload;

InitOperation get initOperation => InitOperation(initPayload);
Future<InitOperation> get initOperation async {
dynamic payload = await initPayload;
return InitOperation(payload);
}
}

enum SocketConnectionState { NOT_CONNECTED, CONNECTING, CONNECTED }
Expand Down Expand Up @@ -105,7 +108,7 @@ class SocketClient {
);
_connectionStateController.value = SocketConnectionState.CONNECTED;
print('Connected to websocket.');
_write(config.initOperation);
_write(await config.initOperation);

_messageStream =
_socket.stream.map<GraphQLSocketMessage>(_parseSocketMessage);
Expand Down Expand Up @@ -259,8 +262,8 @@ class SocketClient {
config.queryAndMutationTimeout != null;

final onListen = () {
final Stream<SocketConnectionState>
waitForConnectedStateWithoutTimeout = _connectionStateController
final Stream<SocketConnectionState> waitForConnectedStateWithoutTimeout =
_connectionStateController
.startWith(
waitForConnection ? null : SocketConnectionState.CONNECTED)
.where((SocketConnectionState state) =>
Expand Down
56 changes: 55 additions & 1 deletion packages/graphql/test/socket_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:test/test.dart';
import 'helpers.dart';

void main() {
group('SocketClient', () {
group('SocketClient without payload', () {
SocketClient socketClient;
setUp(overridePrint((log) {
socketClient = SocketClient(
Expand Down Expand Up @@ -118,4 +118,58 @@ void main() {
);
});
}, tags: "integration");

group('SocketClient with const payload', () {
SocketClient socketClient;
const initPayload = {'token': 'mytoken'};

setUp(overridePrint((log) {
socketClient = SocketClient(
'ws://echo.websocket.org',
config: SocketClientConfig(initPayload: initPayload),
);
}));

tearDown(overridePrint((log) async {
await socketClient.dispose();
}));

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

await expectLater(socketClient.socket.stream.map((s) {
return jsonDecode(s)['payload'];
}), emits(initPayload));
});
});

group('SocketClient with future payload', () {
SocketClient socketClient;
const initPayload = {'token': 'mytoken'};

setUp(overridePrint((log) {
socketClient = SocketClient(
'ws://echo.websocket.org',
config: SocketClientConfig(
initPayload: Future.delayed(Duration(seconds: 3), () => initPayload),
),
);
}));

tearDown(overridePrint((log) async {
await socketClient.dispose();
}));

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

await expectLater(socketClient.socket.stream.map((s) {
return jsonDecode(s)['payload'];
}), emits(initPayload));
});
});
}

0 comments on commit 07132bc

Please sign in to comment.