Skip to content

Conversation

JackOlsen
Copy link

@JackOlsen JackOlsen commented Nov 28, 2018

Use RxJS timer creation operator instead of interval so that first check for updates happens shortly after app startup. Change recurring check interval to 1 hour, rather than seemingly arbitrary 21.6 seconds.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Use RxJS timer creation operator instead of interval so that first check for updates happens shortly after app startup. Change recurring check interval to 1 hour, rather than seemingly arbitrary 21.6 seconds.
@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

@JackOlsen
Copy link
Author

JackOlsen commented Nov 28, 2018 via email

@googlebot
Copy link

CLAs look good, thanks!

Copy link
Member

@gkalpak gkalpak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this is our example for setting up update checks??? This will prevent the app from stabilizing (due to the interval/timer) and the SW will never be registered (since it waits for the app to stabilize first) 🤦‍♂️

Other than that, I think the interval is preferable. There is no need to check when the app loads, since the SW will check on each navigation anyway.

But we do need to fix the issue with preventing the SW from being registered 😁

The proper way to do this is to first wait for the app to stabilize and then start polling for updates:

import { ApplicationRef } from '@angular/core';
import { concat, interval } from 'rxjs';
import { first } from 'rxjs/operators';
...
constructor(appRef: ApplicationRef) {
  concat(appRef.isStable.pipe(first(v => v)), interval(6 * 60 * 60 * 1000)).
    subscribe(() => updates.checkForUpdates());
}

The code can be formatted in a way that is more understandable for beginners. E.g.:

constructor(appRef: ApplicationRef) {
  const appIsStable$ = appRef.isStable.pipe(first(isStable => isStable === true));
  const everySixHours$ = interval(6 * 60 * 60 * 1000);
  const everySixHoursAfterAppIsStable$ = concat(appIsStable$, everySixHours$);

  everySixHoursAfterAppIsStable$.subscribe(() => updates.checkForUpdates());
}

@JackOlsen, would be be interested in making the necessary changes (in both the example code and guide)? 🙏

export class CheckForUpdateService {

constructor(updates: SwUpdate) {
interval(6 * 60 * 60).subscribe(() => updates.checkForUpdate());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am 99% sure this was supposed to be 6 hours (6 * 60 * 60 * 1000) 😁


constructor(updates: SwUpdate) {
interval(6 * 60 * 60).subscribe(() => updates.checkForUpdate());
timer(1000, 1000 * 60 * 60).subscribe(() => updates.checkForUpdate());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This initial delay is still arbitrary and does not guarantee that the app has loaded (although hopefully is has).

Copy link
Author

@JackOlsen JackOlsen Nov 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't aware of ApplicationRef.isStable, but that makes sense as a hook for initiating polling for updates.

Regarding this:

There is no need to check when the app loads, since the SW will check on each navigation anyway.

I think I'm following now. The SW will check for an update every time the page is loaded, and if it finds an update, an event will be emitted on SwUpdate.available, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right! Basically, a check for update is triggered in the following cases:

  • When the SW is instantiated by the browser.
  • On every navigation request (i.e. when navigating to your index.html but when fetching a JS script).
  • When the user calls SwUpdate#checkForUpdates().

In all those cases, if an update is found, a message is emitted on the SwUpdate#available observable.

@gkalpak gkalpak added comp: docs area: service-worker Issues related to the @angular/service-worker package labels Nov 28, 2018
@ngbot ngbot bot added this to the needsTriage milestone Nov 28, 2018
@gkalpak gkalpak added state: WIP target: patch This PR is targeted for the next patch release labels Nov 28, 2018
@jenniferfell jenniferfell added comp: examples type: bug/fix freq3: high state: community Someone from the Angular community is working on this issue or submitted this PR effort2: days risk: medium and removed subtype: docs-bestpractice labels Nov 29, 2018
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Nov 29, 2018
@jenniferfell
Copy link
Contributor

I was just reviewing this as part of doc PR triage (thus the label additions). Two things:

  • Please update the commit message to begin with "docs:" per our conventions here: https://github.com/angular/angular/blob/master/CONTRIBUTING.md#commit

  • I don't find any mention of "interval" in the guides, so this change doesn't seem to invalidate any existing guide content, but that fact makes me ask: Does something need to be added to the guide? Any chance you'd be willing to draft an update as part of this PR? :-) (If not, we can open an issue to add guide content later, as long as the guides aren't wrong today.)

Thanks!

@gkalpak
Copy link
Member

gkalpak commented Nov 30, 2018

@gkalpak gkalpak mentioned this pull request Jan 9, 2019
6 tasks
@gkalpak
Copy link
Member

gkalpak commented Jan 9, 2019

Superceded by #28020.

@gkalpak gkalpak closed this Jan 9, 2019
gkalpak added a commit to gkalpak/angular that referenced this pull request Jan 10, 2019
Poll for updates in a way that does not prevent the SW from being
registered. Discussed in angular#27332 (review).
AndrewKushnir pushed a commit that referenced this pull request Jan 11, 2019
…28020)

Poll for updates in a way that does not prevent the SW from being
registered. Discussed in #27332 (review).

PR Close #28020
AndrewKushnir pushed a commit that referenced this pull request Jan 11, 2019
…28020)

Poll for updates in a way that does not prevent the SW from being
registered. Discussed in #27332 (review).

PR Close #28020
AndrewKushnir pushed a commit that referenced this pull request Jan 16, 2019
…28020)

Poll for updates in a way that does not prevent the SW from being
registered. Discussed in #27332 (review).

PR Close #28020
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: service-worker Issues related to the @angular/service-worker package cla: yes effort2: days freq3: high risk: medium state: community Someone from the Angular community is working on this issue or submitted this PR state: WIP target: patch This PR is targeted for the next patch release type: bug/fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants