Skip to content

Commit 2dd7bcf

Browse files
Patching Github Issue #171 (#176)
* Patching Github Issue #171 * Respect storageJson option when creating client
1 parent 418e2f6 commit 2dd7bcf

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
lines changed

packages/core/lib/client.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ Analytics createClient(Configuration configuration) {
1919
configuration = setFlushPolicies(configuration, defaultFlushPolicies);
2020
}
2121

22-
final analytics = Analytics(configuration, storeFactory());
22+
final analytics = Analytics(
23+
configuration,
24+
storeFactory(storageJson: configuration.storageJson ?? true),
25+
);
2326

2427
if (configuration.debug) {
2528
analytics.addPlugin(EventLogger());

packages/core/lib/utils/store/io.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import 'package:segment_analytics/utils/store/store.dart';
99
import 'package:path_provider/path_provider.dart';
1010

1111
class StoreImpl with Store {
12+
final bool storageJson;
13+
StoreImpl({this.storageJson = true});
1214
@override
1315
Future<Map<String, dynamic>?> getPersisted(String key) {
16+
if (!storageJson) return Future.value(null);
1417
return _readFile(key);
1518
}
1619

@@ -19,8 +22,18 @@ class StoreImpl with Store {
1922

2023
@override
2124
Future setPersisted(String key, Map<String, dynamic> value) {
25+
if (!storageJson) return Future.value();
2226
return _writeFile(key, value);
2327
}
28+
29+
@override
30+
Future deletePersisted(String key) async {
31+
if (!storageJson) return;
32+
final file = File(await _fileName(key));
33+
if (await file.exists()) {
34+
await file.delete();
35+
}
36+
}
2437

2538
Future _writeFile(String fileKey, Map<String, dynamic> data) async {
2639
RandomAccessFile file =

packages/core/lib/utils/store/store.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ mixin Store {
44
Future<Map<String, dynamic>?> getPersisted(String key);
55

66
Future setPersisted(String key, Map<String, dynamic> value);
7+
8+
Future deletePersisted(String key);
79

810
Future get ready;
911

1012
void dispose();
1113
}
1214

13-
StoreImpl storeFactory() {
14-
return StoreImpl();
15+
StoreImpl storeFactory({bool storageJson = true}) {
16+
return StoreImpl(storageJson: storageJson);
1517
}

packages/core/lib/utils/store/unsupported.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import 'package:segment_analytics/errors.dart';
44
import 'store.dart';
55

66
class StoreImpl with Store {
7+
final bool storageJson;
8+
StoreImpl({this.storageJson = true});
79
@override
810
Future<Map<String, dynamic>?> getPersisted(String key) {
911
throw PlatformNotSupportedError();
@@ -16,9 +18,14 @@ class StoreImpl with Store {
1618
Future setPersisted(String key, Map<String, dynamic> value) {
1719
throw PlatformNotSupportedError();
1820
}
21+
22+
@override
23+
Future deletePersisted(String key) {
24+
throw PlatformNotSupportedError();
25+
}
1926

2027
@override
2128
void dispose() {
2229
throw PlatformNotSupportedError();
2330
}
24-
}
31+
}

packages/core/lib/utils/store/web.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import 'package:segment_analytics/utils/store/store.dart';
55
import 'package:web/web.dart' as web;
66

77
class StoreImpl implements Store {
8+
final bool storageJson;
9+
StoreImpl({this.storageJson = true});
810
web.Storage get localStorage => web.window.localStorage;
911

1012
@override
1113
Future<Map<String, dynamic>?> getPersisted(String key) {
14+
if (!storageJson) return Future.value(null);
1215
return _readFromStorage(key);
1316
}
1417

@@ -17,9 +20,17 @@ class StoreImpl implements Store {
1720

1821
@override
1922
Future setPersisted(String key, Map<String, dynamic> value) {
23+
if (!storageJson) return Future.value();
2024
_writeToStorage(key, value);
2125
return Future.value();
2226
}
27+
28+
@override
29+
Future deletePersisted(String key) {
30+
if (!storageJson) return Future.value();
31+
localStorage.removeItem(_getFileName(key));
32+
return Future.value();
33+
}
2334

2435
String _getFileName(String fileKey) {
2536
return "analytics-flutter-$fileKey.json";

packages/core/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: segment_analytics
22
description: The hassle-free way to add Segment analytics to your Flutter app.
3-
version: 1.1.8
3+
version: 1.1.9
44
homepage: https://github.com/segmentio/analytics_flutter#readme
55
repository: https://github.com/segmentio/analytics_flutter/tree/main/packages/core#readme
66
issue_tracker: https://github.com/segmentio/analytics_flutter/issues

0 commit comments

Comments
 (0)