Service Worker - 无论如何要知道 Service Worker 已经完成了所有后台 - 同步标签? [英] Service Worker - Is there anyway to know service worker has finished all background - sync tags?

查看:44
本文介绍了Service Worker - 无论如何要知道 Service Worker 已经完成了所有后台 - 同步标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Service Worker - 无论如何知道 Service Worker 已经完成所有后台 - 同步吗?

Service Worker - Is there anyway to know service worker has finished all background - sync?

我已经为 Service Worker 编写了一组不同的同步任务,但是我想知道有没有什么方法可以知道我的软件从离线状态上线时已经完成了所有同步更新?

I have written a set of different sync tasks for service worker, but am wondering is there any way to know my SW has finished all its sync updates when it comes online from offline state?

这样我就可以向用户发送一个通知,说您的所有更新都已完成.在下面的场景中,我只想知道当所有同步场景都完成时,我想使用 sendMessage 发出一条消息.

So that I can send a single notification to user saying that all your updates are finished. In the below scenario, I just wanted to know when all the sync scenarios are completed, I want to emit a message using sendMessage.

self.addEventListener("sync", function(event) {
console.log("Background Syncing started...");

  if(event.tag === 'case0-sync'){
 

  }else if(event.tag === 'case1-sync'){
   
  }else if(event.tag === 'case2-sync'){
    
  }else if(event.tag === 'case3-sync'){
   
  }else if(event.tag === 'case4-sync'){
    
  }
});

推荐答案

getTags() 方法应该给你你正在寻找的信息.

The getTags() method of the SyncManager interface should give you the info you're looking for.

self.addEventListener('sync', async (event) => {
  // Handle event...

  // Afterwards...
  const tags = await self.registration.sync.getTags();
  if (tags.length === 0) {
    // There are no registered tags.
  }
});

这篇关于Service Worker - 无论如何要知道 Service Worker 已经完成了所有后台 - 同步标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