Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add events to upgrade & blocking callbacks #282

Merged
merged 4 commits into from Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions README.md
Expand Up @@ -81,13 +81,13 @@ This method opens a database, and returns a promise for an enhanced [`IDBDatabas

```js
const db = await openDB(name, version, {
upgrade(db, oldVersion, newVersion, transaction) {
upgrade(db, oldVersion, newVersion, transaction, event) {
// …
},
blocked() {
// …
},
blocking() {
blocking(currentVersion, blockedVersion, event) {
// …
},
terminated() {
Expand All @@ -103,8 +103,12 @@ const db = await openDB(name, version, {
- `oldVersion`: Last version of the database opened by the user.
- `newVersion`: Whatever new version you provided.
- `transaction`: An enhanced transaction for this upgrade. This is useful if you need to get data from other stores as part of a migration.
- `event`: The event object for the associated `upgradeneeded` event.
- `blocked` (optional): Called if there are older versions of the database open on the origin, so this version cannot open. This is similar to the [`blocked` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBOpenDBRequest/blocked_event) in plain IndexedDB.
- `blocking` (optional): Called if this connection is blocking a future version of the database from opening. This is similar to the [`versionchange` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/versionchange_event) in plain IndexedDB.
- `currentVersion`: Version of the open database (whatever version you provided to `openDB`).
- `blockedVersion`: The version of the database that's being blocked.
- `event`: The event object for the associated `versionchange` event.
- `terminated` (optional): Called if the browser abnormally terminates the connection, but not on regular closures like calling `db.close()`. This is similar to the [`close` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/close_event) in plain IndexedDB.

## `deleteDB`
Expand Down Expand Up @@ -320,19 +324,19 @@ const dbPromise = openDB('keyval-store', 1, {

export async function get(key) {
return (await dbPromise).get('keyval', key);
};
}
export async function set(key, val) {
return (await dbPromise).put('keyval', val, key);
};
}
export async function del(key) {
return (await dbPromise).delete('keyval', key);
};
}
export async function clear() {
return (await dbPromise).clear('keyval');
};
}
export async function keys() {
return (await dbPromise).getAllKeys('keyval');
};
}
```

## Article store
Expand Down
23 changes: 19 additions & 4 deletions src/entry.ts
Expand Up @@ -8,8 +8,9 @@ export interface OpenDBCallbacks<DBTypes extends DBSchema | unknown> {
* @param database A database instance that you can use to add/remove stores and indexes.
* @param oldVersion Last version of the database opened by the user.
* @param newVersion Whatever new version you provided.
* @param transaction The transaction for this upgrade. This is useful if you need to get data
* from other stores as part of a migration.
* @param transaction The transaction for this upgrade.
* This is useful if you need to get data from other stores as part of a migration.
* @param event The event object for the associated 'upgradeneeded' event.
*/
upgrade?(
database: IDBPDatabase<DBTypes>,
Expand All @@ -20,6 +21,7 @@ export interface OpenDBCallbacks<DBTypes extends DBSchema | unknown> {
StoreNames<DBTypes>[],
'versionchange'
>,
event: IDBVersionChangeEvent,
): void;
/**
* Called if there are older versions of the database open on the origin, so this version cannot
Expand All @@ -28,8 +30,16 @@ export interface OpenDBCallbacks<DBTypes extends DBSchema | unknown> {
blocked?(): void;
/**
* Called if this connection is blocking a future version of the database from opening.
*
* @param currentVersion Version of the open database (whatever version you provided to `openDB`).
* @param blockedVersion The version of the database that's being blocked.
* @param event The event object for the associated 'versionchange' event.
*/
blocking?(): void;
blocking?(
currentVersion: number,
blockedVersion: number | null,
event: IDBVersionChangeEvent,
): void;
/**
* Called if the browser abnormally terminates the connection.
* This is not called when `db.close()` is called.
Expand Down Expand Up @@ -63,6 +73,7 @@ export function openDB<DBTypes extends DBSchema | unknown = unknown>(
StoreNames<DBTypes>[],
'versionchange'
>,
event,
);
});
}
Expand All @@ -72,7 +83,11 @@ export function openDB<DBTypes extends DBSchema | unknown = unknown>(
openPromise
.then((db) => {
if (terminated) db.addEventListener('close', () => terminated());
if (blocking) db.addEventListener('versionchange', () => blocking());
if (blocking) {
db.addEventListener('versionchange', (event) =>
blocking(event.oldVersion, event.newVersion, event),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Typescript types it as a simple event, so I missed it.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

);
}
})
.catch(() => {});

Expand Down
23 changes: 18 additions & 5 deletions test/open.ts
Expand Up @@ -23,7 +23,7 @@ suite('openDb', () => {
let upgradeRun = false;
const version = getNextVersion();
db = (await openDB<TestDBSchema>(dbName, version, {
upgrade(db, oldVersion, newVersion, tx) {
upgrade(db, oldVersion, newVersion, tx, event) {
upgradeRun = true;

typeAssert<IsExact<typeof db, IDBPDatabase<TestDBSchema>>>(true);
Expand All @@ -42,8 +42,11 @@ suite('openDb', () => {
>
>
>(true);
assert.instanceOf(tx, IDBTransaction, 'db instance');
assert.instanceOf(tx, IDBTransaction, 'transaction');
assert.strictEqual(tx.mode, 'versionchange', 'tx mode');

assert.instanceOf(event, IDBVersionChangeEvent, 'event');
typeAssert<IsExact<typeof event, IDBVersionChangeEvent>>(true);
},
})) as IDBPDatabase;

Expand Down Expand Up @@ -120,12 +123,22 @@ suite('openDb', () => {
let newDbBlockedCalled = false;
let newDbBlockingCalled = false;

db = (await openDB<TestDBSchema>(dbName, getNextVersion(), {
const firstVersion = getNextVersion();
const nextVersion = getNextVersion();

db = (await openDB<TestDBSchema>(dbName, firstVersion, {
blocked() {
blockedCalled = true;
},
blocking() {
blocking(currentVersion, blockedVersion, event) {
blockingCalled = true;

assert.strictEqual(currentVersion, firstVersion);
assert.strictEqual(blockedVersion, nextVersion);

assert.instanceOf(event, IDBVersionChangeEvent, 'event');
typeAssert<IsExact<typeof event, IDBVersionChangeEvent>>(true);

// 'blocked' isn't called if older databases close once blocking fires.
// Using set timeout so closing isn't immediate.
setTimeout(() => db.close(), 0);
Expand All @@ -135,7 +148,7 @@ suite('openDb', () => {
assert.isFalse(blockedCalled);
assert.isFalse(blockingCalled);

db = (await openDB<TestDBSchema>(dbName, getNextVersion(), {
db = (await openDB<TestDBSchema>(dbName, nextVersion, {
blocked() {
newDbBlockedCalled = true;
},
Expand Down