22library launchdarkly_sse;
33
44import 'dart:async' ;
5+ import 'dart:collection' ;
56
67import 'src/http_consts.dart' ;
78import 'src/logging.dart' ;
8- import 'src/message_event .dart' ;
9+ import 'src/events .dart' ;
910import 'src/sse_client_stub.dart'
1011 if (dart.library.io) 'src/sse_client_http.dart'
1112 if (dart.library.js_interop) 'src/sse_client_html.dart' ;
13+ import 'src/test_sse_client.dart' ;
1214
13- export 'src/message_event.dart' show MessageEvent;
15+ export 'src/events.dart' show Event, MessageEvent, OpenEvent;
16+ export 'src/test_sse_client.dart' show TestSseClient;
1417export 'src/logging.dart'
1518 show EventSourceLogger, LogLevel, NoOpLogger, PrintLogger;
1619
@@ -32,10 +35,11 @@ enum SseHttpMethod {
3235
3336/// An [SSEClient] that works to maintain a SSE connection to a server.
3437///
35- /// You can receive [MessageEvent] s by listening to the [stream] object. The SSEClient will
36- /// connect when there is a nonzero number of subscribers on [stream] and will disconnect when
37- /// there are zero subscribers on [stream] . In certain cases, unrecoverable errors will be
38- /// reported on the [stream] at which point the stream will be done.
38+ /// You can receive [Events] s by listening to the [stream] object. The SSEClient
39+ /// will connect when there is a nonzero number of subscribers on the [stream]
40+ /// and will disconnect when there are zero subscribers on the [stream] .
41+ /// In certain cases, unrecoverable errors will be reported on the [stream] at
42+ /// which point the stream will be done.
3943///
4044/// The [SSEClient] will make best effort to maintain the streaming connection.
4145abstract class SSEClient {
@@ -47,9 +51,9 @@ abstract class SSEClient {
4751 static const defaultConnectTimeout = Duration (seconds: 30 );
4852 static const defaultReadTimeout = Duration (minutes: 5 );
4953
50- /// Subscribe to this [stream] to receive events and sometimes errors. The first
54+ /// Subscribe to this [stream] to receive events and sometimes errors.
5155 /// subscribe triggers the connection, so expect network delay initially.
52- Stream <MessageEvent > get stream;
56+ Stream <Event > get stream;
5357
5458 /// Closes the SSEClient and tears down connections and resources. Do not use the
5559 /// SSEClient after close is called, behavior is undefined at that point.
@@ -97,4 +101,33 @@ abstract class SSEClient {
97101 return getSSEClient (uri, eventTypes, mergedHeaders, connectTimeout,
98102 readTimeout, body, httpMethod.toString (), logger);
99103 }
104+
105+ /// Get an SSE client for use in unit tests.
106+ ///
107+ /// Most parameters are the same as those of the main SSEClient factory, but
108+ /// the test client supports an additional property which is the [sourceStream] .
109+ /// Events sent to the [sourceStream] will also be emitted by the event source
110+ /// if the event source has listeners. When a user unsubscribes from the event
111+ /// stream, then the test client will unsubscribe from the source stream.
112+ ///
113+ /// This method is primarily for use the the LaunchDarkly SDK implementation.
114+ /// Changes may be made to this API without following semantic conventions.
115+ static TestSseClient testClient (
116+ Uri uri,
117+ Set <String > eventTypes, {
118+ Map <String , String > headers = defaultHeaders,
119+ Duration connectTimeout = defaultConnectTimeout,
120+ Duration readTimeout = defaultReadTimeout,
121+ String ? body,
122+ SseHttpMethod httpMethod = SseHttpMethod .get ,
123+ Stream <Event >? sourceStream,
124+ }) {
125+ return TestSseClient .internal (
126+ headers: UnmodifiableMapView (headers),
127+ connectTimeout: connectTimeout,
128+ readTimeout: readTimeout,
129+ body: body,
130+ httpMethod: httpMethod,
131+ sourceStream: sourceStream);
132+ }
100133}
0 commit comments