javascript - 中止 Dexie.js 查询
问题描述
在我的应用程序中,用户指定了查询的某些部分。在用户更改查询中的某些内容后,我会立即做出反应。在大型数据集上,这是一个问题 - 查询可能需要大约 2 秒才能完成,有时用户会在查询完成之前应用额外的约束,因此会创建一个新查询,因此用户会同时应用太多查询而使系统不堪重负. 当多个查询运行时,即使是 2 秒的查询也变成了 30 秒的查询。这是一种病态的极端情况,一旦用户指定了所有参数,就不希望用户有一个额外的按钮来触发查询。
Dexie 是否有可能在查询完成之前取消查询?当用户指定一个新查询时,我想取消上一个查询。
解决方案
事务可以中止。我还没有测试过,但是一种方法应该是,如果您在事务中打开每个查询并将事务存储在一个状态中,这样您就可以在新事务即将触发时中止先前的事务。
function cancellableDexieQuery(includedTables, querierFunction) {
let tx = null;
let cancelled = false;
const promise = db.transaction('r', includedTables, () => {
if (cancelled) throw new Dexie.AbortError('Query was cancelled');
tx = Dexie.currentTransaction;
return querierFunction();
});
return [
promise,
() => {
cancelled = true; // In case transaction hasn't been started yet.
if (tx) tx.abort(); // If started, abort it.
tx = null; // Avoid calling abort twice.
}
];
}
然后作为使用此辅助函数的示例:
const [promise1, cancel1] = cancellableDexieQuery(
"friends",
()=>db.friends.where('name').startsWith('A').toArray()
);
cancel1(); // Cancel the operation we just started.
const [promise2, cancel2] = cancellableDexieQuery(
"friends",
()=>db.friends.where('name').startsWith('B').toArray()
);
promise1.catch(error => {
// Expect a Dexie.AbortError
}
promise2.then(result => {
// Expect the array as result
});
免责声明:我没有测试过这个代码,它只是干编码的。如果您尝试这个或者代码片段中是否有任何拼写错误,请回复它是否是一个有效的解决方案。
推荐阅读
- javascript - 在 JSON 的开头和结尾添加单引号
- json - 解析股票市场数据的嵌套 JSON 数据
- web-scraping - Google 表格:从 IMPORTDATA() 中拆分多行分隔文本
- python - 如何检测 Tkinter 中单击的按钮在哪一帧上?
- swift - SwiftUI 新手:绑定、切换和致命错误:索引超出范围
- notepad++ - SRT 文件问题(索引和空行)
- python - Python:IndexError:修改代码后列表索引超出范围
- c - 给定区间之间的质数
- typescript - 当同一接口上存在其他属性(字符串文字)时,有条件地使类型成为必需
- python - Keras Sequential:创建 Tensorflow numeric_columns 的优势?