aboutsummaryrefslogtreecommitdiff
path: root/src/background/repositories/CachedSettingRepository.ts
blob: b4cdd1c04a913328c5cff73bacbeda50285de096 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import MemoryStorage from "../infrastructures/MemoryStorage";
import Settings from "../../shared/settings/Settings";
import Properties from "../../shared/settings/Properties";
import ColorScheme from "../../shared/ColorScheme";

const CACHED_SETTING_KEY = "setting";

export default interface CachedSettingRepository {
  get(): Promise<Settings>;

  update(value: Settings): Promise<void>;

  setProperty(name: string, value: string | number | boolean): Promise<void>;
}

export class CachedSettingRepositoryImpl implements CachedSettingRepository {
  private cache: MemoryStorage;

  constructor() {
    this.cache = new MemoryStorage();
  }

  get(): Promise<Settings> {
    const data = this.cache.get(CACHED_SETTING_KEY);
    return Promise.resolve(Settings.fromJSON(data));
  }

  update(value: Settings): Promise<void> {
    this.cache.set(CACHED_SETTING_KEY, value.toJSON());
    return Promise.resolve();
  }

  async setProperty(
    name: string,
    value: string | number | boolean
  ): Promise<void> {
    const def = Properties.def(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;
    }

    const 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;
      case "colorscheme": {
        switch (newValue) {
          case ColorScheme.Light:
          case ColorScheme.Dark:
          case ColorScheme.System:
            current.properties.colorscheme = newValue as ColorScheme;
            break;
          default:
            throw new Error(`Unsupported colorscheme: ${newValue}`);
        }
        break;
      }
    }
    await this.update(current);
  }
}