是否有任何方法可以查明localForage/indexedDb是否正在处理数据

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

本文关键字:是否 indexedDb 数据 处理 localForage 任何 方法      更新时间:2023-09-26

所以我有了这个web应用程序,并且我正在使用async localForage(具体来说是angular localForage)。现在,我想警告用户,如果他试图关闭浏览器窗口,而仍有一些本地Forage操作在进行。

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

没有类似的服务,但您可以用的自定义服务包装localForage

  1. 启动localForage操作后设置一个特殊标志
  2. 释放回调中的标志(一旦操作完成)

我觉得这有点棘手,因为我不知道任何"IsBusy"函数可以与localForage甚至IndexedDB一起使用。可以专门设计一个合适的解决方案来检查单个操作是否已经完成。这也可以被用作所有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);     
            }
        };

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