aboutsummaryrefslogtreecommitdiff
path: root/src/background/usecases
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-05-05 08:03:29 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2019-05-06 22:17:18 +0900
commita0882bbceb7ed71d56bf8557620449fbc3f19749 (patch)
treef2087d44f21dd68fc3584f62cfb9b62ae58bab2b /src/background/usecases
parentd01db82c0dca352de2d7644c383d388fc3ec0366 (diff)
Declare setting types
Diffstat (limited to 'src/background/usecases')
-rw-r--r--src/background/usecases/CommandUseCase.ts7
-rw-r--r--src/background/usecases/CompletionsUseCase.ts30
-rw-r--r--src/background/usecases/SettingUseCase.ts17
-rw-r--r--src/background/usecases/parsers.ts21
4 files changed, 37 insertions, 38 deletions
diff --git a/src/background/usecases/CommandUseCase.ts b/src/background/usecases/CommandUseCase.ts
index e0e3ada..2247d7b 100644
--- a/src/background/usecases/CommandUseCase.ts
+++ b/src/background/usecases/CommandUseCase.ts
@@ -6,7 +6,6 @@ import SettingRepository from '../repositories/SettingRepository';
import BookmarkRepository from '../repositories/BookmarkRepository';
import ConsoleClient from '../infrastructures/ConsoleClient';
import ContentMessageClient from '../infrastructures/ContentMessageClient';
-import * as properties from '../../shared/settings/properties';
export default class CommandIndicator {
private tabPresenter: TabPresenter;
@@ -115,16 +114,16 @@ export default class CommandIndicator {
async addbookmark(title: string): Promise<any> {
let tab = await this.tabPresenter.getCurrent();
- let item = await this.bookmarkRepository.create(title, tab.url);
+ let item = await this.bookmarkRepository.create(title, tab.url as string);
let message = 'Saved current page: ' + item.url;
- return this.consoleClient.showInfo(tab.id, message);
+ return this.consoleClient.showInfo(tab.id as number, message);
}
async set(keywords: string): Promise<any> {
if (keywords.length === 0) {
return;
}
- let [name, value] = parsers.parseSetOption(keywords, properties.types);
+ let [name, value] = parsers.parseSetOption(keywords);
await this.settingRepository.setProperty(name, value);
return this.contentMessageClient.broadcastSettingsChanged();
diff --git a/src/background/usecases/CompletionsUseCase.ts b/src/background/usecases/CompletionsUseCase.ts
index f3a808b..ae1ceed 100644
--- a/src/background/usecases/CompletionsUseCase.ts
+++ b/src/background/usecases/CompletionsUseCase.ts
@@ -4,7 +4,7 @@ import CompletionsRepository from '../repositories/CompletionsRepository';
import * as filters from './filters';
import SettingRepository from '../repositories/SettingRepository';
import TabPresenter from '../presenters/TabPresenter';
-import * as properties from '../../shared/settings/properties';
+import * as PropertyDefs from '../../shared/property-defs';
const COMPLETION_ITEM_LIMIT = 10;
@@ -44,7 +44,7 @@ export default class CompletionsUseCase {
let settings = await this.settingRepository.get();
let groups: CompletionGroup[] = [];
- let complete = settings.properties.complete || properties.defaults.complete;
+ let complete = settings.properties.complete;
for (let c of complete) {
if (c === 's') {
// eslint-disable-next-line no-await-in-loop
@@ -127,25 +127,25 @@ export default class CompletionsUseCase {
}
querySet(name: string, keywords: string): Promise<CompletionGroup[]> {
- let items = Object.keys(properties.docs).map((key) => {
- if (properties.types[key] === 'boolean') {
+ let items = PropertyDefs.defs.map((def) => {
+ if (def.type === 'boolean') {
return [
{
- caption: key,
- content: name + ' ' + key,
- url: 'Enable ' + properties.docs[key],
+ caption: def.name,
+ content: name + ' ' + def.name,
+ url: 'Enable ' + def.description,
}, {
- caption: 'no' + key,
- content: name + ' no' + key,
- url: 'Disable ' + properties.docs[key],
+ caption: 'no' + def.name,
+ content: name + ' no' + def.name,
+ url: 'Disable ' + def.description
}
];
}
return [
{
- caption: key,
- content: name + ' ' + key,
- url: 'Set ' + properties.docs[key],
+ caption: def.name,
+ content: name + ' ' + def.name,
+ url: 'Set ' + def.description,
}
];
});
@@ -195,8 +195,8 @@ export default class CompletionsUseCase {
.map(filters.filterByTailingSlash)
.map(pages => filters.filterByPathname(pages, COMPLETION_ITEM_LIMIT))
.map(pages => filters.filterByOrigin(pages, COMPLETION_ITEM_LIMIT))[0]
- .sort((x: HistoryItem, y: HistoryItem) => {
- return Number(x.visitCount) < Number(y.visitCount);
+ .sort((x: HistoryItem, y: HistoryItem): number => {
+ return Number(x.visitCount) - Number(y.visitCount);
})
.slice(0, COMPLETION_ITEM_LIMIT);
return histories.map(page => ({
diff --git a/src/background/usecases/SettingUseCase.ts b/src/background/usecases/SettingUseCase.ts
index b66ce02..aa3b534 100644
--- a/src/background/usecases/SettingUseCase.ts
+++ b/src/background/usecases/SettingUseCase.ts
@@ -1,7 +1,8 @@
-import Setting from '../domains/Setting';
// eslint-disable-next-line max-len
import PersistentSettingRepository from '../repositories/PersistentSettingRepository';
import SettingRepository from '../repositories/SettingRepository';
+import { DefaultSettingData } from '../../shared/SettingData';
+import Settings from '../../shared/Settings';
export default class SettingUseCase {
private persistentSettingRepository: PersistentSettingRepository;
@@ -13,20 +14,18 @@ export default class SettingUseCase {
this.settingRepository = new SettingRepository();
}
- get(): Promise<any> {
+ get(): Promise<Settings> {
return this.settingRepository.get();
}
- async reload(): Promise<any> {
- let settings = await this.persistentSettingRepository.load();
- if (!settings) {
- settings = Setting.defaultSettings();
+ async reload(): Promise<Settings> {
+ let data = await this.persistentSettingRepository.load();
+ if (!data) {
+ data = DefaultSettingData;
}
- let value = settings.value();
-
+ let value = data.toSettings();
this.settingRepository.update(value);
-
return value;
}
}
diff --git a/src/background/usecases/parsers.ts b/src/background/usecases/parsers.ts
index 3616ac3..6135fd8 100644
--- a/src/background/usecases/parsers.ts
+++ b/src/background/usecases/parsers.ts
@@ -1,3 +1,5 @@
+import * as PropertyDefs from '../../shared//property-defs';
+
const mustNumber = (v: any): number => {
let num = Number(v);
if (isNaN(num)) {
@@ -7,29 +9,28 @@ const mustNumber = (v: any): number => {
};
const parseSetOption = (
- word: string,
- types: { [key: string]: string },
+ args: string,
): any[] => {
- let [key, value]: any[] = word.split('=');
+ let [key, value]: any[] = args.split('=');
if (value === undefined) {
value = !key.startsWith('no');
key = value ? key : key.slice(2);
}
- let type = types[key];
- if (!type) {
+ let def = PropertyDefs.defs.find(d => d.name === key);
+ if (!def) {
throw new Error('Unknown property: ' + key);
}
- if (type === 'boolean' && typeof value !== 'boolean' ||
- type !== 'boolean' && typeof value === 'boolean') {
- throw new Error('Invalid argument: ' + word);
+ if (def.type === 'boolean' && typeof value !== 'boolean' ||
+ def.type !== 'boolean' && typeof value === 'boolean') {
+ throw new Error('Invalid argument: ' + args);
}
- switch (type) {
+ switch (def.type) {
case 'string': return [key, value];
case 'number': return [key, mustNumber(value)];
case 'boolean': return [key, value];
}
- throw new Error('Unknown property type: ' + type);
+ throw new Error('Unknown property type: ' + def.type);
};
export { parseSetOption };