Skip to content

Commit

Permalink
Merge pull request #14 from grahamsmith/ensure_uniqueness
Browse files Browse the repository at this point in the history
Fixes bug where new instances of the same vendor could be init'd
  • Loading branch information
grahamsmith authored Aug 25, 2021
2 parents db3d50a + 8190f2b commit 90db638
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/src/analytics_x.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ class AnalyticsX {
Future<void> init(List<AnalyticsVendor> vendors, [AnalyticsError? onError]) async {
this.onError = onError;

final newVendors = List<AnalyticsVendor>.from(vendors)..removeWhere((v) => _vendors.contains(v));
final uniqueVendors = _getUniqueVendorsById(vendors);

final newVendors = List<AnalyticsVendor>.from(uniqueVendors)
..removeWhere((v) => _getVendorsById([v.id]).isNotEmpty);

if (newVendors.isEmpty) {
return;
Expand Down Expand Up @@ -83,9 +86,23 @@ class AnalyticsX {
return _vendors.where((element) => ids.contains(element.id)).toList();
}

List<AnalyticsVendor> _getUniqueVendorsById(List<AnalyticsVendor> vendorList) {
final List<String> uniqueIds = [];
for (final AnalyticsVendor vendor in List.from(vendorList)) {
if (uniqueIds.contains(vendor.id)) {
vendorList.remove(vendor);
}
uniqueIds.add(vendor.id);
}
return vendorList;
}

/// Resets the list of known vendors.
///
/// Doesn't perform any additional work on the [AnalyticsVendor], but permits all vendors to be passed to [init]
/// again and have their [AnalyticsVendor.init] method invoked.
void reset() => _vendors.clear();

/// Fetches the list of all registered (init'd) vendors
List<AnalyticsVendor> get allRegisteredVendors => _vendors;
}
19 changes: 19 additions & 0 deletions test/analytics_x_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,23 @@ void main() {
await ax.invokeAction(FakeAction("potato"), ['NonExistentVendorId']);
expect(fakeVendor.handleActionWasCalledXTimes, 0);
});

test('AnalyticsX only registers one of each class of AnalyticsVendor when initialised together', () async {
final anotherFakeVendor = FakeVendor();
await ax.init([fakeVendor, anotherFakeVendor]);
expect(AnalyticsX().allRegisteredVendors.length, 1);
});

test('AnalyticsX only registers one of each class of AnalyticsVendor when initialised separately', () async {
final anotherFakeVendor = FakeVendor();
await ax.init([fakeVendor]);
await ax.init([anotherFakeVendor]);
expect(AnalyticsX().allRegisteredVendors.length, 1);
});

test('AnalyticsX only registers one AnalyticsVendor when two implementations are given the same ID', () async {
final anotherFakeVendor = FakeVendorWithSameID();
await ax.init([fakeVendor, anotherFakeVendor]);
expect(AnalyticsX().allRegisteredVendors.length, 1);
});
}
2 changes: 2 additions & 0 deletions test/util/vendors_and_actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class FakeVendor2 extends FakeVendor {
FakeVendor2() : super.withVendorId('Dummy2');
}

class FakeVendorWithSameID extends FakeVendor {}

class BrokenFakeVendor extends FakeVendor {
bool initWillThrow = true;
bool handleActionWillThrow = false;
Expand Down

0 comments on commit 90db638

Please sign in to comment.