简体   繁体   English

使用Dexie.js处理请求队列异步

[英]Handling request queue asynchronos with Dexie.js

I'm currently working on processing "cached" requests out of an IndexedDB using Dexie.js. 我目前正在使用Dexie.js从IndexedDB处理“缓存的”请求。 At first I save my requests into an IndexedDB queue for than to be flushed all at once. 首先,我将请求保存到IndexedDB队列中,然后立即将其全部刷新。 Unfortunately it seems that I can't delete my entries after they got sent out. 不幸的是,似乎我的条目发送出去后无法删除。

I followed the guides/samples from the official docs but I just can't get it to work. 我遵循了官方文档中的指南/示例,但是我无法使其正常工作。 I even tried to campsulate it into a db.transaction with 'rw' 我什至尝试使用'rw'将其预占为db.transaction

function flushQueue(){
  queueRunning = true; // used to mimic singleton pattern

  return db.queue.orderBy(':id').modify(async function(value){
    var _this = this;
    return deserializeRequest(value.serializedReq).then(async function(request){
      return fetch(request).then(function(){
        notifyClients("Request has been sent: ", request);
        // deleting request from queue
        delete _this.value;
      });
    });
  });
}

Any ideas what could have gone wrong? 任何想法可能出了什么问题?

Kind regards, Felix 亲切的问候,菲利克斯

The callback to modify must be synchronous and cannot be async. 要修改的回调必须是同步的,并且不能是异步的。 If you need to do async work, you have to pull the existing entries first using toArray(), then modify them in memory, and when done, use bulkPut() to update them. 如果需要进行异步工作,则必须首先使用toArray()拉取现有条目,然后在内存中对其进行修改,完成后,请使用bulkPut()更新它们。

In your case, you are processing a queue. 就您而言,您正在处理队列。 Seems to involve other async operations per queue item. 似乎涉及每个队列项目的其他异步操作。

I'd suggest you to instead, have a separate function that only processes the first item in queue, when process succeeds, remove the item. 我建议您改为使用一个单独的函数,该函数仅处理队列中的第一项,当处理成功时,将其删除。 Then call that function asynchronicallt until the queue is empty. 然后异步调用该函数,直到队列为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM