Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequestControllerTypedLink return subscription streams that can end #629

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/ferry/lib/src/request_controller_typed_link.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:ferry_exec/ferry_exec.dart';
import 'package:ferry/ferry.dart';
import 'package:gql_exec/gql_exec.dart';
import 'package:rxdart/rxdart.dart';

/// Allows multiple requests to be made by adding requests to the
Expand Down Expand Up @@ -57,7 +58,7 @@ class RequestControllerTypedLink extends TypedLink {
// If no stream has been cached for this request, create a new one.
ValueStream<OperationResponse<TData, TVars>>? prev;
var initial = true;
stream = requestController.stream
var requestStream = requestController.stream
.whereType<OperationRequest<TData, TVars>>()
.where(
(req) => req.requestId == null
Expand All @@ -71,7 +72,13 @@ class RequestControllerTypedLink extends TypedLink {
final sub = prev?.listen(null);
Future.delayed(Duration.zero, () => sub?.cancel());
},
).switchMap(
);
//Only use the first instance of the request stream for subscriptions, to ensure that a possible .done event is propagated.
//Note: This disables the paging and refetch feature for subscriptions.
if (request.operation.getOperationType() == OperationType.subscription) {
requestStream = requestStream.take(1);
}
stream = requestStream.switchMap(
(req) {
final stream = req.updateResult == null
? forward!(req)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:ferry_test_graphql2/queries/__generated__/review_by_id.req.gql.d
import 'package:ferry_test_graphql2/queries/__generated__/reviews.data.gql.dart';
import 'package:ferry_test_graphql2/queries/__generated__/reviews.req.gql.dart';
import 'package:ferry_test_graphql2/queries/__generated__/reviews.var.gql.dart';
import 'package:gql/language.dart' as gql;
import 'package:gql_exec/gql_exec.dart';
import 'package:rxdart/rxdart.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -468,6 +469,38 @@ void main() {
await client.dispose();
});
});

group('done event for subscriptions', () {
late TypedLink typedLink;

setUp(() {
typedLink = TypedLink.from([
RequestControllerTypedLink(),
TypedLink.function(<TData, TVars>(request, [next]) =>
Stream<OperationResponse<TData, TVars>>.empty()),
]);
});

test('onDone is propagated for subscriptions', () {
final stream = typedLink.request(
JsonOperationRequest(
fetchPolicy: FetchPolicy.NetworkOnly,
vars: {},
operation: Operation(
document: gql.parseString(r'''
subscription Sub {
reviews {
id
stars
}
}'''),
),
),
);

expect(stream, emitsInOrder([emitsDone]));
});
});
}

class _AutoResponderTerminalLink extends Link {
Expand Down