【问题标题】:PeriodicSyc: How to resolve the pending promise?PeriodicSyc:如何解决未决的承诺?
【发布时间】:2021-09-03 13:06:37
【问题描述】:

我想使用webapi 实现后台同步,对于特定的后台任务,如同步、更新检查和其他事情,我需要几个不同的实现。 我的想法是创建一个类 BackgroundManager,我可以在其中实现 WebApi 并使我的实现继承自它。

使用这段代码,我可以创建一个 Update 类的实例,该实例从 BackgroundManager 继承 subscribeunsubscribegetSubscriptions 。 问题是我无法得到一个已解决的承诺,而且我不明白为什么。

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


【解决方案1】:

目前在 Chrome 中实现的定期后台同步有一些先决条件,如 this article 所述:

如果不满足其中任何一个条件,这可能解释了您在使用该功能时遇到问题的原因。

【讨论】:

  • 我在这里检查过,这就是代码不起作用的原因。基于此,我将无法使用它,因为我无法“强制”用户安装该应用程序。我将不得不考虑另一种解决方案。
猜你喜欢
  • 2020-03-16
  • 2020-10-22
  • 2014-12-15
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
  • 2018-04-02
相关资源
最近更新 更多