@steve31415/baselib 3.48.0 → 3.48.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,12 @@
1
1
  export declare const STABLE_MS: number;
2
2
  export declare const RETRY_MS: number;
3
+ /**
4
+ * How long one renewal keeps the extension busy. Renewals come every ~10 s from
5
+ * an open overlay, but Chrome's intensive throttling slows a page's chained
6
+ * timers to one wake-up a minute once its tab has been hidden 5 minutes, so the
7
+ * lease must outlast a 60 s gap with room to spare (an overlay left open in a
8
+ * background tab must still hold off a reload).
9
+ */
3
10
  export declare const BUSY_LEASE_MS: number;
4
11
  export declare const CODE_MANIFEST_FILE = "mac-code.json";
5
12
  /** chrome.storage.session: { code, since } — the shipped code first seen verified on disk. */
@@ -10,6 +17,16 @@ export declare const ATTEMPT_KEY = "selfUpdateAttempt";
10
17
  export declare const LOADED_KEY = "selfUpdateLoaded";
11
18
  /** chrome.storage.session: epoch ms until which the extension is busy (an open overlay renews it). */
12
19
  export declare const BUSY_LEASE_KEY = "selfUpdateBusyUntil";
20
+ /** chrome.storage.session: epoch ms of the previous check (to notice the machine just woke). */
21
+ export declare const LAST_CHECK_KEY = "selfUpdateLastCheck";
22
+ /**
23
+ * A check that comes more than this long after the previous one follows a
24
+ * sleep (checks run on a 1-minute alarm): it only waits. Chrome fires the
25
+ * overdue alarm the moment the machine wakes, before a page's lease-renewal
26
+ * timer has had its turn, so a lease that lapsed during the sleep must not
27
+ * count as "not busy" yet.
28
+ */
29
+ export declare const WOKE_GAP_MS: number;
13
30
  interface Seen {
14
31
  code: string;
15
32
  since: number;
@@ -58,10 +75,12 @@ export declare function decideUpdate(input: {
58
75
  /**
59
76
  * Check and reload when the rules allow. `shipped`: the code the server last
60
77
  * said it deployed (null when unknown). `busy`: the extension's own work in
61
- * flight; the busy lease and open extension pages are checked here too.
78
+ * flight — pass a function, so it is read at the moment of the reload rather
79
+ * than before the disk checks; the busy lease and open extension pages are
80
+ * checked here too. The first check after a sleep only waits (WOKE_GAP_MS).
62
81
  */
63
82
  export declare function maybeSelfUpdate(options: {
64
- busy: boolean;
83
+ busy: boolean | (() => boolean);
65
84
  shipped: string | null | undefined;
66
85
  log?: (message: string) => void;
67
86
  }): Promise<UpdateAction | 'busy'>;
@@ -18,7 +18,14 @@
18
18
  // test in each repo.
19
19
  export const STABLE_MS = 60 * 1000;
20
20
  export const RETRY_MS = 6 * 60 * 60 * 1000;
21
- export const BUSY_LEASE_MS = 30 * 1000;
21
+ /**
22
+ * How long one renewal keeps the extension busy. Renewals come every ~10 s from
23
+ * an open overlay, but Chrome's intensive throttling slows a page's chained
24
+ * timers to one wake-up a minute once its tab has been hidden 5 minutes, so the
25
+ * lease must outlast a 60 s gap with room to spare (an overlay left open in a
26
+ * background tab must still hold off a reload).
27
+ */
28
+ export const BUSY_LEASE_MS = 150 * 1000;
22
29
  export const CODE_MANIFEST_FILE = 'mac-code.json';
23
30
  /** chrome.storage.session: { code, since } — the shipped code first seen verified on disk. */
24
31
  export const SEEN_KEY = 'selfUpdateSeen';
@@ -28,6 +35,16 @@ export const ATTEMPT_KEY = 'selfUpdateAttempt';
28
35
  export const LOADED_KEY = 'selfUpdateLoaded';
29
36
  /** chrome.storage.session: epoch ms until which the extension is busy (an open overlay renews it). */
30
37
  export const BUSY_LEASE_KEY = 'selfUpdateBusyUntil';
38
+ /** chrome.storage.session: epoch ms of the previous check (to notice the machine just woke). */
39
+ export const LAST_CHECK_KEY = 'selfUpdateLastCheck';
40
+ /**
41
+ * A check that comes more than this long after the previous one follows a
42
+ * sleep (checks run on a 1-minute alarm): it only waits. Chrome fires the
43
+ * overdue alarm the moment the machine wakes, before a page's lease-renewal
44
+ * timer has had its turn, so a lease that lapsed during the sleep must not
45
+ * count as "not busy" yet.
46
+ */
47
+ export const WOKE_GAP_MS = 2 * 60 * 1000;
31
48
  function hex(bytes) {
32
49
  return Array.from(new Uint8Array(bytes), (b) => b.toString(16).padStart(2, '0')).join('');
33
50
  }
@@ -142,9 +159,15 @@ export function decideUpdate(input) {
142
159
  /**
143
160
  * Check and reload when the rules allow. `shipped`: the code the server last
144
161
  * said it deployed (null when unknown). `busy`: the extension's own work in
145
- * flight; the busy lease and open extension pages are checked here too.
162
+ * flight — pass a function, so it is read at the moment of the reload rather
163
+ * than before the disk checks; the busy lease and open extension pages are
164
+ * checked here too. The first check after a sleep only waits (WOKE_GAP_MS).
146
165
  */
147
166
  export async function maybeSelfUpdate(options) {
167
+ const { [LAST_CHECK_KEY]: lastCheck } = await chrome.storage.session.get(LAST_CHECK_KEY);
168
+ const started = Date.now();
169
+ await chrome.storage.session.set({ [LAST_CHECK_KEY]: started });
170
+ const justWoke = typeof lastCheck === 'number' && started - lastCheck > WOKE_GAP_MS;
148
171
  const loaded = await loadedCode();
149
172
  const disk = await diskCode();
150
173
  const { [SEEN_KEY]: seen } = await chrome.storage.session.get(SEEN_KEY);
@@ -162,7 +185,10 @@ export async function maybeSelfUpdate(options) {
162
185
  if (decision.action !== 'reload' || !decision.seen)
163
186
  return decision.action;
164
187
  const target = decision.seen.code;
165
- if (options.busy || (await busyLeaseActive(now)) || (await extensionPageOpen()))
188
+ if (justWoke)
189
+ return 'wait';
190
+ const busy = typeof options.busy === 'function' ? options.busy() : options.busy;
191
+ if (busy || (await busyLeaseActive(Date.now())) || (await extensionPageOpen()))
166
192
  return 'busy';
167
193
  options.log?.(`code ${target} is deployed and on disk (running ${loaded}): reloading.`);
168
194
  await chrome.storage.local.set({ [ATTEMPT_KEY]: { code: target, at: now } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steve31415/baselib",
3
- "version": "3.48.0",
3
+ "version": "3.48.2",
4
4
  "description": "Plasticine new-world shared platform library: logging, auth, service-to-service auth, db, HTTP, sync, app updates",
5
5
  "type": "module",
6
6
  "license": "MIT",