hydrate method
Hydrates the given paths and returns a map of document paths to their serialized data.
Implementation
Future<Map<String, dynamic>> hydrate(List<String>? paths) {
return _syncLock.run(
() async {
final Map<String, List<DualDataStore>> pathDataStores = {};
final Set<DualDataStore> dataStores = {};
// If the hydration operation is only for certain paths, then resolve all of the file data stores
// reachable under the given paths and hydrate only those data stores.
if (paths != null) {
for (final path in paths) {
final stores = pathDataStores[path] = _resolveDataStores(path);
dataStores.addAll(stores);
}
} else {
// If no specific collections have been specified, then hydrate all file data stores.
dataStores.addAll(index.values);
}
await Future.wait(dataStores.map((store) => store.hydrate()));
final Map<String, dynamic> data = {};
if (paths != null) {
for (final path in paths) {
// Only extract the data in the hydrated store that falls under the requested store path. This ensures that
// only the data that was requested is hydrated and limits the data copied over to the main isolate to only what is necessary.
//
// If later on the client requests to hydrate a path for a data store that has already been
// hydrated but has not yet delivered the data under that path to the client, then this still
// works as expected, as the hydration operation will just extract that data from the already hydrated
// data store and return it.
for (final dataStore in pathDataStores[path]!) {
data.addAll(dataStore.extract(path));
}
}
} else {
for (final dataStore in index.values) {
data.addAll(dataStore.extract());
}
}
return data;
},
);
}