Skip to content

Commit a7c8492

Browse files
committed
deserializeAndSave
1 parent 7533b51 commit a7c8492

File tree

3 files changed

+61
-37
lines changed

3 files changed

+61
-37
lines changed

lib/src/adapter/adapter.dart

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -217,33 +217,42 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
217217
});
218218
}
219219

220-
Future<void> saveManyLocal(Iterable<DataModelMixin> models,
221-
{bool notify = true}) async {
222-
final savedKeys = await runInIsolate((adapter) async {
223-
final db = adapter.db;
224-
final savedKeys = <String>[];
225-
226-
final grouped = models.groupSetsBy((e) => e._adapter);
227-
for (final e in grouped.entries) {
228-
final adapter = e.key;
229-
final ps = db.prepare(
230-
'REPLACE INTO ${adapter.internalType} (key, data) VALUES (?, ?) RETURNING key;');
231-
for (final model in e.value) {
232-
final key = model._key!.detypifyKey();
233-
final map = adapter.serializeLocal(model, withRelationships: false);
234-
final data = jsonEncode(map);
235-
final result = ps.select([key, data]);
236-
savedKeys.add(
237-
(result.first['key'] as int).typifyWith(adapter.internalType));
238-
}
239-
ps.dispose();
220+
List<String> _saveManyLocal(
221+
Adapter adapter, Iterable<DataModelMixin> models) {
222+
final db = adapter.db;
223+
final savedKeys = <String>[];
224+
225+
final grouped = models.groupSetsBy((e) => e._adapter);
226+
for (final e in grouped.entries) {
227+
final adapter = e.key;
228+
final ps = db.prepare(
229+
'REPLACE INTO ${adapter.internalType} (key, data) VALUES (?, ?) RETURNING key;');
230+
for (final model in e.value) {
231+
final key = model._key!.detypifyKey();
232+
final map = adapter.serializeLocal(model, withRelationships: false);
233+
final data = jsonEncode(map);
234+
final result = ps.select([key, data]);
235+
savedKeys
236+
.add((result.first['key'] as int).typifyWith(adapter.internalType));
240237
}
241-
return savedKeys;
242-
});
238+
ps.dispose();
239+
}
240+
return savedKeys;
241+
}
243242

244-
if (notify) {
245-
core._notify(savedKeys, type: DataGraphEventType.updateNode);
243+
Future<List<String>?> saveManyLocal(Iterable<DataModelMixin> models,
244+
{bool notify = true, bool async = true}) async {
245+
final savedKeys = async
246+
? await runInIsolate(
247+
(adapter) => adapter._saveManyLocal(adapter, models))
248+
: _saveManyLocal(this as Adapter, models);
249+
if (async) {
250+
if (notify) {
251+
core._notify(savedKeys, type: DataGraphEventType.updateNode);
252+
return null;
253+
}
246254
}
255+
return savedKeys;
247256
}
248257

249258
/// Deletes model of type [T] from local storage.

lib/src/adapter/remote_adapter.dart

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -531,28 +531,23 @@ mixin _RemoteAdapter<T extends DataModelMixin<T>> on _SerializationAdapter<T> {
531531
return response.body as R?;
532532
}
533533

534-
final deserialized = await deserialize(body);
534+
final deserialized = await deserializeAndSave(body);
535+
deserialized._log(adapter, label);
535536

536537
if (isFindAll || (isCustom && deserialized.model == null)) {
537-
await _saveDeserialized(deserialized);
538-
deserialized._log(adapter, label);
539-
540538
late R? models;
541539
if (response.statusCode == 304) {
542-
models = await adapter.findAll(remote: false) as R?;
540+
models = adapter.findAllLocal() as R?;
543541
} else {
544542
models = deserialized.models as R?;
545543
}
546544
return models;
547545
}
548546

549547
if (isFindOne || (isCustom && deserialized.model != null)) {
550-
await _saveDeserialized(deserialized);
551-
deserialized._log(adapter, label);
552-
553548
late R? model;
554549
if (response.statusCode == 304) {
555-
model = await adapter.findOne(label.id!, remote: false) as R?;
550+
model = adapter.findOneLocalById(label.id!) as R?;
556551
} else {
557552
model = deserialized.model as R?;
558553
}

lib/src/adapter/serialization_adapter.dart

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ part of flutter_data;
33
mixin _SerializationAdapter<T extends DataModelMixin<T>> on _BaseAdapter<T> {
44
/// Returns a serialized version of a model of [T],
55
/// as a [Map<String, dynamic>] ready to be JSON-encoded.
6-
@protected
7-
@visibleForTesting
86
Future<Map<String, dynamic>> serialize(T model,
97
{bool withRelationships = true}) async {
108
final map = serializeLocal(model, withRelationships: withRelationships);
@@ -99,8 +97,6 @@ mixin _SerializationAdapter<T extends DataModelMixin<T>> on _BaseAdapter<T> {
9997

10098
/// Returns a [DeserializedData] object when deserializing a given [data].
10199
///
102-
@protected
103-
@visibleForTesting
104100
Future<DeserializedData<T>> deserialize(Object? data,
105101
{String? key, bool async = true}) async {
106102
final record = async
@@ -109,6 +105,30 @@ mixin _SerializationAdapter<T extends DataModelMixin<T>> on _BaseAdapter<T> {
109105
: _deserialize(this as Adapter, data, key: key);
110106
return DeserializedData<T>(record.$1.cast<T>(), included: record.$2);
111107
}
108+
109+
Future<DeserializedData<T>> deserializeAndSave(Object? data,
110+
{String? key, bool notify = true, bool ignoreReturn = false}) async {
111+
final record = await runInIsolate<
112+
(
113+
List<DataModelMixin>,
114+
List<DataModelMixin>,
115+
List<String>
116+
)>((adapter) async {
117+
final emptyDm = <DataModelMixin>[];
118+
final r = adapter._deserialize(adapter, data, key: key);
119+
final models = [...r.$1, ...r.$2];
120+
if (models.isEmpty) return (emptyDm, emptyDm, <String>[]);
121+
final savedKeys = await adapter.saveManyLocal(models, async: false);
122+
return ignoreReturn
123+
? (emptyDm, emptyDm, savedKeys!)
124+
: (r.$1, r.$2, savedKeys!);
125+
});
126+
final (models, included, savedKeys) = record;
127+
if (notify && savedKeys.isNotEmpty) {
128+
core._notify(savedKeys, type: DataGraphEventType.updateNode);
129+
}
130+
return DeserializedData<T>(models.cast<T>(), included: included);
131+
}
112132
}
113133

114134
/// A utility class used to return deserialized main [models] AND [included] models.

0 commit comments

Comments
 (0)