@peers-app/peers-ui 0.33.23 → 0.34.0
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.
- package/dist/command-palette/command-palette.js +4 -2
- package/dist/components/router.d.ts +1 -0
- package/dist/components/router.js +4 -1
- package/dist/host-shell/host-shell-consumer.js +8 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -1
- package/dist/isolated-screens/config.d.ts +51 -0
- package/dist/isolated-screens/config.js +73 -0
- package/dist/isolated-screens/frame-autostart.d.ts +1 -0
- package/dist/isolated-screens/frame-autostart.js +18 -0
- package/dist/isolated-screens/frame-bridge.d.ts +30 -0
- package/dist/isolated-screens/frame-bridge.js +113 -0
- package/dist/isolated-screens/frame-component.d.ts +6 -0
- package/dist/isolated-screens/frame-component.js +20 -0
- package/dist/isolated-screens/frame-document.d.ts +26 -0
- package/dist/isolated-screens/frame-document.js +69 -0
- package/dist/isolated-screens/frame-policy.d.ts +17 -0
- package/dist/isolated-screens/frame-policy.js +230 -0
- package/dist/isolated-screens/frame-protocol.d.ts +185 -0
- package/dist/isolated-screens/frame-protocol.js +139 -0
- package/dist/isolated-screens/frame-pvars.d.ts +33 -0
- package/dist/isolated-screens/frame-pvars.js +152 -0
- package/dist/isolated-screens/frame-realm.d.ts +12 -0
- package/dist/isolated-screens/frame-realm.js +31 -0
- package/dist/isolated-screens/frame-runtime.d.ts +5 -0
- package/dist/isolated-screens/frame-runtime.js +416 -0
- package/dist/isolated-screens/frame-storage.d.ts +43 -0
- package/dist/isolated-screens/frame-storage.js +85 -0
- package/dist/isolated-screens/index.d.ts +3 -0
- package/dist/isolated-screens/index.js +13 -0
- package/dist/isolated-screens/isolated-screen.d.ts +191 -0
- package/dist/isolated-screens/isolated-screen.js +684 -0
- package/dist/isolated-screens/piece-frames.d.ts +27 -0
- package/dist/isolated-screens/piece-frames.js +311 -0
- package/dist/isolated-screens/piece-reach.d.ts +11 -0
- package/dist/isolated-screens/piece-reach.js +56 -0
- package/dist/isolated-screens/port-channel.d.ts +47 -0
- package/dist/isolated-screens/port-channel.js +153 -0
- package/dist/isolated-screens/routes-sandbox.d.ts +8 -0
- package/dist/isolated-screens/routes-sandbox.js +130 -0
- package/dist/isolated-screens/screen-tab-context.d.ts +4 -0
- package/dist/isolated-screens/screen-tab-context.js +6 -0
- package/dist/isolated-screens/test-ports.d.ts +3 -0
- package/dist/isolated-screens/test-ports.js +21 -0
- package/dist/mentions/mentions-client.js +9 -0
- package/dist/notifications/index.js +9 -5
- package/dist/operator-console/console-copy.d.ts +15 -0
- package/dist/operator-console/console-copy.js +97 -0
- package/dist/operator-console/console-header.d.ts +33 -0
- package/dist/operator-console/console-header.js +67 -0
- package/dist/operator-console/console-input.d.ts +20 -0
- package/dist/operator-console/console-input.js +313 -0
- package/dist/operator-console/console-repl.d.ts +7 -0
- package/dist/operator-console/console-repl.js +108 -0
- package/dist/operator-console/console-scope.d.ts +45 -0
- package/dist/operator-console/console-scope.js +174 -0
- package/dist/operator-console/console-screen.d.ts +8 -0
- package/dist/operator-console/console-screen.js +32 -0
- package/dist/operator-console/console-session.d.ts +106 -0
- package/dist/operator-console/console-session.js +567 -0
- package/dist/operator-console/console-test-host.d.ts +21 -0
- package/dist/operator-console/console-test-host.js +114 -0
- package/dist/operator-console/console-theme.d.ts +41 -0
- package/dist/operator-console/console-theme.js +55 -0
- package/dist/operator-console/console-value.d.ts +27 -0
- package/dist/operator-console/console-value.js +291 -0
- package/dist/operator-console/operator-console-state.js +3 -1
- package/dist/operator-console/operator-console.d.ts +8 -21
- package/dist/operator-console/operator-console.js +8 -61
- package/dist/root-layout/app-shell-height.d.ts +24 -3
- package/dist/root-layout/app-shell-height.js +25 -4
- package/dist/root-layout/host-app-menu.d.ts +17 -0
- package/dist/root-layout/host-app-menu.js +22 -0
- package/dist/root-layout/layouts/full-screen-layout.js +7 -1
- package/dist/root-layout/layouts/tabs-with-console-layout.js +1 -1
- package/dist/system-apps/console.app.d.ts +15 -0
- package/dist/system-apps/console.app.js +25 -0
- package/dist/system-apps/index.d.ts +1 -0
- package/dist/system-apps/index.js +6 -1
- package/dist/tabs-layout/tabs-layout.js +13 -20
- package/dist/tabs-layout/tabs-state.js +39 -16
- package/dist/theme.css +66 -0
- package/dist/ui-contracts/piece-height.d.ts +18 -0
- package/dist/ui-contracts/piece-height.js +15 -0
- package/dist/ui-contracts/piece-host.d.ts +24 -1
- package/dist/ui-contracts/piece-host.js +53 -2
- package/dist/ui-contracts/piece-props.d.ts +16 -0
- package/dist/ui-contracts/piece-props.js +39 -0
- package/dist/ui-contracts/piece-resolution.d.ts +5 -0
- package/dist/ui-contracts/piece-resolution.js +38 -2
- package/dist/ui-contracts/piece.d.ts +2 -7
- package/dist/ui-contracts/piece.js +22 -37
- package/dist/ui-contracts/popover-surface.d.ts +14 -0
- package/dist/ui-contracts/popover-surface.js +32 -3
- package/dist/ui-inspection/dom-actions.d.ts +23 -0
- package/dist/ui-inspection/dom-actions.js +169 -0
- package/dist/ui-inspection/dom-inspector.d.ts +20 -0
- package/dist/ui-inspection/dom-inspector.js +184 -0
- package/dist/ui-inspection/index.d.ts +73 -0
- package/dist/ui-inspection/index.js +147 -0
- package/dist/ui-router/find-route.d.ts +6 -0
- package/dist/ui-router/find-route.js +22 -0
- package/dist/ui-router/routes-loader.js +23 -4
- package/dist/ui-router/runnable-version.js +3 -0
- package/dist/ui-router/ui-loader.d.ts +6 -1
- package/dist/ui-router/ui-loader.js +102 -26
- package/package.json +3 -3
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The Console's REPL session: the contracts of one data context as globals, the entries the
|
|
4
|
+
* person runs, and what they print. Module state, so collapsing the panel (which unmounts it)
|
|
5
|
+
* keeps the output, the variables and the group.
|
|
6
|
+
*
|
|
7
|
+
* Built on `@peers-app/peers-sdk/repl`, the module `peers repl` uses, so the Console and the CLI
|
|
8
|
+
* have the same names, helpers and hints. Shared by the Tabs + Console layout's panel and the
|
|
9
|
+
* Console app.
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.consoleScope = exports.consoleReady = exports.consoleDraft = exports.consoleContext = exports.consoleBlocks = void 0;
|
|
13
|
+
exports.consoleHistory = consoleHistory;
|
|
14
|
+
exports.consolePrint = consolePrint;
|
|
15
|
+
exports.clearConsole = clearConsole;
|
|
16
|
+
exports.refreshConsoleContracts = refreshConsoleContracts;
|
|
17
|
+
exports.startConsole = startConsole;
|
|
18
|
+
exports.runInConsole = runInConsole;
|
|
19
|
+
exports.stopWaiting = stopWaiting;
|
|
20
|
+
exports.resetConsoleForTests = resetConsoleForTests;
|
|
21
|
+
const peers_sdk_1 = require("@peers-app/peers-sdk");
|
|
22
|
+
const repl_1 = require("@peers-app/peers-sdk/repl");
|
|
23
|
+
const console_copy_1 = require("./console-copy");
|
|
24
|
+
const console_scope_1 = require("./console-scope");
|
|
25
|
+
/** The output keeps this many blocks; older ones drop off the top. */
|
|
26
|
+
const MAX_BLOCKS = 500;
|
|
27
|
+
/** History keeps this many entries. */
|
|
28
|
+
const MAX_HISTORY = 200;
|
|
29
|
+
/** How the Console is used, after the help every prompt shares. */
|
|
30
|
+
const CONSOLE_HELP_TEXT = `${repl_1.SESSION_HELP_TEXT}
|
|
31
|
+
|
|
32
|
+
At the Console:
|
|
33
|
+
Enter runs; Shift+Enter starts a new line (Enter does too while a bracket is open).
|
|
34
|
+
Tab completes a name; ↑ and ↓ go through what you ran.
|
|
35
|
+
print(value) show a value; console.log does the same
|
|
36
|
+
clear() empty the output (Ctrl+L)`;
|
|
37
|
+
/** The Console's output, oldest first. */
|
|
38
|
+
exports.consoleBlocks = (0, peers_sdk_1.observable)([]);
|
|
39
|
+
/** The data context the Console's calls run in, by name: "personal", or a group's. */
|
|
40
|
+
exports.consoleContext = (0, peers_sdk_1.observable)("personal");
|
|
41
|
+
/** What's typed in the Console's input and not yet run, kept while the panel is closed. */
|
|
42
|
+
exports.consoleDraft = (0, peers_sdk_1.observable)("");
|
|
43
|
+
/** Whether the session has its contracts yet. */
|
|
44
|
+
exports.consoleReady = (0, peers_sdk_1.observable)(false);
|
|
45
|
+
/** The stored history, made when the Console first starts, so other screens don't read it. */
|
|
46
|
+
let historyVar;
|
|
47
|
+
/**
|
|
48
|
+
* The history the Console reads and adds to. A copy, so entries run in quick succession aren't
|
|
49
|
+
* lost when the stored var's echo of an earlier write comes back.
|
|
50
|
+
*/
|
|
51
|
+
let history;
|
|
52
|
+
/** Load what the person ran on this device, once. */
|
|
53
|
+
function loadHistory() {
|
|
54
|
+
historyVar ??= (0, peers_sdk_1.deviceVar)("operatorConsole:history", { defaultValue: [] });
|
|
55
|
+
const stored = historyVar;
|
|
56
|
+
return stored.loadingPromise.then(() => {
|
|
57
|
+
history ??= stored() ?? [];
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/** What the person ran on this device, oldest first; empty until the Console has started. */
|
|
61
|
+
function consoleHistory() {
|
|
62
|
+
return history ?? [];
|
|
63
|
+
}
|
|
64
|
+
/** The names code at the Console sees. */
|
|
65
|
+
exports.consoleScope = new console_scope_1.ConsoleScope();
|
|
66
|
+
let session;
|
|
67
|
+
let sessionTransport;
|
|
68
|
+
let starting;
|
|
69
|
+
let unsubscribeTransport;
|
|
70
|
+
let queue = Promise.resolve();
|
|
71
|
+
const stops = new Map();
|
|
72
|
+
/** The entry running now. */
|
|
73
|
+
let current;
|
|
74
|
+
function add(block) {
|
|
75
|
+
const blocks = [...(0, exports.consoleBlocks)(), block];
|
|
76
|
+
(0, exports.consoleBlocks)(blocks.length > MAX_BLOCKS ? blocks.slice(-MAX_BLOCKS) : blocks);
|
|
77
|
+
return block;
|
|
78
|
+
}
|
|
79
|
+
function update(id, patch) {
|
|
80
|
+
(0, exports.consoleBlocks)((0, exports.consoleBlocks)().map((block) => block.id === id && block.type === "entry" ? { ...block, ...patch } : block));
|
|
81
|
+
}
|
|
82
|
+
function info(text, tone) {
|
|
83
|
+
add({ id: (0, peers_sdk_1.newid)(), type: "info", text, tone });
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Print into an entry above its result, or as a separate block.
|
|
87
|
+
*/
|
|
88
|
+
function printAt(level, values, entryId) {
|
|
89
|
+
const print = { id: (0, peers_sdk_1.newid)(), type: "print", values: values.map(console_copy_1.asItWas), level };
|
|
90
|
+
const entry = entryId ? (0, exports.consoleBlocks)().find((block) => block.id === entryId) : undefined;
|
|
91
|
+
if (entry?.type === "entry")
|
|
92
|
+
update(entry.id, { prints: [...(entry.prints ?? []), print] });
|
|
93
|
+
else
|
|
94
|
+
add(print);
|
|
95
|
+
}
|
|
96
|
+
/** Print values in the Console's output. */
|
|
97
|
+
function consolePrint(...values) {
|
|
98
|
+
printAt("log", values, current);
|
|
99
|
+
}
|
|
100
|
+
const counts = new Map();
|
|
101
|
+
const timers = new Map();
|
|
102
|
+
/** A timer's time so far, or a warning when it isn't running. */
|
|
103
|
+
function timerLine(label, values, end) {
|
|
104
|
+
const start = timers.get(label);
|
|
105
|
+
if (start === undefined) {
|
|
106
|
+
printAt("warn", [`Timer "${label}" doesn't exist.`], current);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (end)
|
|
110
|
+
timers.delete(label);
|
|
111
|
+
consolePrint(`${label}: ${(performance.now() - start).toFixed(1)} ms`, ...values);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* The `console` code at the Console gets: each method prints into the output. Each is its own
|
|
115
|
+
* function, so it keeps its name and its own argument hint.
|
|
116
|
+
*/
|
|
117
|
+
const printingConsole = {
|
|
118
|
+
log: (...values) => consolePrint(...values),
|
|
119
|
+
info: (...values) => consolePrint(...values),
|
|
120
|
+
debug: (...values) => consolePrint(...values),
|
|
121
|
+
dir: (...values) => consolePrint(...values),
|
|
122
|
+
table: (...values) => consolePrint(...values),
|
|
123
|
+
trace: (...values) => {
|
|
124
|
+
// Its caller's frames, down to the Console's evaluator, as the browser's trace shows them.
|
|
125
|
+
const stack = describeError(new Error("trace")).stack?.split("\n").slice(1).join("\n");
|
|
126
|
+
consolePrint(...values, ...(stack ? [stack] : []));
|
|
127
|
+
},
|
|
128
|
+
group: (...values) => consolePrint(...values),
|
|
129
|
+
groupCollapsed: (...values) => consolePrint(...values),
|
|
130
|
+
groupEnd: () => undefined,
|
|
131
|
+
warn: (...values) => printAt("warn", values, current),
|
|
132
|
+
error: (...values) => printAt("error", values, current),
|
|
133
|
+
assert: (condition, ...values) => {
|
|
134
|
+
if (!condition)
|
|
135
|
+
printAt("error", ["Assertion failed", ...values], current);
|
|
136
|
+
},
|
|
137
|
+
count: (label = "default") => {
|
|
138
|
+
const count = (counts.get(label) ?? 0) + 1;
|
|
139
|
+
counts.set(label, count);
|
|
140
|
+
consolePrint(`${label}: ${count}`);
|
|
141
|
+
},
|
|
142
|
+
countReset: (label = "default") => {
|
|
143
|
+
counts.delete(label);
|
|
144
|
+
},
|
|
145
|
+
time: (label = "default") => {
|
|
146
|
+
timers.set(label, performance.now());
|
|
147
|
+
},
|
|
148
|
+
timeLog: (label = "default", ...values) => timerLine(label, values, false),
|
|
149
|
+
timeEnd: (label = "default") => timerLine(label, [], true),
|
|
150
|
+
};
|
|
151
|
+
const VALUES = { name: "...values", type: "any" };
|
|
152
|
+
const LABEL = {
|
|
153
|
+
name: "label",
|
|
154
|
+
type: "string",
|
|
155
|
+
optional: true,
|
|
156
|
+
description: 'Defaults to "default".',
|
|
157
|
+
};
|
|
158
|
+
/** Each `console` method's arguments, for its grey line. */
|
|
159
|
+
const CONSOLE_PARAMS = {
|
|
160
|
+
log: [VALUES],
|
|
161
|
+
info: [VALUES],
|
|
162
|
+
debug: [VALUES],
|
|
163
|
+
dir: [VALUES],
|
|
164
|
+
table: [VALUES],
|
|
165
|
+
trace: [VALUES],
|
|
166
|
+
group: [VALUES],
|
|
167
|
+
groupCollapsed: [VALUES],
|
|
168
|
+
groupEnd: [],
|
|
169
|
+
warn: [VALUES],
|
|
170
|
+
error: [VALUES],
|
|
171
|
+
assert: [
|
|
172
|
+
{
|
|
173
|
+
name: "condition",
|
|
174
|
+
type: "any",
|
|
175
|
+
optional: true,
|
|
176
|
+
description: "When it's falsy, the values print as an error.",
|
|
177
|
+
},
|
|
178
|
+
VALUES,
|
|
179
|
+
],
|
|
180
|
+
count: [LABEL],
|
|
181
|
+
countReset: [LABEL],
|
|
182
|
+
time: [LABEL],
|
|
183
|
+
timeLog: [LABEL, VALUES],
|
|
184
|
+
timeEnd: [LABEL],
|
|
185
|
+
};
|
|
186
|
+
for (const [name, params] of Object.entries(CONSOLE_PARAMS)) {
|
|
187
|
+
(0, repl_1.withCallInfo)(printingConsole[name], {
|
|
188
|
+
name,
|
|
189
|
+
kind: "helper",
|
|
190
|
+
params,
|
|
191
|
+
objectArgument: false,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/** The Console's own helpers, beside the session's, with their argument hints. */
|
|
195
|
+
const consoleHelpers = {
|
|
196
|
+
help: (0, repl_1.withCallInfo)(function help() {
|
|
197
|
+
return CONSOLE_HELP_TEXT;
|
|
198
|
+
}, {
|
|
199
|
+
name: "help",
|
|
200
|
+
kind: "helper",
|
|
201
|
+
params: [],
|
|
202
|
+
objectArgument: false,
|
|
203
|
+
description: "How contracts, these helpers and the Console's keys are used.",
|
|
204
|
+
}),
|
|
205
|
+
print: (0, repl_1.withCallInfo)(function print(...values) {
|
|
206
|
+
consolePrint(...values);
|
|
207
|
+
}, {
|
|
208
|
+
name: "print",
|
|
209
|
+
kind: "helper",
|
|
210
|
+
params: [{ name: "...values", type: "any" }],
|
|
211
|
+
objectArgument: false,
|
|
212
|
+
description: "Show values in the output; console.log does the same.",
|
|
213
|
+
}),
|
|
214
|
+
console: printingConsole,
|
|
215
|
+
clear: (0, repl_1.withCallInfo)(function clear() {
|
|
216
|
+
clearConsole(current);
|
|
217
|
+
return undefined;
|
|
218
|
+
}, {
|
|
219
|
+
name: "clear",
|
|
220
|
+
kind: "helper",
|
|
221
|
+
params: [],
|
|
222
|
+
objectArgument: false,
|
|
223
|
+
description: "Empty the output.",
|
|
224
|
+
}),
|
|
225
|
+
};
|
|
226
|
+
/**
|
|
227
|
+
* Empty the output, except entries still running or waiting to run, whose results would
|
|
228
|
+
* otherwise have nowhere to show.
|
|
229
|
+
*
|
|
230
|
+
* @param except An entry to remove even so: the `clear()` that's running.
|
|
231
|
+
*/
|
|
232
|
+
function clearConsole(except) {
|
|
233
|
+
(0, exports.consoleBlocks)((0, exports.consoleBlocks)().filter((block) => block.type === "entry" && block.status === "running" && block.id !== except));
|
|
234
|
+
}
|
|
235
|
+
/** Open a session over a transport, keeping the current data context if there is one. */
|
|
236
|
+
async function open(transport) {
|
|
237
|
+
const next = new repl_1.ContractSession({
|
|
238
|
+
transport,
|
|
239
|
+
userId: (0, peers_sdk_1.myUserId)(),
|
|
240
|
+
scope: exports.consoleScope.values,
|
|
241
|
+
print: (value) => printAt("log", [value], undefined),
|
|
242
|
+
reserved: [...console_scope_1.CONSOLE_GLOBAL_NAMES, "print", "console", "clear", "$"],
|
|
243
|
+
});
|
|
244
|
+
next.onChange = () => {
|
|
245
|
+
(0, exports.consoleContext)(next.contextName);
|
|
246
|
+
followPackages(transport, next.dataContextId);
|
|
247
|
+
};
|
|
248
|
+
// The old session's globals leave the scope first, so they don't take the new ones with them.
|
|
249
|
+
session?.dispose();
|
|
250
|
+
sessionTransport = undefined;
|
|
251
|
+
// Keep the previous group's id available if the load fails and opening is retried.
|
|
252
|
+
const line = await next.use(session?.dataContextId);
|
|
253
|
+
session = next;
|
|
254
|
+
sessionTransport = transport;
|
|
255
|
+
Object.assign(exports.consoleScope.values, next.helpers(), consoleHelpers);
|
|
256
|
+
(0, exports.consoleReady)(true);
|
|
257
|
+
info(line);
|
|
258
|
+
}
|
|
259
|
+
/** How long package changes settle before the Console looks at its contracts again. */
|
|
260
|
+
const PACKAGES_SETTLE_MS = 750;
|
|
261
|
+
/**
|
|
262
|
+
* How long the queue waits for that look. Contract calls have no timeout, so a reply lost on a
|
|
263
|
+
* dropped connection would otherwise hold every later entry.
|
|
264
|
+
*/
|
|
265
|
+
const REFRESH_WAIT_MS = 10_000;
|
|
266
|
+
/** The packages var the Console follows: for which group, over which transport. */
|
|
267
|
+
let packagesWatch;
|
|
268
|
+
let settling;
|
|
269
|
+
/**
|
|
270
|
+
* Follow which packages run in the session's group on this device, as System Packages'
|
|
271
|
+
* `runtimeStates` reports them. When one starts, stops or changes version, the Console looks at
|
|
272
|
+
* its contracts again: a package that starts after the Console loaded (a Console tab restored
|
|
273
|
+
* at launch, say) brings its contracts then.
|
|
274
|
+
*/
|
|
275
|
+
function followPackages(transport, dataContextId) {
|
|
276
|
+
// A stopped use() can finish on an older transport after the replacement has opened.
|
|
277
|
+
if (transport !== (0, peers_sdk_1.getHostContractTransport)())
|
|
278
|
+
return;
|
|
279
|
+
if (packagesWatch?.transport === transport && packagesWatch.dataContextId === dataContextId)
|
|
280
|
+
return;
|
|
281
|
+
packagesWatch?.dispose();
|
|
282
|
+
const consumer = (0, peers_sdk_1.createContractConsumer)((0, peers_sdk_1.getPackagesContractDefinition)(), transport, {
|
|
283
|
+
dataContextId,
|
|
284
|
+
});
|
|
285
|
+
packagesWatch = { dispose: () => consumer.dispose(), transport, dataContextId };
|
|
286
|
+
const states = consumer.observables.runtimeStates;
|
|
287
|
+
// A dev package can change its version while keeping the same PackageVersion row.
|
|
288
|
+
const summary = (value) => JSON.stringify(Object.entries(value ?? {})
|
|
289
|
+
.map(([packageId, state]) => [
|
|
290
|
+
packageId,
|
|
291
|
+
state?.status,
|
|
292
|
+
state?.running?.packageVersionId,
|
|
293
|
+
state?.running?.version,
|
|
294
|
+
])
|
|
295
|
+
.sort());
|
|
296
|
+
let last = summary(states());
|
|
297
|
+
states.subscribe((value) => {
|
|
298
|
+
const next = summary(value);
|
|
299
|
+
if (next === last)
|
|
300
|
+
return;
|
|
301
|
+
last = next;
|
|
302
|
+
lookAgainSoon();
|
|
303
|
+
});
|
|
304
|
+
// A package may have started between loading the contracts and this watch.
|
|
305
|
+
lookAgainSoon();
|
|
306
|
+
}
|
|
307
|
+
/** Look at the contracts again once package changes settle, after the entries queued so far. */
|
|
308
|
+
function lookAgainSoon() {
|
|
309
|
+
const watch = packagesWatch;
|
|
310
|
+
if (settling)
|
|
311
|
+
clearTimeout(settling);
|
|
312
|
+
settling = setTimeout(() => {
|
|
313
|
+
settling = undefined;
|
|
314
|
+
queue = queue.then(() => {
|
|
315
|
+
if (watch === packagesWatch)
|
|
316
|
+
return refreshConsoleContracts();
|
|
317
|
+
});
|
|
318
|
+
}, PACKAGES_SETTLE_MS);
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Reload the contracts when the session's group serves a different set than it loaded: one
|
|
322
|
+
* `listContracts` call, and a reload only on a difference, so a package restart that changes
|
|
323
|
+
* nothing keeps the person's handles and subscriptions. On a host without `listContracts`, it
|
|
324
|
+
* changes and shows nothing. It's let go after `waitMs`, and a reply that comes later changes
|
|
325
|
+
* nothing.
|
|
326
|
+
*
|
|
327
|
+
* @param waitMs How long to wait for the host before letting the look go.
|
|
328
|
+
* @returns When it's done or let go; a failure is shown in the output.
|
|
329
|
+
*/
|
|
330
|
+
async function refreshConsoleContracts(waitMs = REFRESH_WAIT_MS) {
|
|
331
|
+
let letGo = false;
|
|
332
|
+
let timer;
|
|
333
|
+
const deadline = new Promise((resolve) => {
|
|
334
|
+
timer = setTimeout(() => {
|
|
335
|
+
letGo = true;
|
|
336
|
+
resolve();
|
|
337
|
+
}, waitMs);
|
|
338
|
+
});
|
|
339
|
+
try {
|
|
340
|
+
await Promise.race([lookAtContracts(() => letGo), deadline]);
|
|
341
|
+
}
|
|
342
|
+
finally {
|
|
343
|
+
clearTimeout(timer);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* The look itself: compare, and reload on a difference unless `letGo()` says it's too late. A
|
|
348
|
+
* reload already under way when the look is let go still finishes.
|
|
349
|
+
*/
|
|
350
|
+
async function lookAtContracts(letGo) {
|
|
351
|
+
const current = session;
|
|
352
|
+
const transport = sessionTransport;
|
|
353
|
+
if (!current?.current || !transport || transport !== (0, peers_sdk_1.getHostContractTransport)())
|
|
354
|
+
return;
|
|
355
|
+
const dataContextId = current.dataContextId;
|
|
356
|
+
const isCurrent = () => !letGo() &&
|
|
357
|
+
session === current &&
|
|
358
|
+
current.dataContextId === dataContextId &&
|
|
359
|
+
transport === (0, peers_sdk_1.getHostContractTransport)();
|
|
360
|
+
const catalog = (0, peers_sdk_1.createContractConsumer)((0, peers_sdk_1.getContractCatalogDefinition)(), transport, {
|
|
361
|
+
dataContextId,
|
|
362
|
+
});
|
|
363
|
+
try {
|
|
364
|
+
const { contracts } = await catalog.tools.listContracts({});
|
|
365
|
+
if (!isCurrent())
|
|
366
|
+
return;
|
|
367
|
+
const served = contracts
|
|
368
|
+
.filter((contract) => contract.kind !== "ui")
|
|
369
|
+
.map((contract) => `${contract.contractId}@${contract.version}:${contract.provider?.packageId ?? ""}`)
|
|
370
|
+
.sort()
|
|
371
|
+
.join();
|
|
372
|
+
const loaded = current.current.handles
|
|
373
|
+
.map((handle) => `${handle.id}@${handle.version}:${handle.entry.providerPackageId ?? ""}`)
|
|
374
|
+
.sort()
|
|
375
|
+
.join();
|
|
376
|
+
if (served === loaded)
|
|
377
|
+
return;
|
|
378
|
+
info(`The packages here changed. ${await current.use(dataContextId)}`);
|
|
379
|
+
}
|
|
380
|
+
catch (error) {
|
|
381
|
+
if (!isCurrent())
|
|
382
|
+
return;
|
|
383
|
+
const message = messageOf(error);
|
|
384
|
+
// An older host, which loaded the contracts through Tool Catalog: use() reloads them there.
|
|
385
|
+
if (message.includes("does not provide tool 'listContracts'"))
|
|
386
|
+
return;
|
|
387
|
+
info(`The Console couldn't reload the contracts: ${message}`, "error");
|
|
388
|
+
}
|
|
389
|
+
finally {
|
|
390
|
+
catalog.dispose();
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Start the Console's session, once: wait for the host, load personal's contracts, and reload
|
|
395
|
+
* them in the same group when the host connection is replaced.
|
|
396
|
+
*
|
|
397
|
+
* @returns When the contracts are loaded. A failure is shown in the output, and the next call
|
|
398
|
+
* tries again.
|
|
399
|
+
*/
|
|
400
|
+
function startConsole() {
|
|
401
|
+
starting ??= (async () => {
|
|
402
|
+
void loadHistory();
|
|
403
|
+
const transport = (0, peers_sdk_1.getHostContractTransport)() ?? (await (0, peers_sdk_1.waitForHostContractTransport)({ timeoutMs: 30_000 }));
|
|
404
|
+
await open(transport);
|
|
405
|
+
// The transport may already have changed while the first open was in flight.
|
|
406
|
+
unsubscribeTransport ??= (0, peers_sdk_1.subscribeHostContractTransport)((replacement) => {
|
|
407
|
+
if (!replacement || replacement === sessionTransport)
|
|
408
|
+
return;
|
|
409
|
+
(0, exports.consoleReady)(false);
|
|
410
|
+
queue = queue.then(() =>
|
|
411
|
+
// An earlier queued entry can change the group with use().
|
|
412
|
+
open(replacement).catch((error) => {
|
|
413
|
+
starting = undefined;
|
|
414
|
+
info(`The connection to the host changed, and the contracts didn't reload: ${messageOf(error)}`, "error");
|
|
415
|
+
}));
|
|
416
|
+
});
|
|
417
|
+
})().catch((error) => {
|
|
418
|
+
starting = undefined;
|
|
419
|
+
info(`The Console couldn't load the contracts: ${messageOf(error)}`, "error");
|
|
420
|
+
throw error;
|
|
421
|
+
});
|
|
422
|
+
return starting;
|
|
423
|
+
}
|
|
424
|
+
function messageOf(error) {
|
|
425
|
+
try {
|
|
426
|
+
return String(error instanceof Error ? error.message : error);
|
|
427
|
+
}
|
|
428
|
+
catch {
|
|
429
|
+
return "Error could not be displayed";
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
/** An error as the Console shows it: its message, a hint for a name, and its own frames only. */
|
|
433
|
+
function describeError(error) {
|
|
434
|
+
try {
|
|
435
|
+
if (!(error instanceof Error))
|
|
436
|
+
return { message: `Uncaught ${String(error)}` };
|
|
437
|
+
// A validation error's issues read one per line: `status: Invalid enum value. …`.
|
|
438
|
+
const text = (0, repl_1.errorMessage)(error);
|
|
439
|
+
const message = error.name && error.name !== "Error" ? `${error.name}: ${text}` : text;
|
|
440
|
+
const stackText = error.stack ?? "";
|
|
441
|
+
const header = `${error.name}: ${error.message}\n`;
|
|
442
|
+
const frames = text !== error.message && stackText.startsWith(header)
|
|
443
|
+
? stackText.slice(header.length).split("\n")
|
|
444
|
+
: stackText.split("\n").slice(1);
|
|
445
|
+
// Stop at the Console's evaluator: the frames below it are the Console's, not the code's.
|
|
446
|
+
const end = frames.findIndex((frame) => /ConsoleScope\.evaluate|__consoleScope/.test(frame));
|
|
447
|
+
const stack = (end < 0 ? frames : frames.slice(0, end)).join("\n").trim();
|
|
448
|
+
return { message, hint: session?.hint(error), stack: stack || undefined };
|
|
449
|
+
}
|
|
450
|
+
catch {
|
|
451
|
+
return { message: "Uncaught value could not be displayed" };
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
/** Remember what was run, without repeating the entry just before it. */
|
|
455
|
+
function remember(code) {
|
|
456
|
+
const append = () => {
|
|
457
|
+
const past = consoleHistory();
|
|
458
|
+
if (past[past.length - 1] === code)
|
|
459
|
+
return;
|
|
460
|
+
history = [...past, code].slice(-MAX_HISTORY);
|
|
461
|
+
historyVar?.(history);
|
|
462
|
+
};
|
|
463
|
+
if (history)
|
|
464
|
+
append();
|
|
465
|
+
else
|
|
466
|
+
void loadHistory().then(append);
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Run code at the Console. Entries run one after another, as at Node's REPL; one that's waiting
|
|
470
|
+
* can be let go with {@link stopWaiting}.
|
|
471
|
+
*
|
|
472
|
+
* @param code What the person typed.
|
|
473
|
+
*/
|
|
474
|
+
function runInConsole(code) {
|
|
475
|
+
if (!code.trim())
|
|
476
|
+
return;
|
|
477
|
+
remember(code);
|
|
478
|
+
const entry = add({
|
|
479
|
+
id: (0, peers_sdk_1.newid)(),
|
|
480
|
+
type: "entry",
|
|
481
|
+
code,
|
|
482
|
+
context: (0, exports.consoleContext)(),
|
|
483
|
+
status: "running",
|
|
484
|
+
});
|
|
485
|
+
// Stop waiting works from the start: an entry let go before its turn never runs.
|
|
486
|
+
let letGo = false;
|
|
487
|
+
const stopped = new Promise((resolve) => stops.set(entry.id, () => {
|
|
488
|
+
if (letGo)
|
|
489
|
+
return;
|
|
490
|
+
letGo = true;
|
|
491
|
+
resolve("stopped");
|
|
492
|
+
if (current !== entry.id)
|
|
493
|
+
update(entry.id, { status: "stopped" });
|
|
494
|
+
}));
|
|
495
|
+
queue = queue.then(async () => {
|
|
496
|
+
if (letGo) {
|
|
497
|
+
stops.delete(entry.id);
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
const started = Date.now();
|
|
501
|
+
current = entry.id;
|
|
502
|
+
try {
|
|
503
|
+
// An entry let go while the contracts load doesn't run once they've loaded.
|
|
504
|
+
if ((await Promise.race([stopped, startConsole()])) === "stopped") {
|
|
505
|
+
update(entry.id, { status: "stopped" });
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
// An entry queued behind use() runs in the group use() moved to.
|
|
509
|
+
const context = (0, exports.consoleContext)();
|
|
510
|
+
if (context !== entry.context)
|
|
511
|
+
update(entry.id, { context });
|
|
512
|
+
const result = await Promise.race([
|
|
513
|
+
exports.consoleScope.evaluate(code).then((value) => ({ value })),
|
|
514
|
+
stopped,
|
|
515
|
+
]);
|
|
516
|
+
if (result === "stopped")
|
|
517
|
+
update(entry.id, { status: "stopped", ms: Date.now() - started });
|
|
518
|
+
else
|
|
519
|
+
update(entry.id, {
|
|
520
|
+
status: "done",
|
|
521
|
+
value: (0, console_copy_1.asItWas)(result.value),
|
|
522
|
+
ms: Date.now() - started,
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
catch (error) {
|
|
526
|
+
update(entry.id, { status: "failed", error: describeError(error), ms: Date.now() - started });
|
|
527
|
+
}
|
|
528
|
+
finally {
|
|
529
|
+
stops.delete(entry.id);
|
|
530
|
+
current = undefined;
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Stop waiting for an entry, so the next one can run. Its calls may still finish on the host.
|
|
536
|
+
*
|
|
537
|
+
* @param id The entry's id.
|
|
538
|
+
*/
|
|
539
|
+
function stopWaiting(id) {
|
|
540
|
+
stops.get(id)?.();
|
|
541
|
+
}
|
|
542
|
+
/** End the session and forget everything: for tests. */
|
|
543
|
+
function resetConsoleForTests() {
|
|
544
|
+
packagesWatch?.dispose();
|
|
545
|
+
packagesWatch = undefined;
|
|
546
|
+
if (settling)
|
|
547
|
+
clearTimeout(settling);
|
|
548
|
+
settling = undefined;
|
|
549
|
+
session?.dispose();
|
|
550
|
+
session = undefined;
|
|
551
|
+
sessionTransport = undefined;
|
|
552
|
+
starting = undefined;
|
|
553
|
+
unsubscribeTransport?.();
|
|
554
|
+
unsubscribeTransport = undefined;
|
|
555
|
+
queue = Promise.resolve();
|
|
556
|
+
stops.clear();
|
|
557
|
+
counts.clear();
|
|
558
|
+
timers.clear();
|
|
559
|
+
historyVar = undefined;
|
|
560
|
+
history = undefined;
|
|
561
|
+
for (const name of Object.keys(exports.consoleScope.values))
|
|
562
|
+
delete exports.consoleScope.values[name];
|
|
563
|
+
(0, exports.consoleBlocks)([]);
|
|
564
|
+
(0, exports.consoleContext)("personal");
|
|
565
|
+
(0, exports.consoleDraft)("");
|
|
566
|
+
(0, exports.consoleReady)(false);
|
|
567
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** A fake host for the Console's tests: Tasks and Task List in personal, Task List in Home. */
|
|
2
|
+
import { type IContractDefinition, type IContractTransport } from "@peers-app/peers-sdk";
|
|
3
|
+
export declare const userId = "user00000000000000000000001";
|
|
4
|
+
export declare const homeId = "group0000000000000000000001";
|
|
5
|
+
/** A contract a package that starts late serves. */
|
|
6
|
+
export declare const notes: IContractDefinition;
|
|
7
|
+
/**
|
|
8
|
+
* Serve a contract in a data context from now on, as a package that starts late does, or stop
|
|
9
|
+
* serving it.
|
|
10
|
+
*/
|
|
11
|
+
export declare function serve(dataContextId: string, definition: IContractDefinition, served?: boolean): void;
|
|
12
|
+
/** What the host answers `findTasks` with. */
|
|
13
|
+
export declare const foundTasks: {
|
|
14
|
+
taskId: string;
|
|
15
|
+
title: string;
|
|
16
|
+
}[];
|
|
17
|
+
/** A transport to the fake host, recording each call. */
|
|
18
|
+
export declare function fakeHost(): {
|
|
19
|
+
transport: IContractTransport;
|
|
20
|
+
calls: any[];
|
|
21
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** A fake host for the Console's tests: Tasks and Task List in personal, Task List in Home. */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.foundTasks = exports.notes = exports.homeId = exports.userId = void 0;
|
|
5
|
+
exports.serve = serve;
|
|
6
|
+
exports.fakeHost = fakeHost;
|
|
7
|
+
const peers_sdk_1 = require("@peers-app/peers-sdk");
|
|
8
|
+
exports.userId = "user00000000000000000000001";
|
|
9
|
+
exports.homeId = "group0000000000000000000001";
|
|
10
|
+
const tasks = {
|
|
11
|
+
contractId: "tasks0000000000000000000001",
|
|
12
|
+
version: 1,
|
|
13
|
+
name: "Tasks",
|
|
14
|
+
description: "Tasks to do.",
|
|
15
|
+
tables: [
|
|
16
|
+
{
|
|
17
|
+
name: "Tasks",
|
|
18
|
+
description: "Every task.",
|
|
19
|
+
primaryKeyName: "taskId",
|
|
20
|
+
fields: [
|
|
21
|
+
{ name: "taskId", type: "id" },
|
|
22
|
+
{ name: "title", type: "string" },
|
|
23
|
+
],
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
tools: [],
|
|
27
|
+
observables: [],
|
|
28
|
+
events: [],
|
|
29
|
+
};
|
|
30
|
+
const taskList = {
|
|
31
|
+
contractId: "tasklist00000000000000001",
|
|
32
|
+
version: 1,
|
|
33
|
+
name: "Task List",
|
|
34
|
+
description: "Find tasks.",
|
|
35
|
+
tables: [],
|
|
36
|
+
tools: [
|
|
37
|
+
{
|
|
38
|
+
name: "findTasks",
|
|
39
|
+
usageDescription: "Find tasks by words.",
|
|
40
|
+
inputFields: [
|
|
41
|
+
{ name: "words", type: "string", description: "Words in the title." },
|
|
42
|
+
{ name: "limit", type: "number", optional: true },
|
|
43
|
+
],
|
|
44
|
+
outputFields: [],
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
observables: [],
|
|
48
|
+
events: [],
|
|
49
|
+
};
|
|
50
|
+
/** Contracts per data context. */
|
|
51
|
+
const contexts = {
|
|
52
|
+
[exports.userId]: [tasks, taskList],
|
|
53
|
+
[exports.homeId]: [taskList],
|
|
54
|
+
};
|
|
55
|
+
/** A contract a package that starts late serves. */
|
|
56
|
+
exports.notes = {
|
|
57
|
+
...tasks,
|
|
58
|
+
contractId: "notes0000000000000000000001",
|
|
59
|
+
name: "Notes",
|
|
60
|
+
description: "Notes.",
|
|
61
|
+
tables: [],
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Serve a contract in a data context from now on, as a package that starts late does, or stop
|
|
65
|
+
* serving it.
|
|
66
|
+
*/
|
|
67
|
+
function serve(dataContextId, definition, served = true) {
|
|
68
|
+
const list = contexts[dataContextId].filter((def) => def.contractId !== definition.contractId);
|
|
69
|
+
contexts[dataContextId] = served ? [...list, definition] : list;
|
|
70
|
+
}
|
|
71
|
+
/** What the host answers `findTasks` with. */
|
|
72
|
+
exports.foundTasks = [
|
|
73
|
+
{ taskId: "t1", title: "Car insurance" },
|
|
74
|
+
{ taskId: "t2", title: "Renew car tax" },
|
|
75
|
+
];
|
|
76
|
+
/** A transport to the fake host, recording each call. */
|
|
77
|
+
function fakeHost() {
|
|
78
|
+
const calls = [];
|
|
79
|
+
const transport = {
|
|
80
|
+
on: () => () => { },
|
|
81
|
+
emit: async (_channel, call) => {
|
|
82
|
+
calls.push(call);
|
|
83
|
+
const { contractId, name, args, dataContextId } = call;
|
|
84
|
+
const served = contexts[dataContextId] ?? [];
|
|
85
|
+
if (contractId === peers_sdk_1.contractCatalogContractId && name === "listContracts") {
|
|
86
|
+
return {
|
|
87
|
+
contracts: served.map((def) => ({
|
|
88
|
+
contractId: def.contractId,
|
|
89
|
+
version: def.version,
|
|
90
|
+
name: def.name,
|
|
91
|
+
description: def.description,
|
|
92
|
+
provider: { packageId: "package000000000000000001", name: "peers-core" },
|
|
93
|
+
})),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
if (contractId === peers_sdk_1.contractCatalogContractId && name === "getContractDefinition") {
|
|
97
|
+
return { definition: served.find((def) => def.contractId === args[0].contractId) };
|
|
98
|
+
}
|
|
99
|
+
if (contractId === peers_sdk_1.groupsContractId && name === "listGroups") {
|
|
100
|
+
return {
|
|
101
|
+
groups: [
|
|
102
|
+
{ groupId: exports.userId, name: "Personal", personal: true },
|
|
103
|
+
{ groupId: exports.homeId, name: "Home", personal: false },
|
|
104
|
+
],
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
if (contractId === taskList.contractId && name === "findTasks") {
|
|
108
|
+
return { tasks: exports.foundTasks, words: args[0].words };
|
|
109
|
+
}
|
|
110
|
+
return undefined;
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
return { transport, calls };
|
|
114
|
+
}
|