Add batchWriting() to database
This commit is contained in:
parent
1dad1d4e2b
commit
93d610e942
@ -323,6 +323,38 @@ export class Database {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public batchWriting(payload: { storeName: string; objects: { key: any; object: any }[] }): Promise<void> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
if (!this.serviceWorkerRegistration) {
|
||||||
|
this.serviceWorkerRegistration = await navigator.serviceWorker.ready;
|
||||||
|
}
|
||||||
|
|
||||||
|
const activeWorker = await this.waitForServiceWorkerActivation(this.serviceWorkerRegistration);
|
||||||
|
const messageChannel = new MessageChannel();
|
||||||
|
|
||||||
|
messageChannel.port1.onmessage = (event) => {
|
||||||
|
if (event.data.status === 'success') {
|
||||||
|
resolve();
|
||||||
|
} else {
|
||||||
|
const error = event.data.message;
|
||||||
|
reject(new Error(error || 'Unknown error occurred while adding objects'));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
activeWorker?.postMessage(
|
||||||
|
{
|
||||||
|
type: 'BATCH_WRITING',
|
||||||
|
payload,
|
||||||
|
},
|
||||||
|
[messageChannel.port2],
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
reject(new Error(`Failed to send message to service worker: ${error}`));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public async getObject(storeName: string, key: string): Promise<any | null> {
|
public async getObject(storeName: string, key: string): Promise<any | null> {
|
||||||
const db = await this.getDb();
|
const db = await this.getDb();
|
||||||
const tx = db.transaction(storeName, 'readonly');
|
const tx = db.transaction(storeName, 'readonly');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user