Repository navigation
Expand file tree
/
Copy pathlibrary.ts
More file actions
61 lines (61 loc) · 1.53 KB
/
Copy pathlibrary.ts
File metadata and controls
61 lines (61 loc) · 1.53 KB
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
53
54
55
56
57
58
59
60
61
import type { ExecutionContext } from '@latch-local/contracts';
export const articles = [
{
id: 'start',
title: 'Getting started',
body: 'Install the local packages and initialize a contract.',
},
{
id: 'state',
title: 'Shared state',
body: 'Connect React callbacks to existing application state.',
},
{
id: 'tests',
title: 'Regression tests',
body: 'Verify handlers and visible state after a change.',
},
];
export interface DocState {
query: string;
matches: typeof articles;
openedId: string;
}
let state: DocState = { query: '', matches: articles, openedId: '' };
const listeners = new Set<() => void>();
function commit(next: DocState) {
state = next;
listeners.forEach((l) => l());
return state;
}
export const subscribe = (l: () => void) => {
listeners.add(l);
return () => {
listeners.delete(l);
};
};
export function readDocs() {
return state;
}
export async function searchDocs(
{ text }: { text?: string },
context?: ExecutionContext,
): Promise<DocState> {
context?.assertActive();
const query = text ?? state.query;
return commit({
query,
matches: articles.filter((d) =>
(d.title + ' ' + d.body).toLowerCase().includes(query.toLowerCase()),
),
openedId: '',
});
}
export async function openDoc(
{ id }: { id: string },
context?: ExecutionContext,
): Promise<DocState> {
context?.assertActive();
if (!state.matches.some((d) => d.id === id)) throw Error('Document is not in visible matches');
return commit({ ...state, openedId: id });
}