有什么方法可以查明 localForage/indexedDb 是否正在处理数据?

Is there any way to find out if localForage/indexedDb is processing data?

所以我有这个 webapp,我正在使用异步 localForage(angular localForage 具体而言)。现在,如果用户试图关闭浏览器 window,但仍有一些 localForage 操作正在进行,我想警告他。

我知道至少浏览器知道,因为在 Firefox 中如果我关闭 window,它会给我一个警告(尽管在我再次打开 window 之后)一些 indexedDb 操作是取消(localForage 使用 indexedDb)。

没有类似的东西可用,但您可以用

的自定义服务包装 localForage
  1. localForage 的操作启动后设置一个特殊标志
  2. 在回调中释放标志(一旦操作完成)

我认为这有点棘手,因为我不知道任何 "IsBusy" 函数可以与 localForage 甚至 IndexedDB 一起使用。可以设计一个合适的解决方案 ad-hoc 来检查单个操作是否已完成。这也可以用作所有 localForage 异步操作的全面监视器。

例如:

function cbFunc(err, value) {
                    // Run this code once the value has been
                    // loaded from the offline store.
                    console.log(value);

                    DBWrapper.numCurrOps--;
                    if (DBWrapper.numCurrOps === 0)
                        DBWrapper.isBusy = false;
}

    var DBWrapper = {
            isBusy: false,
            numCurrOps: 0,
            getItem: function(key, cbFunc){
                DBWrapper.isBusy = true;
                DBWrapper.numCurrOps++;
                localforage.getItem('somekey', cbFunc);     
            }

        };

这是一个包装器示例,可以帮助您确定是否有至少一个 运行 异步操作在后台等待完成。您可以向单例对象添加更多函数,并使它们也更改 "isBusy" 属性。