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 }; |
@ -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: {}, |
||||
}); |
||||
} |
||||
} |
@ -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, |
||||
}); |
||||
} |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
Reference in new issue