Settings as clean architecture

jh-changes
Shin'ya Ueoka 6 years ago
parent 0652131de8
commit 89c28d67fd
  1. 20
      src/background/actions/setting.js
  2. 6
      src/background/components/background.js
  3. 18
      src/background/controllers/setting.js
  4. 51
      src/background/domains/setting.js
  5. 6
      src/background/index.js
  6. 12
      src/background/infrastructures/content-message-client.js
  7. 21
      src/background/infrastructures/content-message-listener.js
  8. 11
      src/background/infrastructures/memory-storage.js
  9. 16
      src/background/repositories/persistent-setting.js
  10. 17
      src/background/repositories/setting.js
  11. 31
      src/background/usecases/setting.js

@ -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 * as commandActions from 'background/actions/command';
import * as settingActions from 'background/actions/setting';
import * as findActions from 'background/actions/find';
import * as tabActions from 'background/actions/tab';
@ -38,11 +37,6 @@ export default class BackgroundComponent {
commandActions.exec(sender.tab, message.text, settings.value),
);
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:
return Promise.resolve(find.keyword);
case messages.FIND_SET_KEYWORD:

@ -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();
}
}

@ -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 OperationComponent from 'background/components/operation';
import TabComponent from 'background/components/tab';
@ -9,6 +8,7 @@ import promise from 'redux-promise';
import * as versions from './shared/versions';
import ContentMessageListener from './infrastructures/content-message-listener';
import SettingController from './controllers/setting';
const store = createStore(
reducers,
@ -31,8 +31,8 @@ const tabComponent = new TabComponent(store);
const indicatorComponent = new IndicatorComponent(store);
/* eslint-enable no-unused-vars */
store.dispatch(settingActions.load());
checkAndNotifyUpdated();
new SettingController().reload();
new ContentMessageListener().run();

@ -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 CompletionsController from '../controllers/completions';
import SettingController from '../controllers/setting';
export default class ContentMessageListener {
constructor() {
this.settingController = new SettingController();
this.completionsController = new CompletionsController();
}
@ -22,13 +24,24 @@ export default class ContentMessageListener {
onMessage(message) {
switch (message.type) {
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) {
let completions =
await this.completionsController.getCompletions(message.text);
async onConsoleQueryCompletions(line) {
let completions = await this.completionsController.getCompletions(line);
return Promise.resolve(completions.serialize());
}
onSettingsQuery() {
return this.settingController.getSetting();
}
onSettingsReload() {
return this.settingController.reload();
}
}

@ -0,0 +1,11 @@
const db = {};
export default class MemoryStorage {
set(name, value) {
db[name] = value;
}
get(name) {
return db[name];
}
}

@ -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);
}
}

@ -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);
}
}

@ -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;
}
}