aboutsummaryrefslogtreecommitdiff
path: root/src/background/repositories/CachedSettingRepository.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/background/repositories/CachedSettingRepository.ts')
-rw-r--r--src/background/repositories/CachedSettingRepository.ts54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/background/repositories/CachedSettingRepository.ts b/src/background/repositories/CachedSettingRepository.ts
new file mode 100644
index 0000000..b73d94a
--- /dev/null
+++ b/src/background/repositories/CachedSettingRepository.ts
@@ -0,0 +1,54 @@
+import { injectable } from 'tsyringe';
+import MemoryStorage from '../infrastructures/MemoryStorage';
+import Settings from '../../shared/settings/Settings';
+import Properties from '../../shared/settings/Properties';
+
+const CACHED_SETTING_KEY = 'setting';
+
+@injectable()
+export default class 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): void {
+ return this.cache.set(CACHED_SETTING_KEY, value.toJSON());
+ }
+
+ 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;
+ }
+ return this.update(current);
+ }
+}