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 #114 from dart-lang/add_close_method
Browse files Browse the repository at this point in the history
add a close method; prep for publishing 3.3.0
  • Loading branch information
devoncarew committed Jul 1, 2017
2 parents 5326e44 + 71152b4 commit 5d5a215
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 19 deletions.
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ analyzer:
linter:
rules:
- annotate_overrides
- directives_ordering
- empty_constructor_bodies
- empty_statements
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## unreleased
## 3.3.0
- added a `close()` method to the `Analytics` class
- change our minimum SDK from `1.24.0-dev` to `1.24.0` stable

## 3.2.0
Expand Down
4 changes: 4 additions & 0 deletions example/ga.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ main(List args) async {
await ga.sendTiming('writeDuration', 123);
await ga.sendEvent('create', 'consoleapp', label: 'Console App');
print('pinged ${ua}');

await ga.waitForLastPing();

ga.close();
}
6 changes: 6 additions & 0 deletions lib/src/usage_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ class AnalyticsImpl implements Analytics {
return f;
}

@override
void close() => postHandler.close();

@override
String get clientId => properties['clientId'] ??= new Uuid().generateV4();

Expand Down Expand Up @@ -291,4 +294,7 @@ abstract class PersistentProperties {
*/
abstract class PostHandler {
Future sendPost(String url, Map<String, dynamic> parameters);

/// Free any used resources.
void close();
}
15 changes: 11 additions & 4 deletions lib/src/usage_impl_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ class AnalyticsHtml extends AnalyticsImpl {
}
}

typedef Future<HttpRequest> HttpRequestor(String url,
{String method, sendData});

class HtmlPostHandler extends PostHandler {
final Function mockRequestor;
final HttpRequestor mockRequestor;

HtmlPostHandler({Function this.mockRequestor});
HtmlPostHandler({this.mockRequestor});

@override
Future sendPost(String url, Map<String, dynamic> parameters) {
Expand All @@ -43,12 +46,16 @@ class HtmlPostHandler extends PostHandler {
parameters['vp'] = '${viewportWidth}x$viewportHeight';

String data = postEncode(parameters);
var request = mockRequestor == null ? HttpRequest.request : mockRequestor;
return request(url, method: 'POST', sendData: data).catchError((e) {
HttpRequestor requestor =
mockRequestor == null ? HttpRequest.request : mockRequestor;
return requestor(url, method: 'POST', sendData: data).catchError((e) {
// Catch errors that can happen during a request, but that we can't do
// anything about, e.g. a missing internet connection.
});
}

@override
void close() {}
}

class HtmlPersistentProperties extends PersistentProperties {
Expand Down
16 changes: 12 additions & 4 deletions lib/src/usage_impl_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,21 @@ class IOPostHandler extends PostHandler {
final String _userAgent;
final HttpClient mockClient;

IOPostHandler({HttpClient this.mockClient}) : _userAgent = _createUserAgent();
HttpClient _client;

IOPostHandler({this.mockClient}) : _userAgent = _createUserAgent();

@override
Future sendPost(String url, Map<String, dynamic> parameters) async {
String data = postEncode(parameters);

HttpClient client = mockClient != null ? mockClient : new HttpClient();
client.userAgent = _userAgent;
if (_client == null) {
_client = mockClient != null ? mockClient : new HttpClient();
_client.userAgent = _userAgent;
}

try {
HttpClientRequest req = await client.postUrl(Uri.parse(url));
HttpClientRequest req = await _client.postUrl(Uri.parse(url));
req.write(data);
HttpClientResponse response = await req.close();
response.drain();
Expand All @@ -95,6 +100,9 @@ class IOPostHandler extends PostHandler {
// anything about, e.g. a missing internet connection.
}
}

@override
void close() => _client?.close();
}

JsonEncoder _jsonEncoder = new JsonEncoder.withIndent(' ');
Expand Down
11 changes: 10 additions & 1 deletion lib/usage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,15 @@ abstract class Analytics {
* users won't want their CLI app to pause at the end of the process waiting
* for Google analytics requests to complete. This method allows CLI apps to
* delay for a short time waiting for GA requests to complete, and then do
* something like call `exit()` explicitly themselves.
* something like call `dart:io`'s `exit()` explicitly themselves (or the
* [close] method below).
*/
Future waitForLastPing({Duration timeout});

/// Free any used resources.
///
/// The [Analytics] instance should not be used after this call.
void close();
}

enum AnalyticsOpt {
Expand Down Expand Up @@ -313,6 +319,9 @@ class AnalyticsMock implements Analytics {
@override
Future waitForLastPing({Duration timeout}) => new Future.value();

@override
void close() {}

Future _log(String hitType, Map m) {
if (logCalls) {
print('analytics: ${hitType} ${m}');
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# BSD-style license that can be found in the LICENSE file.

name: usage
version: 3.2.0+1
version: 3.3.0
description: A Google Analytics wrapper for both command-line, web, and Flutter apps.
homepage: https://github.com/dart-lang/usage
author: Dart Team <misc@dartlang.org>
Expand Down
12 changes: 9 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ have completed, or until the specified duration has elapsed. So, CLI apps can do
something like:

```dart
analytics.waitForLastPing(timeout: new Duration(milliseconds: 500)).then((_) {
exit(0);
});
await analytics.waitForLastPing(timeout: new Duration(milliseconds: 200));
analytics.close();
```

or:

```dart
await analytics.waitForLastPing(timeout: new Duration(milliseconds: 200));
exit(0);
```

## Using the API
Expand Down
8 changes: 4 additions & 4 deletions test/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
library usage.all_test;

import 'hit_types_test.dart' as hit_types_test;
import 'usage_test.dart' as usage_test;
import 'usage_impl_test.dart' as usage_impl_test;
import 'usage_impl_io_test.dart' as usage_impl_io_test;
import 'usage_impl_test.dart' as usage_impl_test;
import 'usage_test.dart' as usage_test;
import 'uuid_test.dart' as uuid_test;

void main() {
hit_types_test.defineTests();
usage_test.defineTests();
usage_impl_test.defineTests();
usage_impl_io_test.defineTests();
usage_impl_test.defineTests();
usage_test.defineTests();
uuid_test.defineTests();
}
3 changes: 3 additions & 0 deletions test/src/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,7 @@ class MockPostHandler extends PostHandler {
}

Map<String, dynamic> get last => sentValues.last;

@override
void close() {}
}
3 changes: 2 additions & 1 deletion test/web_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
library usage.web_test;

import 'dart:async';
import 'dart:html';

import 'package:test/test.dart';
import 'package:usage/src/usage_impl_html.dart';
Expand Down Expand Up @@ -61,7 +62,7 @@ void defineWebTests() {
class MockRequestor {
int sendCount = 0;

Future request(String url, {String method, String sendData}) {
Future<HttpRequest> request(String url, {String method, sendData}) {
expect(url, isNotEmpty);
expect(method, isNotEmpty);
expect(sendData, isNotEmpty);
Expand Down

0 comments on commit 5d5a215

Please sign in to comment.