updateMerge<T> function

T? updateMerge<T>(
  1. T? existingData,
  2. T? newData
)

Attempt to replicate how the Cloud Firestore server updates document data in order to support client-side cache changes. TODO: The server merge logic is more advanced than this, figure out how replicate it fully on the client for cache-first updates.

Implementation

T? updateMerge<T>(
  T? existingData,
  T? newData,
) {
  if (newData == null) {
    return null;
  }

  if (existingData == null) {
    return newData;
  }

  if (existingData is Map<String, dynamic> && newData is Map<String, dynamic>) {
    return {
      ...existingData,
      ...newData,
    } as T;
  } else {
    return newData;
  }
}