Skip to content

Commit d1b4eeb

Browse files
authored
fix: onInitialization to wait for initialization to the complete and config received (#160)
1 parent be0afcc commit d1b4eeb

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

android/src/main/kotlin/com/devcycle/devcycle_flutter_client_sdk/DevCycleFlutterClientSdkPlugin.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,18 @@ class DevCycleFlutterClientSdkPlugin: FlutterPlugin, MethodCallHandler {
7171

7272
client.onInitialized(object : DevCycleCallback<String> {
7373
override fun onSuccess(result: String) {
74+
args["isInitialized"] = true
7475
callFlutter("clientInitialized", args)
76+
res.success(null)
7577
}
7678

7779
override fun onError(t: Throwable) {
7880
args["error"] = t.message
81+
args["isInitialized"] = false
7982
callFlutter("clientInitialized", args)
83+
res.error("error", t.message, null)
8084
}
8185
})
82-
res.success(null)
8386
}
8487
"identifyUser" -> {
8588
val user = getUserFromMap(call.argument("user")!!)
@@ -196,7 +199,11 @@ class DevCycleFlutterClientSdkPlugin: FlutterPlugin, MethodCallHandler {
196199
if (anonymous is Boolean) userBuilder.withIsAnonymous(anonymous)
197200

198201
val userId = map["userId"] as? String
199-
if (userId is String) userBuilder.withUserId(userId)
202+
if (userId is String) {
203+
android.util.Log.e("init", map["name"].toString());
204+
userBuilder.withUserId(userId)
205+
userBuilder.withIsAnonymous(false)
206+
} else userBuilder.withIsAnonymous(true)
200207

201208
val email = map["email"] as? String
202209
if (email is String) userBuilder.withEmail(email)

example/pubspec.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ version: 0.0.1
44

55
# The following line prevents the package from being accidentally published to
66
# pub.dev using `flutter pub publish`. This is preferred for private packages.
7-
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
7+
publish_to: "none" # Remove this line if you wish to publish to pub.dev
88

99
environment:
10-
sdk: '>=2.14.0 <3.0.0'
10+
sdk: ">=2.17.0 <3.0.0"
1111

1212
# Dependencies specify other packages that your package needs in order to work.
1313
# To automatically upgrade your package dependencies to the latest versions
@@ -48,7 +48,6 @@ dev_dependencies:
4848

4949
# The following section is specific to Flutter packages.
5050
flutter:
51-
5251
# The following line ensures that the Material Icons font is
5352
# included with your application, so that you can use the icons in
5453
# the material Icons class.

ios/Classes/SwiftDevCycleFlutterClientSdkPlugin.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ public class SwiftDevCycleFlutterClientSdkPlugin: NSObject, FlutterPlugin {
3939
.user(dvcUser)
4040
.options(getOptionsFromDict(dict: options ?? [:] ))
4141
.build(onInitialized: { error in
42+
callbackArgs["isInitialized"] = true
4243
if (error != nil) {
4344
callbackArgs["error"] = "\(String(describing: error))"
45+
callbackArgs["isInitialized"] = false
4446
}
4547
self.channel.invokeMethod("clientInitialized", arguments: callbackArgs)
4648
})

lib/devcycle_flutter_client_sdk.dart

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:devcycle_flutter_client_sdk/devcycle_event.dart';
22
import 'package:flutter/services.dart';
33
import 'package:uuid/uuid.dart';
44
import 'package:logger/logger.dart';
5+
import 'dart:async';
56

67
import 'devcycle_flutter_client_sdk_platform_interface.dart';
78
import 'devcycle_user.dart';
@@ -27,6 +28,9 @@ class DevCycleClient {
2728

2829
Future<void>? _clientReady;
2930

31+
bool _isInitialized = false;
32+
Completer<void>? _initializationCompleter;
33+
3034
/// Callback triggered on client initialization
3135
ErrorCallback? _clientInitializedCallback;
3236
// Map of variable keys to a list of variable objects, used to update variable values
@@ -61,9 +65,14 @@ class DevCycleClient {
6165

6266
switch (call.method) {
6367
case 'clientInitialized':
68+
_isInitialized = call.arguments['isInitialized'];
6469
if (_clientInitializedCallback != null) {
6570
_clientInitializedCallback!(error);
6671
}
72+
// Complete the initialization
73+
if (_isInitialized) {
74+
_initializationCompleter?.complete();
75+
}
6776
break;
6877
case 'variableUpdated':
6978
final key = call.arguments['key'];
@@ -128,8 +137,13 @@ class DevCycleClient {
128137
}
129138
}
130139

131-
DevCycleClient onInitialized(ErrorCallback callback) {
140+
Future<DevCycleClient> onInitialized(ErrorCallback callback) async {
132141
_clientInitializedCallback = callback;
142+
_initializationCompleter = Completer<void>();
143+
144+
// Wait for the initialization to complete
145+
await _initializationCompleter?.future;
146+
133147
return this;
134148
}
135149

@@ -138,7 +152,8 @@ class DevCycleClient {
138152
return DevCycleFlutterClientSdkPlatform.instance.getPlatformVersion();
139153
}
140154

141-
Future<void> identifyUser(DevCycleUser user, [VariablesCallback? callback]) async {
155+
Future<void> identifyUser(DevCycleUser user,
156+
[VariablesCallback? callback]) async {
142157
await _clientReady;
143158
if (callback != null) {
144159
String callbackId = _uuid.v4();
@@ -244,4 +259,4 @@ class DevCycleClientBuilder {
244259
}
245260

246261
@Deprecated('Use DevCycleClientBuilder instead')
247-
typedef DVCClientBuilder = DevCycleClientBuilder;
262+
typedef DVCClientBuilder = DevCycleClientBuilder;

0 commit comments

Comments
 (0)