Skip to content

Commit 2b550f3

Browse files
Respect storageJson option when creating client
1 parent f66ccaf commit 2b550f3

File tree

5 files changed

+40
-40
lines changed

5 files changed

+40
-40
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/state.dart

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -162,52 +162,39 @@ abstract class PersistedState<T> implements AsyncStateNotifier<T> {
162162
void init(ErrorHandler errorHandler, bool storageJson) {
163163
this._errorHandler = errorHandler;
164164
addListener((state) {
165-
// Only persist state if storageJson is true
166-
if (storageJson) {
167-
if (_persistance != null) {
168-
_hasUpdated = true;
169-
} else {
170-
_persistance = _store
171-
.setPersisted(_key, toJson(state))
172-
.whenComplete(_whenPersistenceComplete);
173-
}
165+
if (_persistance != null) {
166+
_hasUpdated = true;
167+
} else {
168+
print("storageJson = $storageJson");
169+
_persistance = storageJson
170+
? _store
171+
.setPersisted(_key, toJson(state))
172+
.whenComplete(_whenPersistenceComplete)
173+
: null;
174174
}
175175
});
176176
_store.ready.then<void>((_) async {
177-
// If storage is disabled, delete any existing files to prevent data leakage
178-
if (!storageJson) {
179-
try {
180-
await _store.deletePersisted(_key);
181-
} catch (e) {
182-
// Ignore deletion errors, as the file might not exist
183-
}
184-
}
185-
186177
Map<String, dynamic>? rawV;
187-
// Only read from storage if storageJson is true
188-
if (storageJson) {
189-
try {
190-
rawV = await _store.getPersisted(_key);
191-
} on FormatException catch (e) {
192-
// Addressing https://github.com/segmentio/analytics_flutter/issues/74
193-
// File corruption should be less likely with removal of async code in writes
194-
// Existing corrupted files are cleaned up here without failing initialization
195-
_store.setPersisted(_key, {});
196-
log("Clean file $_key with format error", kind: LogFilterKind.warning);
197-
final wrappedError = ErrorLoadingStorage(e);
198-
errorHandler(wrappedError);
199-
}
178+
try {
179+
rawV = await _store.getPersisted(_key);
180+
} on FormatException catch (e) {
181+
// Addressing https://github.com/segmentio/analytics_flutter/issues/74
182+
// File corruption should be less likely with removal of async code in writes
183+
// Existing corrupted files are cleaned up here without failing initialization
184+
_store.setPersisted(_key, {});
185+
log("Clean file $_key with format error", kind: LogFilterKind.warning);
186+
final wrappedError = ErrorLoadingStorage(e);
187+
errorHandler(wrappedError);
200188
}
201189
T v;
202190

203191
if (rawV == null) {
204192
final init = await _initialiser();
205-
// Only persist if storageJson is true
206-
if (storageJson) {
207-
_persistance = _store
208-
.setPersisted(_key, toJson(init))
209-
.whenComplete(_whenPersistenceComplete);
210-
}
193+
_persistance = storageJson
194+
? _store
195+
.setPersisted(_key, toJson(init))
196+
.whenComplete(_whenPersistenceComplete)
197+
: null;
211198
_notifier.nonNullState = init;
212199
v = init;
213200
} else {

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

Lines changed: 5 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,11 +22,13 @@ 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
}
2428

2529
@override
2630
Future deletePersisted(String key) async {
31+
if (!storageJson) return;
2732
final file = File(await _fileName(key));
2833
if (await file.exists()) {
2934
await file.delete();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ mixin Store {
1212
void dispose();
1313
}
1414

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

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

Lines changed: 5 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,12 +20,14 @@ 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
}
2327

2428
@override
2529
Future deletePersisted(String key) {
30+
if (!storageJson) return Future.value();
2631
localStorage.removeItem(_getFileName(key));
2732
return Future.value();
2833
}

0 commit comments

Comments
 (0)