@@ -41,6 +41,7 @@ export abstract class AbstractQueryProcessor<Data = unknown[]>
4141 protected abortController : AbortController ;
4242 protected initialized : Promise < void > ;
4343 protected _closed : boolean ;
44+ protected disposeListeners : ( ( ) => void ) | null ;
4445
4546 get closed ( ) {
4647 return this . _closed ;
@@ -66,6 +67,7 @@ export abstract class AbstractQueryProcessor<Data = unknown[]>
6667 lastUpdated : null ,
6768 data : options . watchOptions . placeholderData
6869 } ;
70+ this . disposeListeners = null ;
6971 this . initialized = this . init ( ) ;
7072 }
7173
@@ -97,6 +99,9 @@ export abstract class AbstractQueryProcessor<Data = unknown[]>
9799 protected async updateState ( update : Partial < WatchedQueryState < Data > > ) {
98100 if ( typeof update . error !== 'undefined' ) {
99101 await this . iterateAsyncListenersWithError ( async ( l ) => l . onError ?.( update . error ! ) ) ;
102+ // An error always stops for the current fetching state
103+ update . isFetching = false ;
104+ update . isLoading = false ;
100105 }
101106
102107 if ( typeof update . data !== 'undefined' ) {
@@ -113,19 +118,29 @@ export abstract class AbstractQueryProcessor<Data = unknown[]>
113118 protected async init ( ) {
114119 const { db } = this . options ;
115120
116- db . registerListener ( {
121+ const disposeCloseListener = db . registerListener ( {
122+ closed : async ( ) => {
123+ this . close ( ) ;
124+ }
125+ } ) ;
126+
127+ // Wait for the schema to be set before listening to changes
128+ await db . waitForReady ( ) ;
129+ const disposeSchemaListener = db . registerListener ( {
117130 schemaChanged : async ( ) => {
118131 await this . runWithReporting ( async ( ) => {
119132 await this . updateSettings ( this . options . watchOptions ) ;
120133 } ) ;
121- } ,
122- closing : ( ) => {
123- this . close ( ) ;
124134 }
125135 } ) ;
126136
137+ this . disposeListeners = ( ) => {
138+ disposeCloseListener ( ) ;
139+ disposeSchemaListener ( ) ;
140+ } ;
141+
127142 // Initial setup
128- await this . runWithReporting ( async ( ) => {
143+ this . runWithReporting ( async ( ) => {
129144 await this . updateSettings ( this . options . watchOptions ) ;
130145 } ) ;
131146 }
@@ -134,18 +149,19 @@ export abstract class AbstractQueryProcessor<Data = unknown[]>
134149 // hook in to subscription events in order to report changes
135150 const baseDispose = this . registerListener ( { ...subscription } ) ;
136151
137- const counts = this . subscriptionCounts ;
138- this . iterateListeners ( ( l ) => l . subscriptionsChanged ?.( counts ) ) ;
152+ this . iterateListeners ( ( l ) => l . subscriptionsChanged ?.( this . subscriptionCounts ) ) ;
139153
140154 return ( ) => {
141155 baseDispose ( ) ;
142- this . iterateListeners ( ( l ) => l . subscriptionsChanged ?.( counts ) ) ;
156+ this . iterateListeners ( ( l ) => l . subscriptionsChanged ?.( this . subscriptionCounts ) ) ;
143157 } ;
144158 }
145159
146160 async close ( ) {
147161 await this . initialized ;
148162 this . abortController . abort ( ) ;
163+ this . disposeListeners ?.( ) ;
164+ this . disposeListeners = null ;
149165 this . _closed = true ;
150166 this . iterateListeners ( ( l ) => l . closed ?.( ) ) ;
151167 }
0 commit comments