@@ -25,9 +25,9 @@ export function openDatabaseQueue(dbName: string) {
2525}
2626
2727export function closeDatabaseQueue ( dbName : string ) {
28- const queue = getDatabaseQueue ( dbName )
28+ const databaseQueue = getDatabaseQueue ( dbName )
2929
30- if ( queue . inProgress || queue . queue . length > 0 ) {
30+ if ( databaseQueue . inProgress || databaseQueue . queue . length > 0 ) {
3131 console . warn (
3232 `Database queue for ${ dbName } has operations in the queue. Closing anyway.`
3333 )
@@ -66,7 +66,7 @@ export function queueOperationAsync<
6666 OperationCallback extends ( ) => Promise < Result > ,
6767 Result = void ,
6868> ( dbName : string , callback : OperationCallback ) {
69- const queue = getDatabaseQueue ( dbName )
69+ const databaseQueue = getDatabaseQueue ( dbName )
7070
7171 return new Promise < Result > ( ( resolve , reject ) => {
7272 const operation : QueuedOperation = {
@@ -77,52 +77,50 @@ export function queueOperationAsync<
7777 } catch ( error ) {
7878 reject ( error )
7979 } finally {
80- queue . inProgress = false
80+ databaseQueue . inProgress = false
8181 startOperationAsync ( dbName )
8282 }
8383 } ,
8484 }
8585
86- queue . queue . push ( operation )
86+ databaseQueue . queue . push ( operation )
8787 startOperationAsync ( dbName )
8888 } )
8989}
9090
9191function startOperationAsync ( dbName : string ) {
9292 const queue = getDatabaseQueue ( dbName )
9393
94- if ( queue . inProgress ) {
95- // Operation is already in process bail out
94+ // Queue is empty or in progress. Bail out.
95+ if ( queue . inProgress || queue . queue . length === 0 ) {
9696 return
9797 }
9898
99- if ( queue . queue . length > 0 ) {
100- queue . inProgress = true
99+ queue . inProgress = true
101100
102- const operation = queue . queue . shift ( ) !
103- setImmediate ( ( ) => {
104- operation . start ( )
105- } )
106- }
101+ const operation = queue . queue . shift ( ) !
102+ setImmediate ( ( ) => {
103+ operation . start ( )
104+ } )
107105}
108106
109107export function startOperationSync <
110108 OperationCallback extends ( ) => Result ,
111109 Result = void ,
112110> ( dbName : string , callback : OperationCallback ) {
113- const queue = getDatabaseQueue ( dbName )
111+ const databaseQueue = getDatabaseQueue ( dbName )
114112
115113 // Database is busy - cannot execute synchronously
116- if ( queue . inProgress || queue . queue . length > 0 ) {
114+ if ( databaseQueue . inProgress || databaseQueue . queue . length > 0 ) {
117115 throw new NitroSQLiteError (
118116 `Cannot run synchronous operation on database. Database ${ dbName } is busy with another operation.`
119117 )
120118 }
121119
122120 // Execute synchronously
123- queue . inProgress = true
121+ databaseQueue . inProgress = true
124122 const result = callback ( )
125- queue . inProgress = false
123+ databaseQueue . inProgress = false
126124
127125 return result
128126}
0 commit comments