Skip to content

Commit

Permalink
#483 Fix level wrapper close implementation (#574)
Browse files Browse the repository at this point in the history
* Fix level wrapper close implementation

* Lint
  • Loading branch information
diehuxx committed Oct 24, 2023
1 parent 62d5656 commit 52523db
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/store/level-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { AbstractBatchOperation, AbstractDatabaseOptions, AbstractIteratorO

import { executeUnlessAborted } from '../utils/abort.js';
import { Level } from 'level';
import { sleep } from '../utils/time.js';

export type CreateLevelDatabaseOptions<V> = AbstractDatabaseOptions<string, V>;

Expand Down Expand Up @@ -84,15 +83,31 @@ export class LevelWrapper<V> {
return;
}

while (this.db.status === 'opening' || this.db.status === 'closing') {
await sleep(200);
}
switch (this.db.status) {
// If db is open, we `db.close`.
case 'open':
return this.db.close();

if (this.db.status === 'closed') {
// If db is still opening, wait until it is open then await `db.close()`
case 'opening':
return new Promise((resolve, reject) => {
const onOpen = (): void => {
// Make sure that errors from `db.open()` propogate up
this.db.close().then(resolve).catch(reject);;
};
this.db.once('open', onOpen);
});

// If db is closing, wait until the 'closed' event is emitted
case 'closing':
return new Promise((resolve) => {
this.db.once('closed', resolve);
});

// If db is closed, we are done
case 'closed':
return;
}

return this.db.close();
}

async partition(name: string): Promise<LevelWrapper<V>> {
Expand Down

0 comments on commit 52523db

Please sign in to comment.