【发布时间】:2021-09-03 13:06:37
【问题描述】:
我想使用webapi 实现后台同步,对于特定的后台任务,如同步、更新检查和其他事情,我需要几个不同的实现。 我的想法是创建一个类 BackgroundManager,我可以在其中实现 WebApi 并使我的实现继承自它。
使用这段代码,我可以创建一个 Update 类的实例,该实例从 BackgroundManager 继承 subscribe、unsubscribe 和 getSubscriptions 。 问题是我无法得到一个已解决的承诺,而且我不明白为什么。
class BackgroundManager {
async subscribe() {
const registration = await navigator.serviceWorker.ready;
const type = this.type;
try {
await registration.periodicSync.register(type, {
minInterval: this.interval,
});
} catch {
console.log('Periodic Sync could not be registered!');
}
return 'subscribed'
}
async unsubscribe() {
const type = this.type;
const registration = await navigator.serviceWorker.ready;
if ('periodicSync' in registration) {
await registration.periodicSync.unregister(type);
}
return 'unsubscribed'
}
async getSubscriptions() {
const registration = await navigator.serviceWorker.ready;
if ('periodicSync' in registration) {
const tags = await registration.periodicSync.getTags();
return tags;
}
}
}
class Update extends BackgroundManager{
constructor(type) {
super();
this.type = type;
this.interval = 24 * 60 * 60 * 1000;
}
update() {
this.id = 'updating';
}
}
【问题讨论】:
-
我不完全确定这个想法是否是我想要做的最好的解决方案,所以如果有人有更好的解决方案,这也是受欢迎的。我之所以选择这个,是因为我认为如果我的后台服务增长,我将能够轻松实现它的新实例。
标签: javascript progressive-web-apps webapi