@@ -218,13 +218,30 @@ void _sqliteConnectionIsolate(_SqliteConnectionParams params) async {
218218 final server = params.portServer;
219219 final commandPort = ReceivePort ();
220220
221+ Timer ? updateDebouncer;
221222 Set <String > updatedTables = {};
222223 int ? txId;
223224 Object ? txError;
224225
226+ void maybeFireUpdates () {
227+ if (updatedTables.isNotEmpty) {
228+ client.fire (UpdateNotification (updatedTables));
229+ updatedTables.clear ();
230+ updateDebouncer? .cancel ();
231+ updateDebouncer = null ;
232+ }
233+ }
234+
225235 db.updates.listen ((event) {
226236 updatedTables.add (event.tableName);
237+
238+ // This handles two cases:
239+ // 1. Update arrived after _SqliteIsolateClose (not sure if this could happen).
240+ // 2. Long-running _SqliteIsolateClosure that should fire updates while running.
241+ updateDebouncer ?? =
242+ Timer (const Duration (milliseconds: 10 ), maybeFireUpdates);
227243 });
244+
228245 server.open ((data) async {
229246 if (data is _SqliteIsolateClose ) {
230247 if (txId != null ) {
@@ -238,6 +255,8 @@ void _sqliteConnectionIsolate(_SqliteConnectionParams params) async {
238255 throw sqlite.SqliteException (
239256 0 , 'Transaction must be closed within the read or write lock' );
240257 }
258+ // We would likely have received updates by this point - fire now.
259+ maybeFireUpdates ();
241260 return null ;
242261 } else if (data is _SqliteIsolateStatement ) {
243262 if (data.sql == 'BEGIN' || data.sql == 'BEGIN IMMEDIATE' ) {
@@ -261,10 +280,6 @@ void _sqliteConnectionIsolate(_SqliteConnectionParams params) async {
261280 }
262281 try {
263282 final result = db.select (data.sql, mapParameters (data.args));
264- if (updatedTables.isNotEmpty) {
265- client.fire (UpdateNotification (updatedTables));
266- updatedTables = {};
267- }
268283 return result;
269284 } catch (err) {
270285 if (txId != null ) {
@@ -276,10 +291,7 @@ void _sqliteConnectionIsolate(_SqliteConnectionParams params) async {
276291 try {
277292 return await data.cb (db);
278293 } finally {
279- if (updatedTables.isNotEmpty) {
280- client.fire (UpdateNotification (updatedTables));
281- updatedTables = {};
282- }
294+ maybeFireUpdates ();
283295 }
284296 } else if (data is _SqliteIsolateConnectionClose ) {
285297 db.dispose ();
0 commit comments