Settings as clean architecture
This commit is contained in:
parent
0652131de8
commit
89c28d67fd
11 changed files with 176 additions and 33 deletions
|
@ -1,20 +0,0 @@
|
||||||
import actions from '../actions';
|
|
||||||
import * as settingsStorage from 'shared/settings/storage';
|
|
||||||
|
|
||||||
const load = async() => {
|
|
||||||
let value = await settingsStorage.loadValue();
|
|
||||||
return {
|
|
||||||
type: actions.SETTING_SET_SETTINGS,
|
|
||||||
value,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const setProperty = (name, value) => {
|
|
||||||
return {
|
|
||||||
type: actions.SETTING_SET_PROPERTY,
|
|
||||||
name,
|
|
||||||
value,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export { load, setProperty };
|
|
|
@ -1,6 +1,5 @@
|
||||||
import messages from 'shared/messages';
|
import messages from 'shared/messages';
|
||||||
import * as commandActions from 'background/actions/command';
|
import * as commandActions from 'background/actions/command';
|
||||||
import * as settingActions from 'background/actions/setting';
|
|
||||||
import * as findActions from 'background/actions/find';
|
import * as findActions from 'background/actions/find';
|
||||||
import * as tabActions from 'background/actions/tab';
|
import * as tabActions from 'background/actions/tab';
|
||||||
|
|
||||||
|
@ -38,11 +37,6 @@ export default class BackgroundComponent {
|
||||||
commandActions.exec(sender.tab, message.text, settings.value),
|
commandActions.exec(sender.tab, message.text, settings.value),
|
||||||
);
|
);
|
||||||
return this.broadcastSettingsChanged();
|
return this.broadcastSettingsChanged();
|
||||||
case messages.SETTINGS_QUERY:
|
|
||||||
return Promise.resolve(this.store.getState().setting.value);
|
|
||||||
case messages.SETTINGS_RELOAD:
|
|
||||||
this.store.dispatch(settingActions.load());
|
|
||||||
return this.broadcastSettingsChanged();
|
|
||||||
case messages.FIND_GET_KEYWORD:
|
case messages.FIND_GET_KEYWORD:
|
||||||
return Promise.resolve(find.keyword);
|
return Promise.resolve(find.keyword);
|
||||||
case messages.FIND_SET_KEYWORD:
|
case messages.FIND_SET_KEYWORD:
|
||||||
|
|
18
src/background/controllers/setting.js
Normal file
18
src/background/controllers/setting.js
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import SettingInteractor from '../usecases/setting';
|
||||||
|
import ContentMessageClient from '../infrastructures/content-message-client';
|
||||||
|
|
||||||
|
export default class SettingController {
|
||||||
|
constructor() {
|
||||||
|
this.settingInteractor = new SettingInteractor();
|
||||||
|
this.contentMessageClient = new ContentMessageClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
getSetting() {
|
||||||
|
return this.settingInteractor.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
async reload() {
|
||||||
|
await this.settingInteractor.reload();
|
||||||
|
this.contentMessageClient.broadcastSettingsChanged();
|
||||||
|
}
|
||||||
|
}
|
51
src/background/domains/setting.js
Normal file
51
src/background/domains/setting.js
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
import DefaultSettings from '../../shared/settings/default';
|
||||||
|
import * as settingsValues from '../../shared/settings/values';
|
||||||
|
|
||||||
|
export default class Setting {
|
||||||
|
constructor({ source, json, form }) {
|
||||||
|
this.obj = {
|
||||||
|
source, json, form
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
get source() {
|
||||||
|
return this.obj.source;
|
||||||
|
}
|
||||||
|
|
||||||
|
get json() {
|
||||||
|
return this.obj.json;
|
||||||
|
}
|
||||||
|
|
||||||
|
get form() {
|
||||||
|
return this.obj.form;
|
||||||
|
}
|
||||||
|
|
||||||
|
value() {
|
||||||
|
let value = JSON.parse(DefaultSettings.json);
|
||||||
|
if (this.obj.source === 'json') {
|
||||||
|
value = settingsValues.valueFromJson(this.obj.json);
|
||||||
|
} else if (this.obj.source === 'form') {
|
||||||
|
value = settingsValues.valueFromForm(this.obj.form);
|
||||||
|
}
|
||||||
|
if (!value.properties) {
|
||||||
|
value.properties = {};
|
||||||
|
}
|
||||||
|
return { ...settingsValues.valueFromJson(DefaultSettings.json), ...value };
|
||||||
|
}
|
||||||
|
|
||||||
|
serialize() {
|
||||||
|
return this.obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
static deserialize(obj) {
|
||||||
|
return new Setting({ source: obj.source, json: obj.json, form: obj.form });
|
||||||
|
}
|
||||||
|
|
||||||
|
static defaultSettings() {
|
||||||
|
return new Setting({
|
||||||
|
source: DefaultSettings.source,
|
||||||
|
json: DefaultSettings.json,
|
||||||
|
form: {},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,3 @@
|
||||||
import * as settingActions from 'background/actions/setting';
|
|
||||||
import BackgroundComponent from 'background/components/background';
|
import BackgroundComponent from 'background/components/background';
|
||||||
import OperationComponent from 'background/components/operation';
|
import OperationComponent from 'background/components/operation';
|
||||||
import TabComponent from 'background/components/tab';
|
import TabComponent from 'background/components/tab';
|
||||||
|
@ -9,6 +8,7 @@ import promise from 'redux-promise';
|
||||||
import * as versions from './shared/versions';
|
import * as versions from './shared/versions';
|
||||||
|
|
||||||
import ContentMessageListener from './infrastructures/content-message-listener';
|
import ContentMessageListener from './infrastructures/content-message-listener';
|
||||||
|
import SettingController from './controllers/setting';
|
||||||
|
|
||||||
const store = createStore(
|
const store = createStore(
|
||||||
reducers,
|
reducers,
|
||||||
|
@ -31,8 +31,8 @@ const tabComponent = new TabComponent(store);
|
||||||
const indicatorComponent = new IndicatorComponent(store);
|
const indicatorComponent = new IndicatorComponent(store);
|
||||||
/* eslint-enable no-unused-vars */
|
/* eslint-enable no-unused-vars */
|
||||||
|
|
||||||
store.dispatch(settingActions.load());
|
|
||||||
|
|
||||||
checkAndNotifyUpdated();
|
checkAndNotifyUpdated();
|
||||||
|
|
||||||
|
new SettingController().reload();
|
||||||
|
|
||||||
new ContentMessageListener().run();
|
new ContentMessageListener().run();
|
||||||
|
|
12
src/background/infrastructures/content-message-client.js
Normal file
12
src/background/infrastructures/content-message-client.js
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import messages from '../../shared/messages';
|
||||||
|
|
||||||
|
export default class ContentMessageClient {
|
||||||
|
async broadcastSettingsChanged() {
|
||||||
|
let tabs = await browser.tabs.query({});
|
||||||
|
for (let tab of tabs) {
|
||||||
|
browser.tabs.sendMessage(tab.id, {
|
||||||
|
type: messages.SETTINGS_CHANGED,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,10 @@
|
||||||
import messages from '../../shared/messages';
|
import messages from '../../shared/messages';
|
||||||
import CompletionsController from '../controllers/completions';
|
import CompletionsController from '../controllers/completions';
|
||||||
|
import SettingController from '../controllers/setting';
|
||||||
|
|
||||||
export default class ContentMessageListener {
|
export default class ContentMessageListener {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
this.settingController = new SettingController();
|
||||||
this.completionsController = new CompletionsController();
|
this.completionsController = new CompletionsController();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,13 +24,24 @@ export default class ContentMessageListener {
|
||||||
onMessage(message) {
|
onMessage(message) {
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
case messages.CONSOLE_QUERY_COMPLETIONS:
|
case messages.CONSOLE_QUERY_COMPLETIONS:
|
||||||
return this.onConsoleQueryCompletions(message);
|
return this.onConsoleQueryCompletions(message.text);
|
||||||
|
case messages.SETTINGS_QUERY:
|
||||||
|
return this.onSettingsQuery();
|
||||||
|
case messages.SETTINGS_RELOAD:
|
||||||
|
return this.onSettingsReload();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async onConsoleQueryCompletions(message) {
|
async onConsoleQueryCompletions(line) {
|
||||||
let completions =
|
let completions = await this.completionsController.getCompletions(line);
|
||||||
await this.completionsController.getCompletions(message.text);
|
|
||||||
return Promise.resolve(completions.serialize());
|
return Promise.resolve(completions.serialize());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onSettingsQuery() {
|
||||||
|
return this.settingController.getSetting();
|
||||||
|
}
|
||||||
|
|
||||||
|
onSettingsReload() {
|
||||||
|
return this.settingController.reload();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
11
src/background/infrastructures/memory-storage.js
Normal file
11
src/background/infrastructures/memory-storage.js
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
const db = {};
|
||||||
|
|
||||||
|
export default class MemoryStorage {
|
||||||
|
set(name, value) {
|
||||||
|
db[name] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
get(name) {
|
||||||
|
return db[name];
|
||||||
|
}
|
||||||
|
}
|
16
src/background/repositories/persistent-setting.js
Normal file
16
src/background/repositories/persistent-setting.js
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import Setting from '../domains/setting';
|
||||||
|
|
||||||
|
export default class SettingRepository {
|
||||||
|
save(settings) {
|
||||||
|
return browser.storage.local.set({ settings: settings.serialize() });
|
||||||
|
}
|
||||||
|
|
||||||
|
async load() {
|
||||||
|
let { settings } = await browser.storage.local.get('settings');
|
||||||
|
if (!settings) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return Setting.deserialize(settings);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
17
src/background/repositories/setting.js
Normal file
17
src/background/repositories/setting.js
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import MemoryStorage from '../infrastructures/memory-storage';
|
||||||
|
|
||||||
|
const CACHED_SETTING_KEY = 'setting';
|
||||||
|
|
||||||
|
export default class SettingRepository {
|
||||||
|
constructor() {
|
||||||
|
this.cache = new MemoryStorage();
|
||||||
|
}
|
||||||
|
|
||||||
|
get() {
|
||||||
|
return Promise.resolve(this.cache.get(CACHED_SETTING_KEY));
|
||||||
|
}
|
||||||
|
|
||||||
|
update(value) {
|
||||||
|
return this.cache.set(CACHED_SETTING_KEY, value);
|
||||||
|
}
|
||||||
|
}
|
31
src/background/usecases/setting.js
Normal file
31
src/background/usecases/setting.js
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
import Setting from '../domains/setting';
|
||||||
|
import PersistentSettingRepository from '../repositories/persistent-setting';
|
||||||
|
import SettingRepository from '../repositories/setting';
|
||||||
|
|
||||||
|
export default class SettingInteractor {
|
||||||
|
constructor() {
|
||||||
|
this.persistentSettingRepository = new PersistentSettingRepository();
|
||||||
|
this.settingRepository = new SettingRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
save(settings) {
|
||||||
|
this.persistentSettingRepository.save(settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
get() {
|
||||||
|
return this.settingRepository.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
async reload() {
|
||||||
|
let settings = await this.persistentSettingRepository.load();
|
||||||
|
if (!settings) {
|
||||||
|
settings = Setting.defaultSettings();
|
||||||
|
}
|
||||||
|
|
||||||
|
let value = settings.value();
|
||||||
|
|
||||||
|
this.settingRepository.update(value);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue