Skip to content

Commit 40b75e9

Browse files
authored
Use SplitioPlatform interface (#46)
1 parent ec0e716 commit 40b75e9

File tree

9 files changed

+477
-207
lines changed

9 files changed

+477
-207
lines changed

splitio/example/pubspec.lock

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ packages:
109109
url: "https://pub.dartlang.org"
110110
source: hosted
111111
version: "1.8.0"
112+
plugin_platform_interface:
113+
dependency: transitive
114+
description:
115+
name: plugin_platform_interface
116+
url: "https://pub.dartlang.org"
117+
source: hosted
118+
version: "2.1.2"
112119
sky_engine:
113120
dependency: transitive
114121
description: flutter
@@ -128,6 +135,13 @@ packages:
128135
relative: true
129136
source: path
130137
version: "0.1.1"
138+
splitio_platform_interface:
139+
dependency: transitive
140+
description:
141+
path: "../../splitio_platform_interface"
142+
relative: true
143+
source: path
144+
version: "1.0.0"
131145
stack_trace:
132146
dependency: transitive
133147
description:

splitio/lib/split_client.dart

Lines changed: 76 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import 'package:flutter/foundation.dart';
2-
import 'package:splitio/channel/method_channel_manager.dart';
3-
import 'package:splitio/events/split_events_listener.dart';
4-
import 'package:splitio/events/split_method_call_handler.dart';
5-
import 'package:splitio/split_result.dart';
2+
import 'package:splitio_platform_interface/split_result.dart';
3+
import 'package:splitio_platform_interface/splitio_platform_interface.dart';
64

75
abstract class SplitClient {
86
/// Performs an evaluation for the [splitName] feature.
@@ -142,201 +140,157 @@ abstract class SplitClient {
142140
}
143141

144142
class DefaultSplitClient implements SplitClient {
145-
static const String _controlTreatment = 'control';
146-
static const SplitResult _controlResult =
147-
SplitResult(_controlTreatment, null);
148-
149-
final MethodChannelManager _methodChannelManager;
150-
late final SplitEventMethodCallHandler _methodCallHandler;
143+
final SplitioPlatform _platform;
151144
final String _matchingKey;
152145
final String? _bucketingKey;
153146

154-
late final SplitEventsListener _splitEventsListener;
155-
156-
DefaultSplitClient(
157-
this._methodChannelManager, this._matchingKey, this._bucketingKey) {
158-
_methodCallHandler =
159-
SplitEventMethodCallHandler(_matchingKey, _bucketingKey, this);
160-
_splitEventsListener =
161-
DefaultEventsListener(_methodChannelManager, _methodCallHandler);
162-
}
147+
DefaultSplitClient(this._platform, this._matchingKey, this._bucketingKey);
163148

164149
@visibleForTesting
165-
DefaultSplitClient.withEventListener(this._methodChannelManager,
166-
this._matchingKey, this._bucketingKey, this._splitEventsListener);
150+
DefaultSplitClient.withEventListener(
151+
this._platform, this._matchingKey, this._bucketingKey);
167152

168153
@override
169154
Future<String> getTreatment(String splitName,
170155
[Map<String, dynamic> attributes = const {}]) async {
171-
return await _methodChannelManager.invokeMethod(
172-
'getTreatment',
173-
_buildParameters(
174-
{'splitName': splitName, 'attributes': attributes})) ??
175-
_controlTreatment;
156+
return _platform.getTreatment(
157+
matchingKey: _matchingKey,
158+
bucketingKey: _bucketingKey,
159+
splitName: splitName,
160+
attributes: attributes);
176161
}
177162

178163
@override
179164
Future<SplitResult> getTreatmentWithConfig(String splitName,
180165
[Map<String, dynamic> attributes = const {}]) async {
181-
Map? treatment = (await _methodChannelManager.invokeMapMethod(
182-
'getTreatmentWithConfig',
183-
_buildParameters(
184-
{'splitName': splitName, 'attributes': attributes})))
185-
?.entries
186-
.first
187-
.value;
188-
if (treatment == null) {
189-
return _controlResult;
190-
}
191-
192-
return SplitResult(treatment['treatment'], treatment['config']);
166+
return _platform.getTreatmentWithConfig(
167+
matchingKey: _matchingKey,
168+
bucketingKey: _bucketingKey,
169+
splitName: splitName,
170+
attributes: attributes);
193171
}
194172

195173
@override
196174
Future<Map<String, String>> getTreatments(List<String> splitNames,
197175
[Map<String, dynamic> attributes = const {}]) async {
198-
Map? treatments = await _methodChannelManager.invokeMapMethod(
199-
'getTreatments',
200-
_buildParameters({'splitName': splitNames, 'attributes': attributes}));
201-
202-
return treatments
203-
?.map((key, value) => MapEntry<String, String>(key, value)) ??
204-
{for (var item in splitNames) item: _controlTreatment};
176+
return _platform.getTreatments(
177+
matchingKey: _matchingKey,
178+
bucketingKey: _bucketingKey,
179+
splitNames: splitNames,
180+
attributes: attributes);
205181
}
206182

207183
@override
208184
Future<Map<String, SplitResult>> getTreatmentsWithConfig(
209185
List<String> splitNames,
210186
[Map<String, dynamic> attributes = const {}]) async {
211-
Map? treatments = await _methodChannelManager.invokeMapMethod(
212-
'getTreatmentsWithConfig',
213-
_buildParameters({'splitName': splitNames, 'attributes': attributes}));
214-
215-
return treatments?.map((key, value) =>
216-
MapEntry(key, SplitResult(value['treatment'], value['config']))) ??
217-
{for (var item in splitNames) item: _controlResult};
187+
return _platform.getTreatmentsWithConfig(
188+
matchingKey: _matchingKey,
189+
bucketingKey: _bucketingKey,
190+
splitNames: splitNames,
191+
attributes: attributes);
218192
}
219193

220194
@override
221195
Future<bool> track(String eventType,
222196
{String? trafficType,
223197
double? value,
224198
Map<String, dynamic> properties = const {}}) async {
225-
var parameters = _buildParameters({'eventType': eventType});
226-
227-
if (trafficType != null) {
228-
parameters['trafficType'] = trafficType;
229-
}
230-
231-
if (value != null) {
232-
parameters['value'] = value;
233-
}
234-
235-
try {
236-
return await _methodChannelManager.invokeMethod("track", parameters)
237-
as bool;
238-
} on Exception catch (_) {
239-
return false;
240-
}
199+
return _platform.track(
200+
matchingKey: _matchingKey,
201+
bucketingKey: _bucketingKey,
202+
eventType: eventType,
203+
trafficType: trafficType,
204+
value: value,
205+
properties: properties);
241206
}
242207

243208
@override
244209
Future<bool> setAttribute(String attributeName, dynamic value) async {
245-
var result = await _methodChannelManager.invokeMethod('setAttribute',
246-
_buildParameters({'attributeName': attributeName, 'value': value}));
247-
248-
if (result is bool) {
249-
return result;
250-
}
251-
252-
return false;
210+
return _platform.setAttribute(
211+
matchingKey: _matchingKey,
212+
bucketingKey: _bucketingKey,
213+
attributeName: attributeName,
214+
value: value);
253215
}
254216

255217
@override
256218
Future<dynamic> getAttribute(String attributeName) async {
257-
return _methodChannelManager.invokeMethod(
258-
'getAttribute', _buildParameters({'attributeName': attributeName}));
219+
return _platform.getAttribute(
220+
matchingKey: _matchingKey,
221+
bucketingKey: _bucketingKey,
222+
attributeName: attributeName);
259223
}
260224

261225
@override
262226
Future<bool> setAttributes(Map<String, dynamic> attributes) async {
263-
var result = await _methodChannelManager.invokeMethod(
264-
'setAttributes', _buildParameters({'attributes': attributes}));
265-
266-
if (result is bool) {
267-
return result;
268-
}
269-
270-
return false;
227+
return _platform.setAttributes(
228+
matchingKey: _matchingKey,
229+
bucketingKey: _bucketingKey,
230+
attributes: attributes);
271231
}
272232

273233
@override
274234
Future<Map<String, dynamic>> getAttributes() async {
275-
return (await _methodChannelManager.invokeMapMethod(
276-
'getAllAttributes', _buildParameters()))
277-
?.map((key, value) => MapEntry<String, Object?>(key, value)) ??
278-
{};
235+
return _platform.getAllAttributes(
236+
matchingKey: _matchingKey, bucketingKey: _bucketingKey);
279237
}
280238

281239
@override
282240
Future<bool> removeAttribute(String attributeName) async {
283-
return await _methodChannelManager.invokeMethod(
284-
'removeAttribute', _buildParameters({'attributeName': attributeName}));
241+
return _platform.removeAttribute(
242+
matchingKey: _matchingKey,
243+
bucketingKey: _bucketingKey,
244+
attributeName: attributeName);
285245
}
286246

287247
@override
288248
Future<bool> clearAttributes() async {
289-
return await _methodChannelManager.invokeMethod(
290-
'clearAttributes', _buildParameters());
249+
return _platform.clearAttributes(
250+
matchingKey: _matchingKey, bucketingKey: _bucketingKey);
291251
}
292252

293253
@override
294254
Future<void> flush() async {
295-
return _methodChannelManager.invokeMethod('flush', _buildParameters());
255+
return _platform.flush(
256+
matchingKey: _matchingKey, bucketingKey: _bucketingKey);
296257
}
297258

298259
@override
299260
Future<void> destroy() async {
300-
_splitEventsListener.destroy();
301-
return _methodChannelManager.invokeMethod('destroy', _buildParameters());
261+
return _platform.destroy(
262+
matchingKey: _matchingKey, bucketingKey: _bucketingKey);
302263
}
303264

304265
@override
305-
Future<SplitClient> whenReady() {
306-
return _splitEventsListener.onReady();
266+
Future<SplitClient> whenReady() async {
267+
await _platform.onReady(
268+
matchingKey: _matchingKey, bucketingKey: _bucketingKey);
269+
270+
return Future.value(this);
307271
}
308272

309273
@override
310-
Future<SplitClient> whenReadyFromCache() {
311-
return _splitEventsListener.onReadyFromCache();
274+
Future<SplitClient> whenReadyFromCache() async {
275+
await _platform.onReadyFromCache(
276+
matchingKey: _matchingKey, bucketingKey: _bucketingKey);
277+
278+
return Future.value(this);
312279
}
313280

314281
@override
315282
Stream<SplitClient> whenUpdated() {
316-
return _splitEventsListener.onUpdated();
283+
return _platform
284+
.onUpdated(matchingKey: _matchingKey, bucketingKey: _bucketingKey)
285+
?.map((event) => this) ??
286+
const Stream.empty();
317287
}
318288

319289
@override
320-
Future<SplitClient> whenTimeout() {
321-
return _splitEventsListener.onTimeout();
322-
}
323-
324-
Map<String, String> _getKeysMap() {
325-
Map<String, String> result = {'matchingKey': _matchingKey};
326-
327-
if (_bucketingKey != null) {
328-
result.addAll({'bucketingKey': _bucketingKey!});
329-
}
330-
331-
return result;
332-
}
333-
334-
Map<String, dynamic> _buildParameters(
335-
[Map<String, dynamic> parameters = const {}]) {
336-
Map<String, dynamic> result = {};
337-
result.addAll(parameters);
338-
result.addAll(_getKeysMap());
290+
Future<SplitClient> whenTimeout() async {
291+
await _platform.onTimeout(
292+
matchingKey: _matchingKey, bucketingKey: _bucketingKey);
339293

340-
return result;
294+
return Future.value(this);
341295
}
342296
}

0 commit comments

Comments
 (0)