@@ -164,51 +164,43 @@ void open_a_database_connection(JS::Realm& realm, StorageAPI::StorageKey storage
164164 // 4. If any of the connections in openConnections are still not closed,
165165 // queue a database task to fire a version change event named blocked at request with db’s version and version.
166166 for (auto const & entry : open_connections) {
167- if (entry->state () != IDBDatabase:: ConnectionState::Closed) {
167+ if (entry->state () != ConnectionState::Closed) {
168168 queue_a_database_task (GC::create_function (realm.vm ().heap (), [&realm, entry, db, version]() {
169169 fire_a_version_change_event (realm, HTML::EventNames::blocked, *entry, db->version (), version);
170170 }));
171171 }
172172 }
173173
174174 // 5. Wait until all connections in openConnections are closed.
175- HTML::main_thread_event_loop ().spin_until (GC::create_function (realm.vm ().heap (), [open_connections]() {
176- if constexpr (IDB_DEBUG) {
177- dbgln (" open_a_database_connection: waiting for step 10.5" );
178- dbgln (" open connections: {}" , open_connections.size ());
179- for (auto const & connection : open_connections) {
180- dbgln (" - {}" , connection->uuid ());
181- }
182- }
183-
184- for (auto const & entry : open_connections) {
185- if (entry->state () != IDBDatabase::ConnectionState::Closed) {
186- return false ;
187- }
175+ if constexpr (IDB_DEBUG) {
176+ dbgln (" open_a_database_connection: waiting for step 10.5" );
177+ dbgln (" open connections: {}" , open_connections.size ());
178+ for (auto const & open_connection : open_connections) {
179+ dbgln (" - {}" , open_connection->uuid ());
188180 }
181+ }
189182
190- return true ;
191- }));
192-
193- // 6. Run upgrade a database using connection, version and request.
194- upgrade_a_database (realm, connection, version, request);
183+ db->wait_for_connections_to_close (open_connections, GC::create_function (realm.heap (), [&realm, connection, version, request, on_complete] {
184+ // 6. Run upgrade a database using connection, version and request.
185+ upgrade_a_database (realm, connection, version, request);
195186
196- // 7. If connection was closed, return a newly created "AbortError" DOMException and abort these steps.
197- if (connection->state () == IDBDatabase:: ConnectionState::Closed) {
198- on_complete->function ()(WebIDL::AbortError::create (realm, " Connection was closed" _utf16));
199- return ;
200- }
187+ // 7. If connection was closed, return a newly created "AbortError" DOMException and abort these steps.
188+ if (connection->state () == ConnectionState::Closed) {
189+ on_complete->function ()(WebIDL::AbortError::create (realm, " Connection was closed" _utf16));
190+ return ;
191+ }
201192
202- // 8. If request's error is set, run the steps to close a database connection with connection,
203- // return a newly created "AbortError" DOMException and abort these steps.
204- if (request->has_error ()) {
205- close_a_database_connection (*connection);
206- on_complete->function ()(WebIDL::AbortError::create (realm, " Upgrade transaction was aborted" _utf16));
207- return ;
208- }
193+ // 8. If request's error is set, run the steps to close a database connection with connection,
194+ // return a newly created "AbortError" DOMException and abort these steps.
195+ if (request->has_error ()) {
196+ close_a_database_connection (*connection);
197+ on_complete->function ()(WebIDL::AbortError::create (realm, " Upgrade transaction was aborted" _utf16));
198+ return ;
199+ }
209200
210- // 11. Return connection.
211- on_complete->function ()(connection);
201+ // 11. Return connection.
202+ on_complete->function ()(connection);
203+ }));
212204 });
213205
214206 if (task_counter_state) {
@@ -396,7 +388,7 @@ void close_a_database_connection(GC::Ref<IDBDatabase> connection, bool forced)
396388 return true ;
397389 }));
398390
399- connection->set_state (IDBDatabase:: ConnectionState::Closed);
391+ connection->set_state (ConnectionState::Closed);
400392
401393 // 4. If the forced flag is true, then fire an event named close at connection.
402394 if (forced)
@@ -535,44 +527,36 @@ void delete_a_database(JS::Realm& realm, StorageAPI::StorageKey storage_key, Str
535527 auto after_all = GC::create_function (realm.heap (), [&realm, open_connections, db, storage_key = move (storage_key), name = move (name), on_complete] {
536528 // 8. If any of the connections in openConnections are still not closed, queue a database task to fire a version change event named blocked at request with db’s version and null.
537529 for (auto const & entry : open_connections) {
538- if (entry->state () != IDBDatabase:: ConnectionState::Closed) {
530+ if (entry->state () != ConnectionState::Closed) {
539531 queue_a_database_task (GC::create_function (realm.vm ().heap (), [&realm, entry, db]() {
540532 fire_a_version_change_event (realm, HTML::EventNames::blocked, *entry, db->version (), {});
541533 }));
542534 }
543535 }
544536
545537 // 9. Wait until all connections in openConnections are closed.
546- HTML::main_thread_event_loop ().spin_until (GC::create_function (realm.vm ().heap (), [open_connections]() {
547- if constexpr (IDB_DEBUG) {
548- dbgln (" delete_a_database: waiting for step 9" );
549- dbgln (" open connections: {}" , open_connections.size ());
550- for (auto const & connection : open_connections) {
551- dbgln (" - {}" , connection->uuid ());
552- }
538+ if constexpr (IDB_DEBUG) {
539+ dbgln (" delete_a_database: waiting for step 9" );
540+ dbgln (" open connections: {}" , open_connections.size ());
541+ for (auto const & connection : open_connections) {
542+ dbgln (" - {}" , connection->uuid ());
553543 }
544+ }
554545
555- for (auto const & entry : open_connections) {
556- if (entry->state () != IDBDatabase::ConnectionState::Closed) {
557- return false ;
558- }
546+ db->wait_for_connections_to_close (open_connections, GC::create_function (realm.heap (), [&realm, db, storage_key = move (storage_key), name = move (name), on_complete] {
547+ // 10. Let version be db’s version.
548+ auto version = db->version ();
549+
550+ // 11. Delete db. If this fails for any reason, return an appropriate error (e.g. "QuotaExceededError" or "UnknownError" DOMException).
551+ auto maybe_deleted = Database::delete_for_key_and_name (storage_key, name);
552+ if (maybe_deleted.is_error ()) {
553+ on_complete->function ()(WebIDL::OperationError::create (realm, " Unable to delete database" _utf16));
554+ return ;
559555 }
560556
561- return true ;
557+ // 12. Return version.
558+ on_complete->function ()(version);
562559 }));
563-
564- // 10. Let version be db’s version.
565- auto version = db->version ();
566-
567- // 11. Delete db. If this fails for any reason, return an appropriate error (e.g. "QuotaExceededError" or "UnknownError" DOMException).
568- auto maybe_deleted = Database::delete_for_key_and_name (storage_key, name);
569- if (maybe_deleted.is_error ()) {
570- on_complete->function ()(WebIDL::OperationError::create (realm, " Unable to delete database" _utf16));
571- return ;
572- }
573-
574- // 12. Return version.
575- on_complete->function ()(version);
576560 });
577561
578562 if (task_counter_state) {
0 commit comments