@operad-io/tui 0.1.1 → 0.2.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/package.json +3 -4
- package/src/actions.ts +428 -24
- package/src/app.ts +105 -21
- package/src/chat.ts +241 -0
- package/src/client.ts +17 -37
- package/src/commands.ts +128 -0
- package/src/events.ts +113 -19
- package/src/index.ts +1 -0
- package/src/live.ts +39 -4
- package/src/markdown.ts +1 -36
- package/src/recent.ts +3 -6
- package/src/state.ts +311 -272
- package/src/theme.ts +8 -18
- package/src/view/index.ts +124 -19
- package/src/wrap.ts +2 -9
package/src/commands.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The commands a chat offers on `/`: the actions an object marks with a
|
|
3
|
+
* description, the name each is typed by, and the hint for its arguments.
|
|
4
|
+
* Nothing here runs an action: a chosen command only fills the chat's input,
|
|
5
|
+
* and the line goes through the chat's send action like any other text.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ViewAction, ViewRow } from "@operad-io/engine";
|
|
9
|
+
import { allowedText, argFields, type ArgField } from "./args.ts";
|
|
10
|
+
import { sanitize } from "./sanitize.ts";
|
|
11
|
+
|
|
12
|
+
/** One command, as the input lists it. */
|
|
13
|
+
export interface TuiChatCommand {
|
|
14
|
+
/** The name typed after the slash, with the clash rule applied. */
|
|
15
|
+
readonly name: string;
|
|
16
|
+
/** The name with its slash. */
|
|
17
|
+
readonly slash: string;
|
|
18
|
+
/** The one-line description from the action's declaration. */
|
|
19
|
+
readonly description: string;
|
|
20
|
+
/** The arguments in one line; a choice lists its options. */
|
|
21
|
+
readonly hint: string;
|
|
22
|
+
/** The arguments the action asks for, in order. */
|
|
23
|
+
readonly fields: readonly ArgField[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The actions and row a command list is built from. */
|
|
27
|
+
export interface CommandActions {
|
|
28
|
+
readonly actions: readonly ViewAction[];
|
|
29
|
+
readonly row?: ViewRow | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The state a command list is built from: the open object's actions and input. */
|
|
33
|
+
export interface ChatCommandSource {
|
|
34
|
+
readonly object?: CommandActions | undefined;
|
|
35
|
+
readonly input?: unknown;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Does this action declare that a chat may type it after a slash? */
|
|
39
|
+
function isCommand(action: ViewAction): boolean {
|
|
40
|
+
return typeof action.command === "string" && action.command.trim().length > 0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const shortOf = (action: ViewAction): string =>
|
|
44
|
+
action.action.length > 0 ? action.action : action.name.slice(action.name.indexOf(":") + 1);
|
|
45
|
+
|
|
46
|
+
/** The name each command is typed by; the `chat` plugin keeps a clashing short name. */
|
|
47
|
+
function resolvedNames(actions: readonly ViewAction[]): ReadonlyMap<string, string> {
|
|
48
|
+
const groups = new Map<string, ViewAction[]>();
|
|
49
|
+
for (const action of actions) {
|
|
50
|
+
const short = shortOf(action);
|
|
51
|
+
const group = groups.get(short) ?? [];
|
|
52
|
+
group.push(action);
|
|
53
|
+
groups.set(short, group);
|
|
54
|
+
}
|
|
55
|
+
const names = new Map<string, string>();
|
|
56
|
+
for (const [short, group] of groups) {
|
|
57
|
+
for (const action of group) {
|
|
58
|
+
const name =
|
|
59
|
+
group.length === 1 || action.plugin === "chat"
|
|
60
|
+
? short
|
|
61
|
+
: `${action.plugin}_${short}`;
|
|
62
|
+
names.set(action.name, name);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return names;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The line after the arguments: each argument, a choice with every option. */
|
|
69
|
+
export function argumentHint(fields: readonly ArgField[]): string {
|
|
70
|
+
return fields.map(allowedText).join(" · ");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Every command this chat has, in the order the actions are declared. */
|
|
74
|
+
export function chatCommands(
|
|
75
|
+
actions: readonly ViewAction[],
|
|
76
|
+
row?: ViewRow | undefined,
|
|
77
|
+
): readonly TuiChatCommand[] {
|
|
78
|
+
const declared = actions.filter(isCommand);
|
|
79
|
+
const names = resolvedNames(declared);
|
|
80
|
+
return declared.map((action) => {
|
|
81
|
+
const fields = argFields(action, row);
|
|
82
|
+
const description = sanitize((action.command ?? "").replace(/\s+/g, " ").trim());
|
|
83
|
+
const name = names.get(action.name) ?? shortOf(action);
|
|
84
|
+
return {
|
|
85
|
+
name,
|
|
86
|
+
slash: `/${name}`,
|
|
87
|
+
description,
|
|
88
|
+
hint: argumentHint(fields),
|
|
89
|
+
fields,
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function queryOf(text: string): string {
|
|
95
|
+
const withoutSlash = text.trimStart().replace(/^\//, "");
|
|
96
|
+
return (withoutSlash.split(/\s+/)[0] ?? "").toLowerCase();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The commands a filtered word keeps, in declaration order. */
|
|
100
|
+
export function filterCommands(
|
|
101
|
+
text: string,
|
|
102
|
+
commands: readonly TuiChatCommand[],
|
|
103
|
+
): readonly TuiChatCommand[] {
|
|
104
|
+
const query = queryOf(text);
|
|
105
|
+
return query === ""
|
|
106
|
+
? commands
|
|
107
|
+
: commands.filter((command) => command.name.toLowerCase().startsWith(query));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function textOf(source: ChatCommandSource): string {
|
|
111
|
+
const input = source.input as { readonly kind?: unknown; readonly text?: unknown } | undefined;
|
|
112
|
+
return input?.kind === "note" && typeof input.text === "string" ? input.text : "";
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The commands the open chat input lists now, filtered by what is typed after the slash. */
|
|
116
|
+
export function commandEntries(
|
|
117
|
+
source: ChatCommandSource,
|
|
118
|
+
text?: string,
|
|
119
|
+
): readonly TuiChatCommand[] {
|
|
120
|
+
const object = source.object;
|
|
121
|
+
if (object === undefined) return [];
|
|
122
|
+
return filterCommands(text ?? textOf(source), chatCommands(object.actions, object.row));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** What choosing a command puts in the input: its slash name, and a space when it takes arguments. */
|
|
126
|
+
export function commandText(command: TuiChatCommand): string {
|
|
127
|
+
return command.fields.length > 0 ? `${command.slash} ` : command.slash;
|
|
128
|
+
}
|
package/src/events.ts
CHANGED
|
@@ -19,6 +19,28 @@ export const DEFAULT_RETRY_MIN_MS = 250;
|
|
|
19
19
|
/** The longest delay between reconnects. */
|
|
20
20
|
export const DEFAULT_RETRY_MAX_MS = 5_000;
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* The most characters one line of the stream may hold, its line ending
|
|
24
|
+
* excluded.
|
|
25
|
+
*
|
|
26
|
+
* The server's lines are a field name and a short value: an `id:` of
|
|
27
|
+
* `<boot>-<seq>`, an `event:` name such as `reset`, or `data:` with
|
|
28
|
+
* `{"id":"<object>"}`. 64 KiB leaves room for an id far longer than any the
|
|
29
|
+
* server writes and still caps a broken line at a few pages. The check counts
|
|
30
|
+
* decoded characters, not bytes: the decoder has already run, and a character
|
|
31
|
+
* is at most four UTF-8 bytes, so the buffer stays bounded either way.
|
|
32
|
+
*/
|
|
33
|
+
export const MAX_EVENT_LINE_CHARS = 64 * 1024;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The most characters one event block may hold.
|
|
37
|
+
*
|
|
38
|
+
* 1 MiB is the API's own request-body cap, so an event can never be larger
|
|
39
|
+
* than a body the server accepts. Real events carry one object id and are a
|
|
40
|
+
* few dozen bytes. Like the line limit, this counts decoded characters.
|
|
41
|
+
*/
|
|
42
|
+
export const MAX_EVENT_BLOCK_CHARS = 1024 * 1024;
|
|
43
|
+
|
|
22
44
|
/** One object changed. */
|
|
23
45
|
export interface TuiChangeEvent {
|
|
24
46
|
readonly kind: "change";
|
|
@@ -101,6 +123,93 @@ interface SseBlock {
|
|
|
101
123
|
readonly data: string;
|
|
102
124
|
}
|
|
103
125
|
|
|
126
|
+
/**
|
|
127
|
+
* The unfinished block of one connection. A block ends at the line ending that
|
|
128
|
+
* ends an empty line, and every character is looked at once as it arrives, so a
|
|
129
|
+
* chunk costs what it adds and never the tail it is appended to.
|
|
130
|
+
*/
|
|
131
|
+
class BlockTail {
|
|
132
|
+
/** The characters of the unfinished block that arrived in earlier chunks. */
|
|
133
|
+
private readonly pieces: string[] = [];
|
|
134
|
+
/** The characters {@link pieces} hold. */
|
|
135
|
+
private length = 0;
|
|
136
|
+
/** The characters of the line still being received, its ending excluded. */
|
|
137
|
+
private lineLength = 0;
|
|
138
|
+
/** Whether the last character is a carriage return that awaits its line feed. */
|
|
139
|
+
private carriage = false;
|
|
140
|
+
/** The length of the line ending that ended the last line of this block. */
|
|
141
|
+
private ending = 0;
|
|
142
|
+
|
|
143
|
+
/** Adds one chunk; every block it completes goes to `onBlock` in order. */
|
|
144
|
+
push(chunk: string, onBlock: (block: string) => void): void {
|
|
145
|
+
let start = 0;
|
|
146
|
+
for (let i = 0; i < chunk.length; i += 1) {
|
|
147
|
+
const code = chunk.charCodeAt(i);
|
|
148
|
+
if (code === 13) {
|
|
149
|
+
// Two carriage returns in a row: the first one was content.
|
|
150
|
+
if (this.carriage) this.lineLength += 1;
|
|
151
|
+
this.carriage = true;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (code !== 10) {
|
|
155
|
+
// A carriage return not followed by a line feed is content too.
|
|
156
|
+
if (this.carriage) {
|
|
157
|
+
this.lineLength += 1;
|
|
158
|
+
this.carriage = false;
|
|
159
|
+
}
|
|
160
|
+
this.lineLength += 1;
|
|
161
|
+
if (this.lineLength > MAX_EVENT_LINE_CHARS) {
|
|
162
|
+
throw new Error(
|
|
163
|
+
`a line of the event stream is longer than ${MAX_EVENT_LINE_CHARS} characters`,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const ending = this.carriage ? 2 : 1;
|
|
169
|
+
if (this.lineLength > 0 || this.ending === 0) {
|
|
170
|
+
// The line had content, or no line ended before it: no block ends here.
|
|
171
|
+
this.ending = ending;
|
|
172
|
+
this.lineLength = 0;
|
|
173
|
+
this.carriage = false;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
// An empty line after a line ending ends the block, which drops both.
|
|
177
|
+
const before = this.pieces.join("") + chunk.slice(start, i + 1);
|
|
178
|
+
const block = before.slice(0, before.length - (this.ending + ending));
|
|
179
|
+
this.pieces.length = 0;
|
|
180
|
+
this.length = 0;
|
|
181
|
+
this.ending = 0;
|
|
182
|
+
this.lineLength = 0;
|
|
183
|
+
this.carriage = false;
|
|
184
|
+
start = i + 1;
|
|
185
|
+
if (block.length > MAX_EVENT_BLOCK_CHARS) {
|
|
186
|
+
throw new Error(
|
|
187
|
+
`an event block is longer than ${MAX_EVENT_BLOCK_CHARS} characters`,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
onBlock(block);
|
|
191
|
+
}
|
|
192
|
+
if (start < chunk.length) {
|
|
193
|
+
this.pieces.push(chunk.slice(start));
|
|
194
|
+
this.length += chunk.length - start;
|
|
195
|
+
}
|
|
196
|
+
// A line ending that ends the tail may be the first half of a terminator
|
|
197
|
+
// split across chunks, so it does not count against the block's limit.
|
|
198
|
+
const possibleEnding = this.carriage
|
|
199
|
+
? this.lineLength === 0
|
|
200
|
+
? this.ending + 1
|
|
201
|
+
: 1
|
|
202
|
+
: this.lineLength === 0
|
|
203
|
+
? this.ending
|
|
204
|
+
: 0;
|
|
205
|
+
if (this.length - possibleEnding > MAX_EVENT_BLOCK_CHARS) {
|
|
206
|
+
throw new Error(
|
|
207
|
+
`an event block is longer than ${MAX_EVENT_BLOCK_CHARS} characters`,
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
104
213
|
/** Reads one SSE block; a comment-only block is undefined. */
|
|
105
214
|
function parseBlock(block: string): SseBlock | undefined {
|
|
106
215
|
let id: string | undefined;
|
|
@@ -284,16 +393,15 @@ export class TuiEvents implements AsyncIterableIterator<TuiEvent> {
|
|
|
284
393
|
|
|
285
394
|
const reader = response.body.getReader();
|
|
286
395
|
const decoder = new TextDecoder();
|
|
287
|
-
|
|
396
|
+
const tail = new BlockTail();
|
|
288
397
|
let delivered = false;
|
|
289
398
|
try {
|
|
290
399
|
while (!this.closed) {
|
|
291
400
|
const read = await reader.read();
|
|
292
401
|
if (read.done) break;
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
delivered = delivered || drained.delivered;
|
|
402
|
+
tail.push(decoder.decode(read.value, { stream: true }), (block) => {
|
|
403
|
+
if (this.handleBlock(block)) delivered = true;
|
|
404
|
+
});
|
|
297
405
|
}
|
|
298
406
|
} finally {
|
|
299
407
|
await reader.cancel().catch(() => undefined);
|
|
@@ -303,20 +411,6 @@ export class TuiEvents implements AsyncIterableIterator<TuiEvent> {
|
|
|
303
411
|
return delivered;
|
|
304
412
|
}
|
|
305
413
|
|
|
306
|
-
/** Consumes every complete block and returns the unfinished tail. */
|
|
307
|
-
private drain(buffer: string): { remaining: string; delivered: boolean } {
|
|
308
|
-
let remaining = buffer;
|
|
309
|
-
let delivered = false;
|
|
310
|
-
for (;;) {
|
|
311
|
-
const match = /\r?\n\r?\n/.exec(remaining);
|
|
312
|
-
if (match === null) break;
|
|
313
|
-
const block = remaining.slice(0, match.index);
|
|
314
|
-
remaining = remaining.slice(match.index + match[0].length);
|
|
315
|
-
if (this.handleBlock(block)) delivered = true;
|
|
316
|
-
}
|
|
317
|
-
return { remaining, delivered };
|
|
318
|
-
}
|
|
319
|
-
|
|
320
414
|
/** Handles one block; true when it yielded an event. */
|
|
321
415
|
private handleBlock(block: string): boolean {
|
|
322
416
|
const parsed = parseBlock(block);
|
package/src/index.ts
CHANGED
package/src/live.ts
CHANGED
|
@@ -10,6 +10,9 @@ import { sameObject, type TuiConnection } from "./state.ts";
|
|
|
10
10
|
/** At most one batch of refetches per this many milliseconds. */
|
|
11
11
|
export const LIVE_DEBOUNCE_MS = 250;
|
|
12
12
|
|
|
13
|
+
/** The lists refetch at most once per this many milliseconds, apart from rows. */
|
|
14
|
+
export const LIVE_LISTS_DEBOUNCE_MS = 1500;
|
|
15
|
+
|
|
13
16
|
/** What the shell needs from time: one timer that can be cancelled. */
|
|
14
17
|
export interface TuiClock {
|
|
15
18
|
/** Calls `run` once after `ms`; the returned function cancels it. */
|
|
@@ -73,36 +76,68 @@ export function mergeRefetch(a: Refetch, b: Refetch): Refetch {
|
|
|
73
76
|
};
|
|
74
77
|
}
|
|
75
78
|
|
|
79
|
+
function hasWork(targets: Refetch): boolean {
|
|
80
|
+
return targets.all || targets.lists || targets.rows.length > 0 || targets.object;
|
|
81
|
+
}
|
|
82
|
+
|
|
76
83
|
/**
|
|
77
84
|
* Collects targets and hands them over once per window. The first push opens
|
|
78
|
-
* the window; pushes inside it join the same batch.
|
|
85
|
+
* the window; pushes inside it join the same batch. The lists have a longer
|
|
86
|
+
* window of their own, so a stream of output does not fetch Pending and
|
|
87
|
+
* Running on every short batch; the changed row and the open object stay on
|
|
88
|
+
* the short one. A `reset` fetches everything on the short window and drops
|
|
89
|
+
* any list batch it already covers.
|
|
79
90
|
*/
|
|
80
91
|
export class LiveBatcher {
|
|
81
92
|
private batch: Refetch = NO_REFETCH;
|
|
93
|
+
private lists: Refetch = NO_REFETCH;
|
|
82
94
|
private cancel: (() => void) | undefined;
|
|
95
|
+
private listsCancel: (() => void) | undefined;
|
|
83
96
|
|
|
84
97
|
constructor(
|
|
85
98
|
private readonly clock: TuiClock,
|
|
86
99
|
private readonly flush: (targets: Refetch) => void,
|
|
87
100
|
private readonly windowMs = LIVE_DEBOUNCE_MS,
|
|
101
|
+
private readonly listsWindowMs = LIVE_LISTS_DEBOUNCE_MS,
|
|
88
102
|
) {}
|
|
89
103
|
|
|
90
104
|
push(targets: Refetch): void {
|
|
91
|
-
|
|
92
|
-
|
|
105
|
+
if (targets.lists) {
|
|
106
|
+
this.lists = mergeRefetch(this.lists, { ...NO_REFETCH, lists: true });
|
|
107
|
+
if (this.listsCancel === undefined) {
|
|
108
|
+
this.listsCancel = this.clock.after(() => {
|
|
109
|
+
const ready = this.lists;
|
|
110
|
+
this.lists = NO_REFETCH;
|
|
111
|
+
this.listsCancel = undefined;
|
|
112
|
+
if (ready.lists) this.flush(ready);
|
|
113
|
+
}, this.listsWindowMs);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
this.batch = mergeRefetch(this.batch, { ...targets, lists: false });
|
|
117
|
+
if (!hasWork(this.batch) || this.cancel !== undefined) return;
|
|
93
118
|
this.cancel = this.clock.after(() => {
|
|
94
119
|
const ready = this.batch;
|
|
95
120
|
this.batch = NO_REFETCH;
|
|
96
121
|
this.cancel = undefined;
|
|
122
|
+
// A full refresh already fetches the lists; their later batch would be a
|
|
123
|
+
// second read of the same lists.
|
|
124
|
+
if (ready.all) {
|
|
125
|
+
this.listsCancel?.();
|
|
126
|
+
this.listsCancel = undefined;
|
|
127
|
+
this.lists = NO_REFETCH;
|
|
128
|
+
}
|
|
97
129
|
this.flush(ready);
|
|
98
130
|
}, this.windowMs);
|
|
99
131
|
}
|
|
100
132
|
|
|
101
|
-
/** Drops the
|
|
133
|
+
/** Drops the batches and their timers. */
|
|
102
134
|
stop(): void {
|
|
103
135
|
this.cancel?.();
|
|
104
136
|
this.cancel = undefined;
|
|
105
137
|
this.batch = NO_REFETCH;
|
|
138
|
+
this.listsCancel?.();
|
|
139
|
+
this.listsCancel = undefined;
|
|
140
|
+
this.lists = NO_REFETCH;
|
|
106
141
|
}
|
|
107
142
|
}
|
|
108
143
|
|
package/src/markdown.ts
CHANGED
|
@@ -7,22 +7,11 @@
|
|
|
7
7
|
|
|
8
8
|
import type { TextChunk } from "@opentui/core";
|
|
9
9
|
import { toRgba, type TuiPalette } from "./theme.ts";
|
|
10
|
-
import {
|
|
11
|
-
layoutMarkdown,
|
|
12
|
-
type LayoutLine,
|
|
13
|
-
type LayoutRole,
|
|
14
|
-
} from "./layout.ts";
|
|
10
|
+
import type { LayoutLine, LayoutRole } from "./layout.ts";
|
|
15
11
|
|
|
16
12
|
/** The colours Markdown needs; the view's palette has them. */
|
|
17
13
|
export type MarkdownPalette = Pick<TuiPalette, "accent" | "run" | "error" | "add" | "remove" | "weak" | "text" | "code">;
|
|
18
14
|
|
|
19
|
-
/** One drawn line: its chunks, its plain text and whether it is fenced code. */
|
|
20
|
-
export interface TuiMarkdownLine {
|
|
21
|
-
readonly text: string;
|
|
22
|
-
readonly code: boolean;
|
|
23
|
-
readonly chunks: readonly TextChunk[];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
15
|
function colourFor(
|
|
27
16
|
role: LayoutRole,
|
|
28
17
|
palette: MarkdownPalette,
|
|
@@ -59,27 +48,3 @@ export function chunksOfLine(
|
|
|
59
48
|
});
|
|
60
49
|
}
|
|
61
50
|
|
|
62
|
-
/** Styles laid-out rows for the theme. */
|
|
63
|
-
export function styleLines(
|
|
64
|
-
lines: readonly LayoutLine[],
|
|
65
|
-
palette: MarkdownPalette,
|
|
66
|
-
): readonly TuiMarkdownLine[] {
|
|
67
|
-
return lines.map((line) => ({
|
|
68
|
-
text: line.text,
|
|
69
|
-
code: line.code,
|
|
70
|
-
chunks: chunksOfLine(line, palette),
|
|
71
|
-
}));
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Every row the object pane draws for one section, styled for the theme and
|
|
76
|
-
* wrapped to `width` when the pane knows it. The reducer counts these same
|
|
77
|
-
* wrapped rows, so its scroll and the drawn content cannot drift apart.
|
|
78
|
-
*/
|
|
79
|
-
export function markdownLines(
|
|
80
|
-
markdown: string,
|
|
81
|
-
palette: MarkdownPalette,
|
|
82
|
-
width?: number | undefined,
|
|
83
|
-
): readonly TuiMarkdownLine[] {
|
|
84
|
-
return styleLines(layoutMarkdown(markdown, width), palette);
|
|
85
|
-
}
|
package/src/recent.ts
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
* last. It is the TUI's, not the server's; a missing or broken file is empty.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { readFileSync } from "node:fs";
|
|
7
7
|
import { join } from "node:path";
|
|
8
|
+
import { writeJsonAtomicSync } from "@operad-io/engine/platform";
|
|
8
9
|
import { RECENT_ENTRIES } from "./actions.ts";
|
|
9
10
|
|
|
10
11
|
export function stateFile(home: string): string {
|
|
@@ -29,11 +30,7 @@ export function loadRecent(home: string): readonly string[] {
|
|
|
29
30
|
/** Writes the list; a failure is ignored, since losing it costs only a convenience. */
|
|
30
31
|
export function saveRecent(home: string, recent: readonly string[]): void {
|
|
31
32
|
try {
|
|
32
|
-
|
|
33
|
-
const file = stateFile(home);
|
|
34
|
-
const temporary = `${file}.${process.pid}.tmp`;
|
|
35
|
-
writeFileSync(temporary, `${JSON.stringify({ recent }, null, 2)}\n`);
|
|
36
|
-
renameSync(temporary, file);
|
|
33
|
+
writeJsonAtomicSync(stateFile(home), { recent }, { space: 2 });
|
|
37
34
|
} catch {
|
|
38
35
|
// Nothing to do: the next change writes again.
|
|
39
36
|
}
|