@@ -45,6 +45,38 @@ final class IdentifyError implements IdentifyResult {
45
45
IdentifyError (this .error);
46
46
}
47
47
48
+ typedef DataSourceFactoriesFn = Map <ConnectionMode , DataSourceFactory > Function (
49
+ LDCommonConfig config, LDLogger logger, HttpProperties httpProperties);
50
+
51
+ Map <ConnectionMode , DataSourceFactory > _defaultFactories (
52
+ LDCommonConfig config, LDLogger logger, HttpProperties httpProperties) {
53
+ return {
54
+ ConnectionMode .streaming: (LDContext context) {
55
+ return StreamingDataSource (
56
+ credential: config.sdkCredential,
57
+ context: context,
58
+ endpoints: config.serviceEndpoints,
59
+ logger: logger,
60
+ dataSourceConfig: StreamingDataSourceConfig (
61
+ useReport: config.dataSourceConfig.useReport,
62
+ withReasons: config.dataSourceConfig.evaluationReasons),
63
+ httpProperties: httpProperties);
64
+ },
65
+ ConnectionMode .polling: (LDContext context) {
66
+ return PollingDataSource (
67
+ credential: config.sdkCredential,
68
+ context: context,
69
+ endpoints: config.serviceEndpoints,
70
+ logger: logger,
71
+ dataSourceConfig: PollingDataSourceConfig (
72
+ useReport: config.dataSourceConfig.useReport,
73
+ withReasons: config.dataSourceConfig.evaluationReasons,
74
+ pollingInterval: config.dataSourceConfig.polling.pollingInterval),
75
+ httpProperties: httpProperties);
76
+ },
77
+ };
78
+ }
79
+
48
80
final class LDCommonClient {
49
81
final LDCommonConfig _config;
50
82
final Persistence _persistence;
@@ -58,6 +90,7 @@ final class LDCommonClient {
58
90
late final DataSourceManager _dataSourceManager;
59
91
late final EnvironmentReport _envReport;
60
92
late final AsyncSingleQueue <void > _identifyQueue = AsyncSingleQueue ();
93
+ late final DataSourceFactoriesFn _dataSourceFactories;
61
94
62
95
// Modifications will happen in the order they are specified in this list.
63
96
// If there are cross-dependent modifiers, then this must be considered.
@@ -93,7 +126,8 @@ final class LDCommonClient {
93
126
}
94
127
95
128
LDCommonClient (LDCommonConfig commonConfig, CommonPlatform platform,
96
- LDContext context, DiagnosticSdkData sdkData)
129
+ LDContext context, DiagnosticSdkData sdkData,
130
+ {DataSourceFactoriesFn ? dataSourceFactories})
97
131
: _config = commonConfig,
98
132
_platform = platform,
99
133
_persistence = ValidatingPersistence (
@@ -107,6 +141,8 @@ final class LDCommonClient {
107
141
persistence: platform.persistence),
108
142
_dataSourceStatusManager = DataSourceStatusManager (),
109
143
_initialUndecoratedContext = context,
144
+ // Data source factories is primarily a mechanism for testing.
145
+ _dataSourceFactories = dataSourceFactories ?? _defaultFactories,
110
146
_sdkData = sdkData {
111
147
final dataSourceEventHandler = DataSourceEventHandler (
112
148
flagManager: _flagManager,
@@ -179,7 +215,7 @@ final class LDCommonClient {
179
215
///
180
216
/// If the return value is true, then the SDK has initialized, if false
181
217
/// then the SDK has encountered an unrecoverable error.
182
- Future <bool > start () {
218
+ Future <bool > start ({ bool waitForNetworkResults = false } ) {
183
219
if (_startFuture != null ) {
184
220
return _startFuture! .then (_mapIdentifyStart);
185
221
}
@@ -191,7 +227,8 @@ final class LDCommonClient {
191
227
// having been set resulting in a crash.
192
228
_identifyQueue.execute (() async {
193
229
await _startInternal ();
194
- await _identifyInternal (_initialUndecoratedContext);
230
+ await _identifyInternal (_initialUndecoratedContext,
231
+ waitForNetworkResults: waitForNetworkResults);
195
232
}).then ((res) {
196
233
_startCompleter! .complete (_mapIdentifyResult (res));
197
234
});
@@ -259,32 +296,8 @@ final class LDCommonClient {
259
296
_updateEventSendingState ();
260
297
261
298
if (! _config.offline) {
262
- _dataSourceManager.setFactories ({
263
- ConnectionMode .streaming: (LDContext context) {
264
- return StreamingDataSource (
265
- credential: _config.sdkCredential,
266
- context: context,
267
- endpoints: _config.serviceEndpoints,
268
- logger: _logger,
269
- dataSourceConfig: StreamingDataSourceConfig (
270
- useReport: _config.dataSourceConfig.useReport,
271
- withReasons: _config.dataSourceConfig.evaluationReasons),
272
- httpProperties: httpProperties);
273
- },
274
- ConnectionMode .polling: (LDContext context) {
275
- return PollingDataSource (
276
- credential: _config.sdkCredential,
277
- context: context,
278
- endpoints: _config.serviceEndpoints,
279
- logger: _logger,
280
- dataSourceConfig: PollingDataSourceConfig (
281
- useReport: _config.dataSourceConfig.useReport,
282
- withReasons: _config.dataSourceConfig.evaluationReasons,
283
- pollingInterval:
284
- _config.dataSourceConfig.polling.pollingInterval),
285
- httpProperties: httpProperties);
286
- },
287
- });
299
+ _dataSourceManager
300
+ .setFactories (_dataSourceFactories (_config, _logger, httpProperties));
288
301
} else {
289
302
_dataSourceManager.setFactories ({
290
303
ConnectionMode .streaming: (LDContext context) {
@@ -350,7 +363,8 @@ final class LDCommonClient {
350
363
/// When the context is changed, the SDK will load flag values for the context from a local cache if available, while
351
364
/// initiating a connection to retrieve the most current flag values. An event will be queued to be sent to the service
352
365
/// containing the public [LDContext] fields for indexing on the dashboard.
353
- Future <IdentifyResult > identify (LDContext context) async {
366
+ Future <IdentifyResult > identify (LDContext context,
367
+ {bool waitForNetworkResults = false }) async {
354
368
if (_startFuture == null ) {
355
369
const message =
356
370
'Identify called before SDK has been started. Start the SDK before '
@@ -359,7 +373,8 @@ final class LDCommonClient {
359
373
return IdentifyError (Exception (message));
360
374
}
361
375
final res = await _identifyQueue.execute (() async {
362
- await _identifyInternal (context);
376
+ await _identifyInternal (context,
377
+ waitForNetworkResults: waitForNetworkResults);
363
378
});
364
379
return _mapIdentifyResult (res);
365
380
}
@@ -375,19 +390,19 @@ final class LDCommonClient {
375
390
}
376
391
}
377
392
378
- Future <void > _identifyInternal (LDContext context) async {
393
+ Future <void > _identifyInternal (LDContext context,
394
+ {bool waitForNetworkResults = false }) async {
379
395
await _setAndDecorateContext (context);
380
396
final completer = Completer <void >();
381
397
_eventProcessor? .processIdentifyEvent (IdentifyEvent (context: _context));
382
398
final loadedFromCache = await _flagManager.loadCached (_context);
383
399
384
400
if (_config.offline) {
385
- // TODO: Do we need to do anything different here?
386
401
return ;
387
402
}
388
403
_dataSourceManager.identify (_context, completer);
389
404
390
- if (loadedFromCache) {
405
+ if (loadedFromCache && ! waitForNetworkResults ) {
391
406
return ;
392
407
}
393
408
return completer.future;
0 commit comments