你好我正在建立一个应用程序,我在其中使用indexeDB。基于应用程序配置,我可以选择是否应该从WebWorker或主UI线程使用indexeDB。无论配置如何,主UI线程的连接总是在进行。但是,根据配置,如果选择了繁重的工作,那么工人将完成繁重的工作。进口样机的IndexedDB主线程和WebWorker eventlisteners
例子:
Database.prototype.importItem = function(item, callback) {
if (this.settings.useWorker) {
this.postMessage({
cmd: "importItem",
data: item
}, callback);
} else {
var that = this;
var transaction = this.db.transaction(this.settings.collection, "readwrite");
var objectStore = transaction.objectStore(this.settings.collection);
var request = objectStore.add(item);
request.onerror = function(evt) {
if (callback && callback instanceof Function) {
callback(new ApplicationError("importItem error", evt.target.error), null);
}
};
request.onsuccess = function(evt) {
if (callback && callback instanceof Function) {
callback(null, evt.target.result);
}
};
transaction.oncomplete = function(evt) {};
}
};
我很好奇的想法是,从索引资料所需的事件监听器,例如:
Database.prototype.connect = function() {
if (!this.supported()) {
return this.emit("error", new ApplicationError("IndexedDB not supported!"));
}
var that = this;
var openRequest = window.indexedDB.open(this.settings.dbName, this.settings.dbVersion);
/**
* [onerror description]
* @param {[type]} evt [description]
* @return {[type]} [description]
*/
openRequest.onerror = function(evt) {
that.emit("error", new ApplicationError("openRequest.onerror error", evt.target.error));
};
/**
* [onblocked description]
* @param {[type]} evt [description]
* @return {[type]} [description]
*/
openRequest.onblocked = function(evt) {
// If some other tab is loaded with the database, then it needs to be closed
// before we can proceed.
alert("Please close all other tabs with this site open!");
};
/**
* [onsuccess description]
* @param {[type]} evt [description]
* @return {[type]} [description]
*/
openRequest.onsuccess = function(evt) {
that.db = evt.target.result;
that.db.onerror = function(evt) {
logger.warn("openRequest.onsuccess error", evt.target);
};
that.db.onabort = function(evt) {
logger.warn("openRequest.onsuccess abort", evt.target);
};
that.db.onversionchange = function(evt) {
that.db.close();
that.emit("versionchange", new ApplicationError("openRequest.onsuccess version change", evt.target));
};
if (that.settings.useWorker) {
that.requestWorker();
} else {
that.emit("connect");
}
};
/**
* [onupgradeneeded description]
* @param {[type]} evt [description]
* @return {[type]} [description]
*/
openRequest.onupgradeneeded = function(evt) {
var stores = {};
that.db = evt.target.result;
that.db.onerror = function(evt) {
logger.warn("openRequest.onupgradeneeded error", evt.target);
};
that.db.onabort = function(evt) {
logger.warn("openRequest.onupgradeneeded abort", evt.target);
};
that.db.onversionchange = function(evt) {
that.db.close();
that.emit("versionchange", new ApplicationError("openRequest.onupgradeneeded version change", evt.target));
};
// Check for the objectStore - collection and delete it if exists
if (that.db.objectStoreNames.contains(that.settings.collection)) {
that.db.deleteObjectStore(that.settings.collection);
}
// Create new objectStore
stores[that.settings.collection] = that.db.createObjectStore(that.settings.collection, {
keyPath: that.settings.indexes[0]
});
// Create database indexes
that.settings.indexes.forEach(function(index) {
stores[that.settings.collection].createIndex(index, index, {
unique: false
});
});
that.upgraded = true;
that.emit("upgrade");
};
/**
* [onbeforeunload description]
* @param {[type]} evt [description]
* @return {[type]} [description]
*/
window.onbeforeunload = function(evt) {
that.db.close();
};
};
由于我第一次从连接总是主要的用户界面,然后从工作人员,我应该只在主UI线程而不是工作人员监听像“onblocked,versionchange”这样的事件吗?我假设不需要从两个线程中听取?
UPDATE
我知道,这是一个奇怪的实现,但我想它的原因是因为我建立一个非常机到有RAM的3GB和2个核心上的应用...此外,我有一个方法,从我的数据库迭代集合中的所有记录。我在想的是将每条记录都传回到另一个方法来处理图像,然后可能调用回调......这会限制内存使用,因为如果我没有弄错,它会在系列中完成。但是我不确定交易是否仍然活着。
底线是我考虑进行2连接的主要原因(因为有1个方法),而且我想知道是否可以避免双重事件监听器。也许有一些线程在其中。
为什么你甚至连接两个线程,如果你总是只使用一个线程的indexedDB? –
由于webworker不支持某些方法。例如,我有一个迭代集合中所有项目的方法,并将光标传递给执行画布绘制的回调,并且Web工作人员不支持画布。 – Syd
是的,但是您不需要主线程中的indexedDB连接 - 您仍然可以检索Worker中的数据并将它们发送到主线程;然后画在画布上。您不需要从主线程和工作者连接。你所描述的只是糟糕的设计,或者你描述的错误。 –