英文:
Sometime indexedDB takes so much to store the data
问题
Here is the translated content:
我在使用indexedDB时遇到了一个奇怪的问题。每当我安装另一个扩展程序时,有时indexedDB在将数据写入数据库时会花费很长时间。
我尝试使用indexedDB API和基于indexedDB的库来解决这个问题,但似乎什么都不起作用。
以下是我正在使用的代码:
function updateRecord({
sessionId,
...record
}) {
return new Promise(async (resolve, reject) => {
try {
console.log(
'%c Inside update record ',
'background: #222; color: #bada55'
);
const dbPromise = await idb.openDB('testbuddyExtension', 1, {
upgrade(db) {
const store = db.createObjectStore('testbuddy', {
keyPath: 'sessionId',
});
store.createIndex('keyIndex', 'tabId');
},
});
const existingRecord = await dbPromise.get('testbuddy', sessionId);
const updatedPayload = {
...record,
...(existingRecord ? existingRecord : {}),
};
await dbPromise.put('testbuddy', { ...updatedPayload, sessionId });
console.log(
'%c Everything is now done! ',
'background: #222; color: #bada55'
);
resolve(true);
} catch (error) {
console.log('%c Error found! ', 'background: #222; color: #bada55');
console.log({ error });
reject(false);
}
});
}
这里是一个可能会帮助你理解的视频。另外,它不是因为我一次写入大量数据而减慢的情况。我已经使用相同的脚本一次写入了数兆字节的数据。
英文:
I am facing one weird issue while working with indexedDB. Whenever I install another extension, sometimes indexedDB takes so much of time to write the data into db.
I tried to fix this issue by using indexedDB API and a library based on indexedDB but nothing seems to work.
Here is the code I am using
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function updateRecord({
sessionId,
...record
}) {
return new Promise(async(resolve, reject) => {
try {
console.log(
'%c Inside update record ',
'background: #222; color: #bada55'
);
const dbPromise = await idb.openDB('testbuddyExtension', 1, {
upgrade(db) {
const store = db.createObjectStore('testbuddy', {
keyPath: 'sessionId',
});
store.createIndex('keyIndex', 'tabId');
},
});
const existingRecord = await dbPromise.get('testbuddy', sessionId);
const updatedPayload = {
...record,
...(existingRecord ? existingRecord : {}),
};
await dbPromise.put('testbuddy', { ...updatedPayload,
sessionId
});
console.log(
'%c Everything is now done! ',
'background: #222; color: #bada55'
);
resolve(true);
} catch (error) {
console.log('%c Error found! ', 'background: #222; color: #bada55');
console.log({
error
});
reject(false);
}
});
}
<!-- end snippet -->
And here is a video that might help you understand. Also, it is not the case that it is slowing down because I am writing a whole lot of data at once. I have used the same script to write down megabytes of data at once.
答案1
得分: 0
这个问题现在已经解决。正如您在视频中所看到的,当我启用另一个扩展时,我的扩展引发了一些错误。那是因为我在扩展启用时运行了一些脚本。我忘记提及扩展的ID。
chrome.management.onEnabled.addListener(() => {
// 每当安装任何扩展时运行此代码
destroyDatabase().catch((error) => {
console.error('Failed to delete database', error);
});
reExecuteScript();
});
而应该这样做:
chrome.management.onEnabled.addListener((data) => {
if (data.id === chrome.runtime.id) {
destroyDatabase().catch((error) => {
console.error('Failed to delete database', error);
});
reExecuteScript();
}
});
英文:
This issue is resolved now. As you can see in the video, when I enabled another extension, my extension raised some errors. And that was because I was running some script whenever the extension is enabled. And I forgot to mention the extension id.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
chrome.management.onEnabled.addListener(() => {
//This code was running whenever any extension is installed
destroyDatabase().catch((error) => {
console.error('Failed to delete database', error);
});
reExecuteScript();
});
<!-- end snippet -->
And this is how it should be done.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
chrome.management.onEnabled.addListener((data) => {
if (data.id === chrome.runtime.id) {
destroyDatabase().catch((error) => {
console.error('Failed to delete database', error);
});
reExecuteScript();
}
});
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论