@steve31415/baselib 3.48.1 → 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.
@@ -17,6 +17,16 @@ export declare const ATTEMPT_KEY = "selfUpdateAttempt";
17
17
  export declare const LOADED_KEY = "selfUpdateLoaded";
18
18
  /** chrome.storage.session: epoch ms until which the extension is busy (an open overlay renews it). */
19
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;
20
30
  interface Seen {
21
31
  code: string;
22
32
  since: number;
@@ -65,10 +75,12 @@ export declare function decideUpdate(input: {
65
75
  /**
66
76
  * Check and reload when the rules allow. `shipped`: the code the server last
67
77
  * said it deployed (null when unknown). `busy`: the extension's own work in
68
- * 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).
69
81
  */
70
82
  export declare function maybeSelfUpdate(options: {
71
- busy: boolean;
83
+ busy: boolean | (() => boolean);
72
84
  shipped: string | null | undefined;
73
85
  log?: (message: string) => void;
74
86
  }): Promise<UpdateAction | 'busy'>;
@@ -35,6 +35,16 @@ export const ATTEMPT_KEY = 'selfUpdateAttempt';
35
35
  export const LOADED_KEY = 'selfUpdateLoaded';
36
36
  /** chrome.storage.session: epoch ms until which the extension is busy (an open overlay renews it). */
37
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;
38
48
  function hex(bytes) {
39
49
  return Array.from(new Uint8Array(bytes), (b) => b.toString(16).padStart(2, '0')).join('');
40
50
  }
@@ -149,9 +159,15 @@ export function decideUpdate(input) {
149
159
  /**
150
160
  * Check and reload when the rules allow. `shipped`: the code the server last
151
161
  * said it deployed (null when unknown). `busy`: the extension's own work in
152
- * 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).
153
165
  */
154
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;
155
171
  const loaded = await loadedCode();
156
172
  const disk = await diskCode();
157
173
  const { [SEEN_KEY]: seen } = await chrome.storage.session.get(SEEN_KEY);
@@ -169,7 +185,10 @@ export async function maybeSelfUpdate(options) {
169
185
  if (decision.action !== 'reload' || !decision.seen)
170
186
  return decision.action;
171
187
  const target = decision.seen.code;
172
- 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()))
173
192
  return 'busy';
174
193
  options.log?.(`code ${target} is deployed and on disk (running ${loaded}): reloading.`);
175
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.1",
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",