@@ -24,6 +24,8 @@ export interface UseTableCallbacks<RowType> {
2424 onInsert ?: ( row : RowType ) => void ;
2525 onDelete ?: ( row : RowType ) => void ;
2626 onUpdate ?: ( oldRow : RowType , newRow : RowType ) => void ;
27+ /** Whether the subscription is active. Defaults to `true`. */
28+ enabled ?: boolean ;
2729}
2830
2931type MembershipChange = 'enter' | 'leave' | 'stayIn' | 'stayOut' ;
@@ -67,6 +69,7 @@ export function useTable<TableDef extends UntypedTableDef>(
6769 callbacks ?: UseTableCallbacks < Prettify < RowType < TableDef > > >
6870) : [ readonly Prettify < RowType < TableDef > > [ ] , boolean ] {
6971 type UseTableRowType = RowType < TableDef > ;
72+ const enabled = callbacks ?. enabled ?? true ;
7073 const accessorName = getQueryAccessorName ( query ) ;
7174 const whereExpr = getQueryWhereClause ( query ) ;
7275
@@ -93,6 +96,9 @@ export function useTable<TableDef extends UntypedTableDef>(
9396 readonly Prettify < UseTableRowType > [ ] ,
9497 boolean ,
9598 ] => {
99+ if ( ! enabled ) {
100+ return [ [ ] , true ] ;
101+ }
96102 const connection = connectionState . getConnection ( ) ;
97103 if ( ! connection ) {
98104 return [ [ ] , false ] ;
@@ -107,7 +113,7 @@ export function useTable<TableDef extends UntypedTableDef>(
107113 // TODO: investigating refactoring so that this is no longer necessary, as we have had genuine bugs with missed deps.
108114 // See https://github.com/clockworklabs/SpacetimeDB/pull/4580.
109115 // eslint-disable-next-line react-hooks/exhaustive-deps
110- } , [ connectionState , accessorName , querySql , subscribeApplied ] ) ;
116+ } , [ connectionState , accessorName , querySql , subscribeApplied , enabled ] ) ;
111117
112118 // Invalidate the cached snapshot when computeSnapshot changes (e.g. when
113119 // subscribeApplied flips to true) so getSnapshot() recomputes on the next
@@ -117,6 +123,10 @@ export function useTable<TableDef extends UntypedTableDef>(
117123 } , [ computeSnapshot ] ) ;
118124
119125 useEffect ( ( ) => {
126+ if ( ! enabled ) {
127+ setSubscribeApplied ( false ) ;
128+ return ;
129+ }
120130 const connection = connectionState . getConnection ( ) ! ;
121131 if ( connectionState . isActive && connection ) {
122132 const cancel = connection
@@ -129,10 +139,14 @@ export function useTable<TableDef extends UntypedTableDef>(
129139 cancel . unsubscribe ( ) ;
130140 } ;
131141 }
132- } , [ querySql , connectionState . isActive , connectionState ] ) ;
142+ } , [ querySql , connectionState . isActive , connectionState , enabled ] ) ;
133143
134144 const subscribe = useCallback (
135145 ( onStoreChange : ( ) => void ) => {
146+ if ( ! enabled ) {
147+ return ( ) => { } ;
148+ }
149+
136150 const onInsert = (
137151 ctx : EventContextInterface < UntypedRemoteModule > ,
138152 row : any
@@ -218,6 +232,7 @@ export function useTable<TableDef extends UntypedTableDef>(
218232 callbacks ?. onDelete ,
219233 callbacks ?. onInsert ,
220234 callbacks ?. onUpdate ,
235+ enabled ,
221236 ]
222237 ) ;
223238
0 commit comments