Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #106 from yjbanov/init-clientId
Browse files Browse the repository at this point in the history
make clientId available immediately
  • Loading branch information
yjbanov committed Jun 15, 2017
2 parents 17bd65e + 021664e commit 8ba4c02
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
10 changes: 1 addition & 9 deletions lib/src/usage_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,7 @@ class AnalyticsImpl implements Analytics {
}

@override
String get clientId => properties['clientId'];

void _initClientId() {
if (clientId == null) {
properties['clientId'] = new Uuid().generateV4();
}
}
String get clientId => properties['clientId'] ??= new Uuid().generateV4();

/**
* Send raw data to analytics. Callers should generally use one of the typed
Expand All @@ -227,8 +221,6 @@ class AnalyticsImpl implements Analytics {
if (!enabled) return new Future.value();

if (_bucket.removeDrop()) {
_initClientId();

_variableMap.forEach((key, value) {
args[key] = value;
});
Expand Down
22 changes: 22 additions & 0 deletions test/usage_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ void defineTests() {
mock.sendScreenView('baz');
return mock.waitForLastPing(timeout: new Duration(milliseconds: 100));
});

group('clientId', () {
test('is available immediately', () {
AnalyticsImplMock mock = createMock();
expect(mock.clientId, isNotEmpty);
});

test('is memoized', () {
AnalyticsImplMock mock = createMock();
final value1 = mock.clientId;
final value2 = mock.clientId;
expect(value1, isNotEmpty);
expect(value1, value2);
});

test('is stored in properties', () {
AnalyticsImplMock mock = createMock();
expect(mock.properties['clientId'], isNull);
final value = mock.clientId;
expect(mock.properties['clientId'], value);
});
});
});

group('postEncode', () {
Expand Down

0 comments on commit 8ba4c02

Please sign in to comment.