Open History to see the last commit
1/**
2 * Things the reader does in the Notes app, as typed events. The guide
3 * listens to them: a section is marked done when its hands-on action happens
4 * (see `doneWhen` in `guide/content.tsx`), and the section that matches the
5 * last one lights up (see `lib/app-state.ts`).
6 *
7 * Mutations report up to three times: `applied` when the optimistic change
8 * shows up, then `confirmed` when the server accepted it or `rolled-back`
9 * when it didn't. Navigation actions only have `applied`.
10 */
11export type AppAction =
12 | { type: "folder.opened"; folderId: string; smart: boolean }
13 | { type: "note.opened"; noteId: string }
14 | { type: "note.created"; noteId: string; folderId: string }
15 | { type: "note.edited"; noteId: string }
16 | { type: "note.pinned"; noteId: string; pinned: boolean }
17 | { type: "note.moved"; noteId: string; folderId: string }
18 | { type: "note.deleted"; noteId: string; permanently: boolean }
19 | { type: "note.restored"; noteId: string }
20 | { type: "folder.renamed"; folderId: string; name: string }
21 | { type: "account.updated" }
22 | { type: "network.latency-set"; latencyMs: number }
23 | { type: "network.fail-next-set"; failNext: boolean };
24
25export type AppActionType = AppAction["type"];
26
27export type AppActionStatus = "applied" | "confirmed" | "rolled-back";
28
29export type AppActionEvent = AppAction & {
30 status: AppActionStatus;
31 /** Why the server rejected the change (`rolled-back` only). */
32 error?: Error;
33};
34
35/**
36 * Matches events by any of their fields, e.g. `{ type: "note.edited" }` or
37 * `{ status: "rolled-back" }`. All given fields must be equal.
38 */
39export type AppActionMatcher = {
40 [T in AppActionType]: Partial<Extract<AppAction, { type: T }>> & { type?: T };
41}[AppActionType] & { status?: AppActionStatus };
42
43export function matchesAppAction(matcher: AppActionMatcher, event: AppActionEvent): boolean {
44 return Object.entries(matcher).every(
45 ([key, value]) => (event as Record<string, unknown>)[key] === value,
46 );
47}
48
49type Listener = (event: AppActionEvent) => void;
50
51const listeners = new Set<Listener>();
52
53export function emitAppAction(event: AppActionEvent) {
54 for (const listener of listeners) listener(event);
55}
56
57export function subscribeToAppActions(listener: Listener): () => void {
58 listeners.add(listener);
59 return () => listeners.delete(listener);
60}
61
62/** Reports something that doesn't go through the server, like opening a folder. */
63export function reportAction(action: AppAction) {
64 emitAppAction({ ...action, status: "applied" });
65}
66
67/**
68 * Reports a mutation: `applied` now, then `confirmed` or `rolled-back` once
69 * its transaction settles. One transaction can carry several actions (a
70 * paced mutation merges an edit and a pin made close together); each action
71 * is reported once per transaction, however often it repeats (keystrokes).
72 * A transaction the app cancelled itself isn't reported as rolled back.
73 */
74export function reportMutation(
75 action: AppAction,
76 transaction: { when: (state: "settled") => Promise<unknown> },
77) {
78 emitAppAction({ ...action, status: "applied" });
79 const reported = reportedActions.get(transaction) ?? new Set<string>();
80 reportedActions.set(transaction, reported);
81 const key = JSON.stringify(action);
82 if (reported.has(key)) return;
83 reported.add(key);
84 transaction.when("settled").then(
85 () => emitAppAction({ ...action, status: "confirmed" }),
86 (error: unknown) => {
87 // A rollback without an error is a cancellation, not a rejection.
88 if (error === undefined) return;
89 emitAppAction({
90 ...action,
91 status: "rolled-back",
92 error: error instanceof Error ? error : new Error("The change was rolled back."),
93 });
94 },
95 );
96}
97
98const reportedActions = new WeakMap<object, Set<string>>();