@@ -25,13 +25,13 @@ function doCallback() {
2525 } ) ;
2626}
2727
28- export function useSubscribe < Tx , D , R extends D > (
29- r : Subscribable < Tx , D > | null | undefined ,
30- query : ( tx : Tx ) => Promise < R > ,
31- def : R ,
28+ export function useSubscribe < Tx , Data , QueryRet extends Data , Default > (
29+ r : Subscribable < Tx , Data > | null | undefined ,
30+ query : ( tx : Tx ) => Promise < QueryRet > ,
31+ def : Default ,
3232 deps : Array < unknown > = [ ] ,
33- ) : R {
34- const [ snapshot , setSnapshot ] = useState < R > ( def ) ;
33+ ) {
34+ const [ snapshot , setSnapshot ] = useState < QueryRet | undefined > ( undefined ) ;
3535 useEffect ( ( ) => {
3636 if ( ! r ) {
3737 return ;
@@ -41,7 +41,7 @@ export function useSubscribe<Tx, D, R extends D>(
4141 onData : data => {
4242 // This is safe because we know that subscribe in fact can only return
4343 // `R` (the return type of query or def).
44- callbacks . push ( ( ) => setSnapshot ( data as R ) ) ;
44+ callbacks . push ( ( ) => setSnapshot ( data as QueryRet ) ) ;
4545 if ( ! hasPendingCallback ) {
4646 void Promise . resolve ( ) . then ( doCallback ) ;
4747 hasPendingCallback = true ;
@@ -51,8 +51,11 @@ export function useSubscribe<Tx, D, R extends D>(
5151
5252 return ( ) => {
5353 unsubscribe ( ) ;
54- setSnapshot ( def ) ;
54+ setSnapshot ( undefined ) ;
5555 } ;
56- } , [ r , ...deps ] ) ;
56+ } , [ r , def , ...deps ] ) ;
57+ if ( snapshot === undefined ) {
58+ return def ;
59+ }
5760 return snapshot ;
5861}
0 commit comments