Skip to content

Commit 5ad2284

Browse files
Polishing
1 parent fd17fb7 commit 5ad2284

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

splitio_web/lib/splitio_web.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ class SplitioWeb extends SplitioPlatform {
2222

2323
late JS_IBrowserSDK _factory;
2424
String? _trafficType;
25+
// Broadcast to allow users to subscribe multiple listeners
2526
final StreamController<Impression> _impressionsStreamController =
26-
StreamController<Impression>();
27+
StreamController<Impression>.broadcast();
2728

2829
final Map<String, JS_IBrowserClient> _clients = {};
2930

@@ -86,7 +87,7 @@ class SplitioWeb extends SplitioPlatform {
8687
// If not, loads it by injecting a script tag.
8788
static Future<void> _loadSplitSdk() async {
8889
if (window.splitio != null) {
89-
return; // Already loaded
90+
return; // Already loaded. JS SDK should not be manually loaded because `splitio.SplitFactory` is not available.
9091
}
9192

9293
// Create and inject script tag
@@ -104,7 +105,7 @@ class SplitioWeb extends SplitioPlatform {
104105

105106
script.onerror = (Event event) {
106107
completer.completeError(
107-
Exception('Failed to load Split SDK, with error: $event'));
108+
Exception('Failed to load Split Browser SDK, with error: $event'));
108109
}.toJS;
109110

110111
document.head!.appendChild(script);
@@ -166,7 +167,7 @@ class SplitioWeb extends SplitioPlatform {
166167
urls.events =
167168
(configuration.configurationMap['eventsEndpoint'] as String).toJS;
168169

169-
// Convert urls for consistency between JS SDK and Android/iOS SDK
170+
// Convert urls for consistency between Browser SDK and Android/iOS SDK
170171
if (configuration.configurationMap.containsKey('authServiceEndpoint')) {
171172
final auth =
172173
configuration.configurationMap['authServiceEndpoint'] as String;
@@ -299,7 +300,7 @@ class SplitioWeb extends SplitioPlatform {
299300
config.storage = window.splitio!.InLocalStorage
300301
?.callAsFunction(null, storageOptions); // Browser SDK
301302
} else {
302-
config.storage = storageOptions; // JS SDK
303+
config.storage = storageOptions; // JS or slim Browser SDK
303304
}
304305

305306
if (configuration.configurationMap['impressionListener'] is bool) {
@@ -437,7 +438,6 @@ class SplitioWeb extends SplitioPlatform {
437438
Map<String, dynamic> attributes = const {},
438439
EvaluationOptions evaluationOptions = const EvaluationOptions.empty(),
439440
}) async {
440-
await this._initFuture;
441441
final client = await _getClient(
442442
matchingKey: matchingKey,
443443
bucketingKey: bucketingKey,
@@ -747,7 +747,7 @@ class SplitioWeb extends SplitioPlatform {
747747
// the `onXXX` method implementations always return a Future or Stream that waits for the client to be initialized.
748748

749749
@override
750-
Future<void>? onReady(
750+
Future<void> onReady(
751751
{required String matchingKey, required String? bucketingKey}) async {
752752
final client = await _getClient(
753753
matchingKey: matchingKey,
@@ -770,7 +770,7 @@ class SplitioWeb extends SplitioPlatform {
770770
}
771771

772772
@override
773-
Future<void>? onReadyFromCache(
773+
Future<void> onReadyFromCache(
774774
{required String matchingKey, required String? bucketingKey}) async {
775775
final client = await _getClient(
776776
matchingKey: matchingKey,
@@ -793,7 +793,7 @@ class SplitioWeb extends SplitioPlatform {
793793
}
794794

795795
@override
796-
Future<void>? onTimeout(
796+
Future<void> onTimeout(
797797
{required String matchingKey, required String? bucketingKey}) async {
798798
final client = await _getClient(
799799
matchingKey: matchingKey,
@@ -816,7 +816,7 @@ class SplitioWeb extends SplitioPlatform {
816816
}
817817

818818
@override
819-
Stream<void>? onUpdated(
819+
Stream<void> onUpdated(
820820
{required String matchingKey, required String? bucketingKey}) {
821821
// To ensure the public `onUpdated` callback and `whenUpdated` method work correctly,
822822
// this method always return a stream, and the StreamController callbacks
@@ -843,6 +843,7 @@ class SplitioWeb extends SplitioPlatform {
843843
client.off(client.Event.SDK_UPDATE, jsCallback);
844844
};
845845

846+
// No broadcast to support pause and resume of individual subscriptions
846847
controller = StreamController<void>(
847848
onListen: registerJsCallback,
848849
onPause: deregisterJsCallback,

splitio_web/test/splitio_web_test.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -990,9 +990,9 @@ void main() {
990990

991991
group('events', () {
992992
test('onReady (SDK_READY event is emitted after onReady is called)', () {
993-
Future<void>? onReady = _platform
993+
Future<void> onReady = _platform
994994
.onReady(matchingKey: 'matching-key', bucketingKey: 'bucketing-key')
995-
?.then((value) => true);
995+
.then((value) => true);
996996

997997
// Emit SDK_READY event
998998
final mockClient =
@@ -1010,22 +1010,22 @@ void main() {
10101010
mock.mockFactory.client(buildJsKey('matching-key', 'bucketing-key'));
10111011
mockClient.emit(mockClient.Event.SDK_READY_FROM_CACHE);
10121012

1013-
Future<void>? onReadyFromCache = _platform
1013+
Future<void> onReadyFromCache = _platform
10141014
.onReadyFromCache(
10151015
matchingKey: 'matching-key', bucketingKey: 'bucketing-key')
1016-
?.then((value) => true);
1016+
.then((value) => true);
10171017

10181018
expect(onReadyFromCache, completion(equals(true)));
10191019
});
10201020

10211021
test('onTimeout (in multiple clients)', () async {
1022-
Future<void>? onTimeout = _platform
1022+
Future<void> onTimeout = _platform
10231023
.onTimeout(matchingKey: 'matching-key', bucketingKey: 'bucketing-key')
1024-
?.then((value) => true);
1024+
.then((value) => true);
10251025

1026-
Future<void>? onTimeoutClient2 = _platform
1026+
Future<void> onTimeoutClient2 = _platform
10271027
.onTimeout(matchingKey: 'matching-key-2', bucketingKey: null)
1028-
?.then((value) => false);
1028+
.then((value) => false);
10291029

10301030
// Emit SDK_READY_TIMED_OUT event on the first client
10311031
final mockClient =
@@ -1042,7 +1042,7 @@ void main() {
10421042
mock.mockFactory.client(buildJsKey('matching-key', 'bucketing-key'));
10431043

10441044
final stream = _platform.onUpdated(
1045-
matchingKey: 'matching-key', bucketingKey: 'bucketing-key')!;
1045+
matchingKey: 'matching-key', bucketingKey: 'bucketing-key');
10461046
final subscription = stream.listen(expectAsync1((_) {}, count: 3));
10471047
await Future<void>.delayed(Duration.zero); // onListen is async
10481048

0 commit comments

Comments
 (0)