Skip to content

Commit 505040d

Browse files
Add SDK initialization and create factory instance with JS interop
1 parent ab1b3c3 commit 505040d

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

splitio_web/lib/splitio_web.dart

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
import 'dart:async';
2+
import 'dart:js_interop';
3+
import 'dart:js_interop_unsafe';
14
import 'package:flutter_web_plugins/flutter_web_plugins.dart' show Registrar;
25
import 'package:splitio_platform_interface/splitio_platform_interface.dart';
6+
import 'package:splitio_web/src/js_interop.dart';
7+
import 'package:web/web.dart';
8+
9+
extension on Window {
10+
@JS()
11+
external JS_BrowserSDKPackage? splitio;
12+
}
313

414
/// Web implementation of [SplitioPlatform].
515
class SplitioWeb extends SplitioPlatform {
@@ -8,4 +18,84 @@ class SplitioWeb extends SplitioPlatform {
818
SplitioPlatform.instance = SplitioWeb();
919
}
1020

21+
// Future to queue method calls until SDK is initialized
22+
Future<void>? _initFuture;
23+
24+
late JS_IBrowserSDK _factory;
25+
26+
@override
27+
Future<void> init({
28+
required String apiKey,
29+
required String matchingKey,
30+
required String? bucketingKey,
31+
SplitConfiguration? sdkConfiguration,
32+
}) async {
33+
if (_initFuture == null) {
34+
_initFuture = this._init(apiKey: apiKey, matchingKey: matchingKey, bucketingKey: bucketingKey, sdkConfiguration: sdkConfiguration);
35+
}
36+
return _initFuture;
37+
}
38+
39+
Future<void> _init({
40+
required String apiKey,
41+
required String matchingKey,
42+
required String? bucketingKey,
43+
SplitConfiguration? sdkConfiguration,
44+
}) async {
45+
await _loadSplitSdk();
46+
47+
final config = _buildConfig(apiKey, matchingKey, bucketingKey, sdkConfiguration);
48+
49+
// Create factory instance
50+
this._factory = window.splitio!.SplitFactory.callAsFunction(null, config) as JS_IBrowserSDK;
51+
52+
return;
53+
}
54+
55+
// Checks whether the Split Browser SDK was manually loaded (`window.splitio != null`).
56+
// If not, loads it by injecting a script tag.
57+
static Future<void> _loadSplitSdk() async {
58+
if (window.splitio != null) {
59+
return; // Already loaded
60+
}
61+
62+
// Create and inject script tag
63+
final script = document.createElement('script') as HTMLScriptElement;
64+
script.type = 'text/javascript';
65+
script.src = 'packages/splitio_web/web/split-browser-1.6.0.full.min.js';
66+
67+
// Wait for script to load
68+
final completer = Completer<void>();
69+
70+
script.onload = (Event event) {
71+
completer.complete();
72+
}.toJS;
73+
74+
script.onerror = (Event event) {
75+
completer.completeError(Exception('Failed to load Split SDK'));
76+
}.toJS;
77+
78+
document.head!.appendChild(script);
79+
80+
await completer.future;
81+
82+
if (window.splitio == null) {
83+
throw Exception('Split Browser SDK failed to initialize after loading');
84+
}
85+
}
86+
87+
// Map SplitConfiguration to JS equivalent object
88+
JSObject _buildConfig(String apiKey, String matchingKey, String? bucketingKey, SplitConfiguration? configuration) {
89+
final core = JSObject();
90+
core.setProperty('authorizationKey'.toJS, apiKey.toJS);
91+
// @TODO: set bucketingKey if provided
92+
core.setProperty('key'.toJS, matchingKey.toJS);
93+
94+
final config = JSObject();
95+
config.setProperty('core'.toJS, core);
96+
97+
// @TODO: complete config
98+
return config;
99+
}
100+
11101
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'dart:js_interop';
2+
3+
@JS()
4+
extension type JS_IBrowserSDK._(JSObject _) implements JSObject {}
5+
6+
@JS()
7+
extension type JS_BrowserSDKPackage._(JSObject _) implements JSObject {
8+
external JSFunction SplitFactory;
9+
}

0 commit comments

Comments
 (0)