ByteBrief / src /firebase /non-blocking-updates.tsx
Gigishot's picture
Upload 68 files
78aa7cb verified
Raw
History Blame Contribute Delete
2.28 kB
'use client';
import {
setDoc,
addDoc,
updateDoc,
deleteDoc,
CollectionReference,
DocumentReference,
SetOptions,
} from 'firebase/firestore';
import { errorEmitter } from '@/firebase/error-emitter';
import {FirestorePermissionError} from '@/firebase/errors';
/**
* Initiates a setDoc operation for a document reference.
* Does NOT await the write operation internally.
*/
export function setDocumentNonBlocking(docRef: DocumentReference, data: any, options: SetOptions) {
setDoc(docRef, data, options).catch(error => {
errorEmitter.emit(
'permission-error',
new FirestorePermissionError({
path: docRef.path,
operation: 'write', // or 'create'/'update' based on options
requestResourceData: data,
})
)
})
// Execution continues immediately
}
/**
* Initiates an addDoc operation for a collection reference.
* Does NOT await the write operation internally.
* Returns the Promise for the new doc ref, but typically not awaited by caller.
*/
export function addDocumentNonBlocking(colRef: CollectionReference, data: any) {
const promise = addDoc(colRef, data)
.catch(error => {
errorEmitter.emit(
'permission-error',
new FirestorePermissionError({
path: colRef.path,
operation: 'create',
requestResourceData: data,
})
)
});
return promise;
}
/**
* Initiates an updateDoc operation for a document reference.
* Does NOT await the write operation internally.
*/
export function updateDocumentNonBlocking(docRef: DocumentReference, data: any) {
updateDoc(docRef, data)
.catch(error => {
errorEmitter.emit(
'permission-error',
new FirestorePermissionError({
path: docRef.path,
operation: 'update',
requestResourceData: data,
})
)
});
}
/**
* Initiates a deleteDoc operation for a document reference.
* Does NOT await the write operation internally.
*/
export function deleteDocumentNonBlocking(docRef: DocumentReference) {
deleteDoc(docRef)
.catch(error => {
errorEmitter.emit(
'permission-error',
new FirestorePermissionError({
path: docRef.path,
operation: 'delete',
})
)
});
}