blob: fccb09de6c8c2ae8d326addc959f49206028aa35 (
plain)
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
|
import { browser } from '$app/environment';
import type { Infer } from 'superstruct';
import { assert, object, optional, number, string } from 'superstruct';
const Config = object({
active_project: optional(string())
});
type Config = Infer<typeof Config>;
const CachedConfig = object({
config: Config,
expires: number()
});
type CachedConfig = Infer<typeof CachedConfig>;
const CACHE_TTL = 10 * 60 * 60 * 1000;
async function get_config(): Promise<Config> {
// Cache, might be outdated but saves on call to server
if (browser) {
try {
// TODO: Use async localStorage
const stored = localStorage.getItem('config');
if (stored !== null) {
const cached: CachedConfig = JSON.parse(stored);
assert(cached, CachedConfig);
if (cached.expires < Date.now()) {
// TODO: Should we update expires here?
// If page is in use we probably don't need to sync for a while.
return cached.config;
}
}
} catch {
// ignore errors
}
}
// Default config
// eslint-disable-next-line prefer-const
let config: Config = { active_project: undefined };
// TODO: Fetch config
if (browser) {
const cached: CachedConfig = { config: config, expires: Date.now() + CACHE_TTL };
// TODO: Use async localStorage
localStorage.setItem('config', JSON.stringify(cached));
}
return config;
}
export { type Config, get_config };
|