readNormalized method

Map<String, dynamic>? readNormalized(
  1. String rootId, {
  2. bool? optimistic = true,
})

Reads dereferences an entity from the first valid optimistic layer, defaulting to the base internal HashMap.

Implementation

Map<String, dynamic>? readNormalized(
  String rootId, {
  bool? optimistic = true,
}) {
  var value = store.get(rootId);

  if (!optimistic!) {
    return value;
  }

  for (final patch in optimisticPatches) {
    if (patch.data.containsKey(rootId)) {
      final patchData = patch.data[rootId];
      if (value is Map<String, Object> && patchData is Map<String, Object>) {
        value = deeplyMergeLeft([
          value,
          patchData,
        ]);
      } else {
        // Overwrite if not mergable
        value = patchData;
      }
    }
  }

  return value;
}