@peers-app/peers-ui 0.33.11 → 0.33.13

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.
@@ -42,6 +42,8 @@ const react_1 = __importStar(require("react"));
42
42
  const loading_indicator_1 = require("../components/loading-indicator");
43
43
  const globals_1 = require("../globals");
44
44
  const hooks_1 = require("../hooks");
45
+ const package_waiting_1 = require("../screens/packages/package-waiting");
46
+ const packages_ipc_1 = require("../screens/packages/packages-ipc");
45
47
  require("../ui-defaults");
46
48
  const bundle_files_1 = require("./bundle-files");
47
49
  const find_route_1 = require("./find-route");
@@ -116,9 +118,16 @@ const UIRouter = (props) => {
116
118
  if (!matchingRoute) {
117
119
  return undefined;
118
120
  }
119
- return (0, jsx_runtime_1.jsx)(UILoader, { peersUIId: matchingRoute.peersUIId, props: props.props });
121
+ return ((0, jsx_runtime_1.jsx)(UILoader, { peersUIId: matchingRoute.peersUIId, props: props.props, screen: context.uiCategory === "screen" }));
120
122
  };
121
123
  exports.UIRouter = UIRouter;
124
+ /**
125
+ * Render one registered UI by id, loading its package's UI bundle first. Packages embed each
126
+ * other's UIs through it (`window.UILoader`).
127
+ *
128
+ * @param args.screen - It fills a whole screen: a waiting package then shows the screen that says
129
+ * why, instead of the line an embedded part shows.
130
+ */
122
131
  const UILoader = (args) => {
123
132
  if (!args.props) {
124
133
  args.props = {};
@@ -128,24 +137,54 @@ const UILoader = (args) => {
128
137
  props.__lastPackageReload = lastReloadTime;
129
138
  // Keep the same loader ancestor after the bundle enters the cache. Replacing it
130
139
  // on a path change would unmount the screen before its navigation guard can run.
131
- return (0, jsx_runtime_1.jsx)(UIAsyncLoader, { peersUIId: peersUIId, props: props }, peersUIId);
140
+ return (0, jsx_runtime_1.jsx)(UIAsyncLoader, { peersUIId: peersUIId, props: props, screen: args.screen }, peersUIId);
132
141
  };
133
- const uiLoadingPromises = {};
134
- function loadUIBundle(pkg, forceRefresh) {
135
- let importPromise = uiLoadingPromises[pkg.packageId];
136
- if (!importPromise || forceRefresh) {
137
- const sTime = Date.now();
138
- console.log(`loading ui bundle for ${pkg.name}`);
139
- importPromise = (async () => {
140
- const selected = await (0, runnable_version_1.resolveRunnableVersion)(pkg);
141
- const uiBundleFileId = selected?.pv.uiBundleFileId;
142
- if (!selected || !uiBundleFileId) {
143
- return;
144
- }
145
- const bundleCode = await (0, bundle_files_1.readBundleText)(selected.dataContext.dataContextId, uiBundleFileId);
142
+ /** Each package's latest UI bundle load, by package id. */
143
+ const uiLoads = {};
144
+ /**
145
+ * Read a package's UI bundle for the version this device runs, checked against that version in a
146
+ * group, without running any of it.
147
+ *
148
+ * @param servingPackageVersionId - The version that serves while the selected one waits, if one
149
+ * does: its UI is read instead.
150
+ */
151
+ async function readUIBundle(pkg, servingPackageVersionId) {
152
+ const selected = await (0, runnable_version_1.resolveRunnableVersion)(pkg, servingPackageVersionId);
153
+ const uiBundleFileId = selected?.pv.uiBundleFileId;
154
+ if (!selected || !uiBundleFileId)
155
+ return undefined;
156
+ const bundleCode = await (0, bundle_files_1.readBundleText)(selected.dataContext.dataContextId, uiBundleFileId);
157
+ if (bundleCode && selected.dataContext.groupId) {
158
+ (0, peers_sdk_1.assertPackageBundleIntegrity)(selected.pv, "ui", bundleCode);
159
+ }
160
+ return bundleCode || undefined;
161
+ }
162
+ /**
163
+ * Run a package's UI bundle once per version, registering its UIs. A load for another version, or
164
+ * one forced, replaces it; a load that's been replaced before its bundle arrives doesn't run it.
165
+ *
166
+ * @param opts.force - Load again even when this version's bundle already loaded.
167
+ * @param opts.read - A read of this version's bundle already under way (one started before the
168
+ * package was known not to be waiting); otherwise the bundle is read now.
169
+ * @param opts.servingPackageVersionId - The version that serves while the selected one waits, if
170
+ * one does; otherwise the selected version's bundle loads.
171
+ */
172
+ function loadUIBundle(pkg, opts) {
173
+ const serving = opts.servingPackageVersionId ?? "";
174
+ const current = uiLoads[pkg.packageId];
175
+ if (current && current.serving === serving && !opts.force)
176
+ return current.promise;
177
+ const sTime = Date.now();
178
+ console.log(`loading ui bundle for ${pkg.name}`);
179
+ const load = { serving, promise: Promise.resolve(), done: false };
180
+ load.promise = (async () => {
181
+ try {
182
+ const bundleCode = await (opts.read ?? readUIBundle(pkg, opts.servingPackageVersionId));
183
+ // A newer load (another version, or a reload) took this one's place: settle with it.
184
+ const latest = uiLoads[pkg.packageId];
185
+ if (latest !== load)
186
+ return latest.promise;
146
187
  if (bundleCode) {
147
- if (selected.dataContext.groupId)
148
- (0, peers_sdk_1.assertPackageBundleIntegrity)(selected.pv, "ui", bundleCode);
149
188
  const exportUIs = (peerUIs) => {
150
189
  peerUIs?.uis?.forEach((ui) => {
151
190
  peersUIs[ui.peersUIId] = ui;
@@ -155,35 +194,78 @@ function loadUIBundle(pkg, forceRefresh) {
155
194
  await bundleFunction(exportUIs);
156
195
  }
157
196
  console.log(`finished loading ui bundle for ${pkg.name}: ${(Date.now() - sTime).toFixed(0)}ms, ${bundleCode ? (bundleCode.length / 1000).toFixed(0) : "0"} KB`);
158
- })().catch((err) => {
197
+ }
198
+ catch (err) {
159
199
  console.error(`Error while loading ui module for ${pkg.name}`, err);
160
200
  throw err;
161
- });
162
- uiLoadingPromises[pkg.packageId] = importPromise;
163
- }
164
- return importPromise;
201
+ }
202
+ finally {
203
+ load.done = true;
204
+ }
205
+ })();
206
+ uiLoads[pkg.packageId] = load;
207
+ return load.promise;
165
208
  }
166
209
  const UIAsyncLoader = (props) => {
167
210
  const { peersUIId } = props;
168
211
  const peersUI = peersUIs[peersUIId];
169
- const [loaded, setLoaded] = react_1.default.useState(false);
212
+ // The version whose UI this screen last saw load ("" for the selected one).
213
+ const [loaded, setLoaded] = (0, react_1.useState)();
170
214
  const [loadError, setLoadError] = react_1.default.useState(null);
171
215
  const allRoutes = _window.getPeersUIRoutes();
172
216
  const uiPackageId = allRoutes.find((r) => r.peersUIId === peersUIId)?.packageId;
173
217
  (0, hooks_1.useObservable)(routes_loader_1.allPackages);
174
218
  const pkg = (0, routes_loader_1.allPackages)().find((p) => p.packageId === uiPackageId);
219
+ // A screen of a package that waits for a contract it requires shows why, and runs none of the
220
+ // package's UI code. Its bundle is read alongside the check of whether it waits, and runs only
221
+ // once it's known not to. (A package that ran before it stopped keeps the UI it loaded, and a
222
+ // reload of it, after a change to its row or selection, can run that bundle again meanwhile;
223
+ // no screen shows it until the package starts.) While an update waits, the version that still
224
+ // serves is the one whose UI runs.
225
+ const gate = (0, packages_ipc_1.usePackageScreenGate)(pkg?.packageId);
226
+ const waiting = !!gate.missing;
227
+ const serving = gate.servingPackageVersionId;
228
+ const read = (0, react_1.useRef)();
175
229
  (0, react_1.useEffect)(() => {
176
- if (!pkg || peersUIs[peersUIId]?.content)
230
+ // A read made before the package was known to wait is dropped: by the time it starts, its
231
+ // selected version or its synced bundle may have changed.
232
+ if (waiting)
233
+ read.current = undefined;
234
+ if (!pkg || peersUIs[peersUIId]?.content || pkg.packageId in uiLoads || waiting)
177
235
  return;
236
+ if (read.current?.packageId === pkg.packageId && read.current.serving === serving)
237
+ return;
238
+ const bundle = readUIBundle(pkg, serving);
239
+ // A failed read is reported when the bundle is loaded, if it ever is.
240
+ bundle.catch(() => undefined);
241
+ read.current = { packageId: pkg.packageId, serving, bundle };
242
+ }, [peersUIId, pkg, waiting, serving]);
243
+ (0, react_1.useEffect)(() => {
244
+ if (!pkg || !gate.ready || waiting)
245
+ return;
246
+ // UI on hand is kept when it's this version's: loaded for it, or built in.
247
+ const current = uiLoads[pkg.packageId];
248
+ if (peersUIs[peersUIId]?.content && (current?.serving ?? "") === (serving ?? "")) {
249
+ if (!current || current.done)
250
+ return;
251
+ }
178
252
  const pkgRef = pkg;
253
+ const started = read.current?.packageId === pkgRef.packageId && read.current.serving === serving
254
+ ? read.current.bundle
255
+ : undefined;
256
+ // Used once: a later load of the same version reads it afresh.
257
+ read.current = undefined;
179
258
  setLoadError(null);
180
- loadUIBundle(pkgRef)
181
- .then(() => setLoaded(true))
259
+ loadUIBundle(pkgRef, { read: started, servingPackageVersionId: serving })
260
+ .then(() => setLoaded(serving ?? ""))
182
261
  .catch((error) => {
183
262
  console.error(`Failed to load UI bundle for package ${pkgRef.name}:`, error);
184
263
  setLoadError(error);
185
264
  });
186
- }, [peersUIId, pkg?.packageId, pkg?.name, pkg]);
265
+ }, [peersUIId, pkg?.packageId, pkg?.name, pkg, gate.ready, waiting, serving]);
266
+ if (pkg && gate.missing) {
267
+ return props.screen ? ((0, jsx_runtime_1.jsx)(package_waiting_1.PackageWaitingScreen, { packageId: pkg.packageId, missing: gate.missing })) : ((0, jsx_runtime_1.jsx)(package_waiting_1.PackageWaitingNotice, { packageId: pkg.packageId, missing: gate.missing }));
268
+ }
187
269
  if (!peersUI?.content && !pkg) {
188
270
  return (0, jsx_runtime_1.jsxs)("div", { children: ["Could not find package that owns peersUIId ", peersUIId] });
189
271
  }
@@ -205,10 +287,10 @@ const UIAsyncLoader = (props) => {
205
287
  maxHeight: "200px",
206
288
  }, children: [(0, jsx_runtime_1.jsx)("strong", { children: "Error:" }), " ", loadError.message || "Unknown error", loadError.stack && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("strong", { children: "Stack Trace:" }), (0, jsx_runtime_1.jsx)("br", {}), loadError.stack] }))] })] }), (0, jsx_runtime_1.jsx)("button", { type: "button", onClick: () => {
207
289
  setLoadError(null);
208
- setLoaded(false);
290
+ setLoaded(undefined);
209
291
  if (pkg) {
210
- loadUIBundle(pkg, true)
211
- .then(() => setLoaded(true))
292
+ loadUIBundle(pkg, { force: true, servingPackageVersionId: serving })
293
+ .then(() => setLoaded(serving ?? ""))
212
294
  .catch(setLoadError);
213
295
  }
214
296
  }, style: {
@@ -221,7 +303,7 @@ const UIAsyncLoader = (props) => {
221
303
  cursor: "pointer",
222
304
  }, children: "Retry Loading" })] }));
