Skip to content
Source
Sign inSign up
fuko/trytanstackdb-v2
Open History to see the last commit

app-actions.ts

96 lines3.5 KB

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 phase 4 highlights sections
5 * from them.
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​
23export type AppActionType = AppAction["type"];
24​
25export type AppActionStatus = "applied" | "confirmed" | "rolled-back";
26​
27export type AppActionEvent = AppAction & {
28 status: AppActionStatus;
29 /** Why the server rejected the change (`rolled-back` only). */
30 error?: Error;
31};
32​
33/**
34 * Matches events by any of their fields, e.g. `{ type: "note.edited" }` or
35 * `{ status: "rolled-back" }`. All given fields must be equal.
36 */
37export type AppActionMatcher = {
38 [T in AppActionType]: Partial<Extract<AppAction, { type: T }>> & { type?: T };
39}[AppActionType] & { status?: AppActionStatus };
40​
41export function matchesAppAction(matcher: AppActionMatcher, event: AppActionEvent): boolean {
42 return Object.entries(matcher).every(
43 ([key, value]) => (event as Record<string, unknown>)[key] === value,
44 );
46​
47type Listener = (event: AppActionEvent) => void;
48​
49const listeners = new Set<Listener>();
50​
51export function emitAppAction(event: AppActionEvent) {
52 for (const listener of listeners) listener(event);
54​
55export function subscribeToAppActions(listener: Listener): () => void {
56 listeners.add(listener);
57 return () => listeners.delete(listener);
59​
60/** Reports something that doesn't go through the server, like opening a folder. */
61export function reportAction(action: AppAction) {
62 emitAppAction({ ...action, status: "applied" });
64​
65/**
66 * Reports a mutation: `applied` now, then `confirmed` or `rolled-back` once
67 * its transaction settles. One transaction can carry several actions (a
68 * paced mutation merges an edit and a pin made close together); each action
69 * is reported once per transaction, however often it repeats (keystrokes).
70 * A transaction the app cancelled itself isn't reported as rolled back.
71 */
72export function reportMutation(
73 action: AppAction,
74 transaction: { when: (state: "settled") => Promise<unknown> },
75) {
76 emitAppAction({ ...action, status: "applied" });
77 const reported = reportedActions.get(transaction) ?? new Set<string>();
78 reportedActions.set(transaction, reported);
79 const key = JSON.stringify(action);
80 if (reported.has(key)) return;
81 reported.add(key);
82 transaction.when("settled").then(
83 () => emitAppAction({ ...action, status: "confirmed" }),
84 (error: unknown) => {
85 // A rollback without an error is a cancellation, not a rejection.
86 if (error === undefined) return;
87 emitAppAction({
88 ...action,
89 status: "rolled-back",
90 error: error instanceof Error ? error : new Error("The change was rolled back."),
91 });
92 },
93 );
95​
96const reportedActions = new WeakMap<object, Set<string>>();