forked from pi-base/web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.ts
More file actions
135 lines (119 loc) · 3.13 KB
/
context.ts
File metadata and controls
135 lines (119 loc) · 3.13 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
export type { Context } from './context/types'
import { formula as F } from '@pi-base/core'
import { getContext, setContext } from 'svelte'
import { derived, type Readable } from 'svelte/store'
import type { Context } from '@/context/types'
import { trace } from '@/debug'
import * as Errors from '@/errors'
import type * as Gateway from '@/gateway'
import { Id } from '@/models'
import { renderer } from '@/parser'
import { local, type Local } from '@/repositories'
import { create, type Prestore, type Store } from '@/stores'
import { type Source } from '@/types'
import { subscribeUntil } from '@/util'
export type Config = {
showDevLink: boolean
}
// TODO: it seems like this shouldn't be necessary. Should the store more closely
// match the prestore (i.e. Readable<Theorem[]> instead of Readable<Theorems>)?
function project(store: Store) {
return {
...store,
properties: derived(store.properties, p => p.all),
spaces: derived(store.spaces, s => s.all),
theorems: derived(store.theorems, ts =>
ts.all.map(t => ({
...t,
when: F.mapProperty(p => p.id, t.when),
then: F.mapProperty(p => p.id, t.then),
})),
),
traits: derived(store.traits, t => t.all),
}
}
export function initialize({
db = local(),
errorHandler = Errors.log(),
source = {},
gateway,
showDev = false,
typesetter = renderer,
}: {
db?: Local<Prestore>
errorHandler?: Errors.Handler
source?: Partial<Source>
gateway: Gateway.Sync
showDev?: boolean
typesetter?: typeof renderer
}): Context {
const pre = db.load()
const store = create(
{
...pre,
source: {
host: source.host || pre.source.host,
branch: source.branch || pre.source.branch,
},
},
gateway,
)
db.subscribe(project(store))
if (
!pre.sync ||
new Date(pre.sync.at).getTime() < Date.now() - 7 * 24 * 60 * 60 * 1000
) {
store.sync.sync()
}
const typeset = derived(
[store.properties, store.spaces, store.theorems, store.traits],
([properties, spaces, theorems, traits]) => {
trace({ event: 'build_typesetter' })
return typesetter(properties, spaces, theorems, traits)
},
)
function loaded() {
return subscribeUntil(
store.sync,
state => state.kind === 'fetched' || state.kind === 'error',
)
}
function load<T, S>(
s: Readable<S>,
lookup: (state: S) => T | null,
until: Promise<unknown> = loaded(),
): Promise<T> {
return new Promise((resolve, reject) => {
var unsubscribe = s.subscribe(state => {
const found = lookup(state)
if (found) {
resolve(found)
unsubscribe && unsubscribe()
}
})
until.then(() => {
reject()
unsubscribe && unsubscribe()
})
})
}
function checked(spaceId: string) {
return store.deduction.checked(Id.toInt(spaceId))
}
return {
...store,
showDev,
errorHandler,
typeset,
loaded,
load,
checked,
}
}
const contextKey = {}
export function set(value: Context) {
setContext<Context>(contextKey, value)
}
export default function context() {
return getContext<Context>(contextKey)
}