File size: 1,859 Bytes
070c931
1aa4a16
070c931
 
 
 
 
 
 
 
 
 
 
 
 
 
db8dd62
070c931
 
 
 
 
 
 
 
 
 
 
 
 
 
db8dd62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
070c931
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { notebooksApi } from "../../api/endpoints";
import type { Doc, NotebookSummary } from "../../api/types";

/** The video wizard's step 1 lists, kept for the browser session (until a full reload) so re-opening the wizard
 * shows them at once. Readers show what is cached, then refresh in the background through the loaders below. */
export const sourceCache = {
  notebooks: null as NotebookSummary[] | null,
  archiveId: null as string | null,
  docs: new Map<string, Doc[]>(),
};

/** Forget everything, so one account never sees another's posts (called on log in and log out). */
export function clearSourceCache(): void {
  sourceCache.notebooks = null;
  sourceCache.archiveId = null;
  sourceCache.docs.clear();
  seededAt.clear();
}

export async function loadNotebooks(): Promise<NotebookSummary[]> {
  const n = await notebooksApi.list();
  sourceCache.notebooks = n;
  return n;
}

export async function loadArchiveId(): Promise<string> {
  const nb = await notebooksApi.archive();
  sourceCache.archiveId = nb.id;
  return nb.id;
}

/** A page that already holds a notebook's current posts (the notebook page) hands them over, so the video wizard
 * shows them without loading them again. For a minute SourcePicker trusts them and skips its own refresh. */
const seededAt = new Map<string, number>();
const SEED_TRUST_MS = 60_000;

export function seedDocs(notebookId: string, docs: Doc[]): void {
  sourceCache.docs.set(notebookId, docs);
  seededAt.set(notebookId, Date.now());
}

export function freshlySeeded(notebookId: string): boolean {
  const at = seededAt.get(notebookId);
  return at !== undefined && Date.now() - at < SEED_TRUST_MS;
}

export async function loadDocs(notebookId: string): Promise<Doc[]> {
  const nb = await notebooksApi.get(notebookId);
  sourceCache.docs.set(notebookId, nb.documents);
  return nb.documents;
}