import { list , object , QueryChange , listVal , objectVal } from 'rxfire/database' ; import { ReactFireOptions , useObservable , checkIdField , ObservableStatus , ReactFireGlobals } from './' ; import type { Query as DatabaseQuery , DatabaseReference } from 'firebase/database' ; const cachedQueries = ( globalThis as any as ReactFireGlobals ) . _reactFireDatabaseCachedQueries || [ ] ; if ( ! ( globalThis as any as ReactFireGlobals ) . _reactFireDatabaseCachedQueries ) { ( globalThis as any as ReactFireGlobals ) . _reactFireDatabaseCachedQueries = cachedQueries ; } function getUniqueIdForDatabaseQuery ( query ) { const index = cachedQueries . findIndex ( ( cachedQuery ) => cachedQuery . isEqual ( query ) ) ; if ( index > - 1 ) { return index ; } return cachedQueries . push ( query ) - 1 ; } param ref param options export function useDatabaseObject < T = unknown > ( ref , options ? ) { const observableId = `database:object:${ ref . toString ( ) }` ; const observable$ = object ( ref ) ; return useObservable ( observableId , observable$ , options ) ; } export function useDatabaseObjectData < T > ( ref , options ? ) { const idField = options ? checkIdField ( options ) : 'NO_ID_FIELD' ; const observableId = `database:objectVal:${ ref . toString ( ) }:idField=${ idField }` ; const observable$ = objectVal < T > ( ref , { keyField : idField } ) ; return useObservable ( observableId , observable$ , options ) ; } param ref param options export function useDatabaseList < T = { [ key ] : unknown ; } > ( ref , options ? ) { const hash = `database:list:${ getUniqueIdForDatabaseQuery ( ref ) }` ; const observable$ = list ( ref ) ; return useObservable ( hash , observable$ , options ) ; } export function useDatabaseListData < T = { [ key ] : unknown ; } > ( ref , options ? ) { const idField = options ? checkIdField ( options ) : 'NO_ID_FIELD' ; const observableId = `database:listVal:${ getUniqueIdForDatabaseQuery ( ref ) }:idField=${ idField }` ; const observable$ = listVal < T > ( ref , { keyField : idField } ) ; return useObservable ( observableId , observable$ , options ) ; }