save method
Save state to storage.
Implementation
Future<void> save(T state) async {
try {
_printDebug('Starting save...');
_printDebug('Running save transformations');
// Run all save transforms
try {
transforms?.onSave?.forEach((transform) {
state = transform(state);
});
} catch (error) {
throw TransformationException(
"On save transformation: ${error.toString()}",
);
}
_printDebug('Serializing');
var data = serializer.encode(state);
_printDebug('Running save raw transformations');
try {
// Run all raw save transforms
rawTransforms?.onSave?.forEach((transform) {
data = transform(data);
});
} catch (error) {
throw TransformationException(
'On save raw transformation: ${error.toString()}');
}
_printDebug('Saving to storage');
// Save to storage
try {
// Use lock to prevent writing twice at the same time
await _saveLock.synchronized(() async => await storage.save(data));
} catch (error) {
throw StorageException('On save: ${error.toString()}');
}
_printDebug('Done saving!');
} catch (error) {
_printDebug('Error while saving: ${error.toString()}');
rethrow;
}
}