updateDoc method

  1. @protected
Future<TurboResponse<T>> updateDoc({
  1. Transaction? transaction,
  2. required String id,
  3. required UpdateDocDef<T> doc,
  4. TurboWriteable remoteUpdateRequestBuilder(
    1. T doc
    )?,
  5. bool doNotifyListeners = true,
})

Updates a document both locally and in Firestore.

Performs an optimistic update by updating the local state first, then syncing with Firestore. If the remote update fails, the local state remains updated.

Parameters:

  • id - The document ID
  • doc - The function to update the document
  • remoteUpdateRequestBuilder - Optional function to build the remote update request
  • doNotifyListeners - Whether to notify listeners of the change
  • transaction - Optional transaction for atomic operations

Returns a TurboResponse with the updated document reference

Implementation

@protected
Future<TurboResponse<T>> updateDoc({
  Transaction? transaction,
  required String id,
  required UpdateDocDef<T> doc,
  TurboWriteable Function(T doc)? remoteUpdateRequestBuilder,
  bool doNotifyListeners = true,
}) async {
  try {
    log.debug('Updating doc with id: $id');
    final pDoc = updateLocalDoc(
      id: id,
      doc: doc,
      doNotifyListeners: doNotifyListeners,
    );
    final future = api.updateDoc(
      writeable: remoteUpdateRequestBuilder?.call(pDoc) ?? pDoc,
      id: id,
      transaction: transaction,
    );
    final turboResponse = await future;
    if (transaction != null) {
      turboResponse.throwWhenFail();
    }
    return turboResponse.mapSuccess((_) => pDoc);
  } catch (error, stackTrace) {
    if (transaction != null) rethrow;
    log.error(
      '$error caught while updating doc',
      error: error,
      stackTrace: stackTrace,
    );
    return TurboResponse.fail(error: error);
  }
}