223
305
  }
224
- if (!peersUI?.content && !loaded) {
306
+ if (!peersUI?.content && loaded === undefined) {
225
307
  return (0, jsx_runtime_1.jsx)(loading_indicator_1.LoadingIndicator, {});
226
308
  }
227
309
  if (peersUI?.content) {
@@ -320,7 +402,10 @@ async function registerInternalPeersUI(props) {
320
402
  const reloadPackage = (0, peers_sdk_1.debounceByArgs)(async (packageId) => {
321
403
  const pkg = await (0, peers_sdk_1.Packages)().get(packageId);
322
404
  if (pkg) {
323
- await loadUIBundle(pkg, true);
405
+ // The same version as before: the selection's, or the one serving while an update waits. A
406
+ // screen that's open follows a change of the version that serves.
407
+ const servingPackageVersionId = uiLoads[packageId]?.serving || undefined;
408
+ await loadUIBundle(pkg, { force: true, servingPackageVersionId });
324
409
  console.log(`reloaded UI bundle for ${pkg.name}`);
325
410
  (0, globals_1.packageReloaded)(Date.now());
326
411
  }
@@ -341,20 +426,19 @@ function subscribeToPrefs(packageId) {
341
426
  });
342
427
  }
343
428
  (0, peers_sdk_1.getUserContext)().then((_userContext) => {
344
- for (const packageId of Object.keys(uiLoadingPromises)) {
429
+ for (const packageId of Object.keys(uiLoads)) {
345
430
  subscribeToPrefs(packageId);
346
431
  }
347
432
  (0, peers_sdk_1.Packages)().dataChanged.subscribe(async (evt) => {
348
433
  const pkg = evt.dataObject;
349
- const loadingPromise = uiLoadingPromises[pkg.packageId];
350
- if (!loadingPromise)
434
+ if (!(pkg.packageId in uiLoads))
351
435
  return;
352
436
  subscribeToPrefs(pkg.packageId);
353
437
  reloadPackage(pkg.packageId);
354
438
  });
355
439
  // A promotion changes only the selected version's row, and can let a refused screen load.
356
440
  (0, runnable_version_1.onSelectedPackageVersionChanged)((packageId) => {
357
- if (packageId in uiLoadingPromises)
441
+ if (packageId in uiLoads)
358
442
  reloadPackage(packageId);
359
443
  });
360
444
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peers-app/peers-ui",
3
- "version": "0.33.11",
3
+ "version": "0.33.13",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/peers-app/peers-ui.git"
@@ -36,7 +36,7 @@
36
36
  "lint:fix": "biome check --write ."
37
37
  },
38
38
  "peerDependencies": {
39
- "@peers-app/peers-sdk": "^0.33.11",
39
+ "@peers-app/peers-sdk": "^0.33.13",
40
40
  "bootstrap": "^5.3.3",
41
41
  "react": "^18.0.0",
42
42
  "react-dom": "^18.0.0"
@@ -47,7 +47,7 @@
47
47
  "@babel/preset-react": "^7.24.1",
48
48
  "@babel/preset-typescript": "^7.27.1",
49
49
  "@biomejs/biome": "2.3.11",
50
- "@peers-app/peers-sdk": "0.33.11",
50
+ "@peers-app/peers-sdk": "0.33.13",
51
51
  "@testing-library/dom": "^10.4.0",
52
52
  "@testing-library/jest-dom": "^6.6.3",
53
53
  "@testing-library/react": "^16.3.0",