blob: 2f159e55f4f3413bd22d796643be76e8557c01c3 (
plain) (
blame)
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
|
import { injectable } from 'tsyringe';
import MemoryStorage from '../infrastructures/MemoryStorage';
import Settings from '../../shared/Settings';
import * as PropertyDefs from '../../shared/property-defs';
const CACHED_SETTING_KEY = 'setting';
@injectable()
export default class SettingRepository {
private cache: MemoryStorage;
constructor() {
this.cache = new MemoryStorage();
}
get(): Promise<Settings> {
return Promise.resolve(this.cache.get(CACHED_SETTING_KEY));
}
update(value: Settings): void {
return this.cache.set(CACHED_SETTING_KEY, value);
}
async setProperty(
name: string, value: string | number | boolean,
): Promise<void> {
let def = PropertyDefs.defs.find(d => name === d.name);
if (!def) {
throw new Error('unknown property: ' + name);
}
if (typeof value !== def.type) {
throw new TypeError(`property type of ${name} mismatch: ${typeof value}`);
}
let newValue = value;
if (typeof value === 'string' && value === '') {
newValue = def.defaultValue;
}
let current = await this.get();
switch (name) {
case 'hintchars':
current.properties.hintchars = newValue as string;
break;
case 'smoothscroll':
current.properties.smoothscroll = newValue as boolean;
break;
case 'complete':
current.properties.complete = newValue as string;
break;
}
return this.update(current);
}
}
|