Merge pull request #76 from dart-lang/fix_settings_file_mutation
fix an issue with changing the settings file
diff --git a/changelog.md b/changelog.md
index ff1087a..2f01cb2 100644
--- a/changelog.md
+++ b/changelog.md
@@ -1,5 +1,8 @@
# Changelog
+## 2.2.0+1
+- bug fix to prevent frequently changing the settings file
+
## 2.2.0
- added `Analytics.firstRun`
- added `Analytics.enabled`
diff --git a/lib/src/usage_impl.dart b/lib/src/usage_impl.dart
index 5eb80fb..290c5fb 100644
--- a/lib/src/usage_impl.dart
+++ b/lib/src/usage_impl.dart
@@ -101,7 +101,10 @@
bool get firstRun {
if (_firstRun == null) {
_firstRun = properties['firstRun'] == null;
- properties['firstRun'] = false;
+
+ if (properties['firstRun'] != false) {
+ properties['firstRun'] = false;
+ }
}
return _firstRun;
diff --git a/lib/src/usage_impl_io.dart b/lib/src/usage_impl_io.dart
index 5b10994..2bb7efa 100644
--- a/lib/src/usage_impl_io.dart
+++ b/lib/src/usage_impl_io.dart
@@ -93,21 +93,31 @@
IOPersistentProperties(String name) : super(name) {
String fileName = '.${name.replaceAll(' ', '_')}';
_file = new File(path.join(_userHomeDir(), fileName));
- _file.createSync();
- String contents = _file.readAsStringSync();
- if (contents.isEmpty) contents = '{}';
- _map = JSON.decode(contents);
+
+ try {
+ if (!_file.existsSync()) _file.createSync();
+ String contents = _file.readAsStringSync();
+ if (contents.isEmpty) contents = '{}';
+ _map = JSON.decode(contents);
+ } catch (_) {
+ _map = {};
+ }
}
dynamic operator[](String key) => _map[key];
void operator[]=(String key, dynamic value) {
+ if (value == null && !_map.containsKey(key)) return;
+ if (_map[key] == value) return;
+
if (value == null) {
_map.remove(key);
} else {
_map[key] = value;
}
- _file.writeAsStringSync(JSON.encode(_map) + '\n');
+ try {
+ _file.writeAsStringSync(JSON.encode(_map) + '\n');
+ } catch (_) { }
}
}
diff --git a/lib/usage.dart b/lib/usage.dart
index bf14930..53f1812 100644
--- a/lib/usage.dart
+++ b/lib/usage.dart
@@ -252,16 +252,13 @@
Future sendSocial(String network, String action, String target) =>
_log('social', {'network': network, 'action': action, 'target': target});
- Future sendTiming(String variableName, int time, {String category,
- String label}) {
+ Future sendTiming(String variableName, int time, {String category, String label}) {
return _log('timing', {'variableName': variableName, 'time': time,
'category': category, 'label': label});
}
- AnalyticsTimer startTimer(String variableName,
- {String category, String label}) {
- return new AnalyticsTimer(this,
- variableName, category: category, label: label);
+ AnalyticsTimer startTimer(String variableName, {String category, String label}) {
+ return new AnalyticsTimer(this, variableName, category: category, label: label);
}
Future sendException(String description, {bool fatal}) =>
diff --git a/pubspec.yaml b/pubspec.yaml
index 86fcd50..e3a323b 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -3,7 +3,7 @@
# BSD-style license that can be found in the LICENSE file.
name: usage
-version: 2.2.0
+version: 2.2.0+1
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>