All files / src/ts/pkjs asks.ts

100% Statements 22/22
100% Branches 15/15
100% Functions 6/6
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128                                9x                                         292x                                 190x   190x 3x       187x   190x 5x 5x     182x                         26x 1x     25x                           22x   22x   22x                       45x       45x   45x 38x 38x 38x      
/**
 * Who gets to fetch between the watch and the phone, shared by every feature that polls.
 *
 * The watch polls each feature on its own clock, and the phone runs a slow refresh of its own in case
 * the watch stops asking. Left alone the two fetch the same data twice. The watch's poll follows the
 * interval the wearer picked, so it leads, and the phone's slow tick only fetches when nothing asked
 * since the last one. A settings save forces its own refetch, and a watch ask that lands before it
 * runs is folded into it. A feature can also give its asks a gap, so an ask that lands just after a
 * fetch finished resends that reading rather than fetching it again.
 */
 
/**
 * The gap weather and the calendar keep after a finished fetch. The phone fetches as it comes up,
 * and the watch's own ask lands a moment after that round has finished, so without it every launch
 * fetches twice. A minute covers that and is short enough that a real poll never lands in it.
 */
export const ASK_GAP_MS = 60 * 1000;
 
/** Who has asked lately, read and updated in place by a feature's hooks. */
export interface FeatureAsks {
  /** The watch asked, or the phone came up, since the last slow tick. */
  sinceSlowTick: boolean;
  /** A setting was saved and its forced refetch has not run yet. */
  savePending: boolean;
  /** How long after a finished fetch an ask resends rather than fetches, 0 for no gap. */
  gapMs: number;
  /** When the last fetch finished with a reading to resend, or null before the first. */
  fetchedAtMs: number | null;
}
 
/**
 * Makes the ask state for one feature, with nothing asked yet.
 *
 * @param gapMs How long after a finished fetch an ask resends rather than fetches. Left out, there is no gap.
 * @return A fresh ask state.
 */
export function createAsks(gapMs = 0): FeatureAsks {
  return { sinceSlowTick: false, savePending: false, gapMs, fetchedAtMs: null };
}
 
/**
 * Decides whether a watch ask, or the phone coming up, should start a fetch.
 *
 * An ask that lands while a save's refetch is pending is folded into it, or the two would race and
 * fetch twice. An ask that lands inside the gap after a finished fetch has `resend` send that
 * reading again, since fetching it again would bring the same reading. Either way the ask counts
 * towards skipping the next slow tick.
 *
 * @param asks The ask state to read and update in place.
 * @param nowMs The time now, in ms since the epoch. Only read when the asks have a gap.
 * @param resend Sends the last reading again, for an ask inside the gap.
 * @return True when the ask should start a fetch.
 */
export function askShouldFetch(asks: FeatureAsks, nowMs = 0, resend?: () => void): boolean {
  asks.sinceSlowTick = true;
 
  if (asks.savePending) {
    return false;
  }
 
  // a clock that stepped back puts now before the fetch, which reads as outside the gap
  const sinceFetch = asks.fetchedAtMs === null ? -1 : nowMs - asks.fetchedAtMs;
 
  if (resend && sinceFetch >= 0 && sinceFetch < asks.gapMs) {
    resend();
    return false;
  }
 
  return true;
}
 
/**
 * Notes that a fetch finished with a reading, which opens the gap an ask resends inside. A fetch that
 * finishes while a save's refetch waits opens none.
 *
 * @param asks The ask state to update in place.
 * @param nowMs The time the fetch finished, in ms since the epoch.
 */
export function fetchFinished(asks: FeatureAsks, nowMs: number): void {
  // a round from before a save that lands while its refetch waits has the old reading, so it opens
  // no gap
  if (asks.savePending) {
    return;
  }
 
  asks.fetchedAtMs = nowMs;
}
 
/**
 * Decides whether a slow tick should start a fetch.
 *
 * A tick that follows one of the watch's asks would only fetch the same data again. The tick
 * fetches when nothing asked since the last one, such as a watch out of range. A save's refetch
 * that is still waiting to run owns the next fetch, so a tick that lands in that moment waits.
 *
 * @param asks The ask state to read and update in place.
 * @return True when the tick should start a fetch.
 */
export function slowTickShouldFetch(asks: FeatureAsks): boolean {
  const due = !asks.sinceSlowTick && !asks.savePending;
 
  asks.sinceSlowTick = false;
 
  return due;
}
 
/**
 * Runs a forced refetch once the settings page has had time to save, holding back watch asks until then.
 * It also closes the gap after the last fetch, since that reading is from before the save.
 *
 * @param asks The ask state to update in place.
 * @param delayMs How long to wait, normally the context's refetchDelayMs.
 * @param refetch The forced fetch to run.
 */
export function refetchAfterSave(asks: FeatureAsks, delayMs: number, refetch: () => void): void {
  asks.savePending = true;
 
  // the save makes the last reading out of date, so the gap closes and no ask resends it. a refetch
  // that brings a reading opens it again, and one that fails leaves the next ask to fetch
  asks.fetchedAtMs = null;
 
  setTimeout(() => {
    asks.savePending = false;
    asks.sinceSlowTick = true;
    refetch();
  }, delayMs);
}