diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/background/controllers/version.js | 11 | ||||
-rw-r--r-- | src/background/index.js | 14 | ||||
-rw-r--r-- | src/background/infrastructures/notifier.js | 23 | ||||
-rw-r--r-- | src/background/repositories/tab.js | 5 | ||||
-rw-r--r-- | src/background/repositories/version.js | 10 | ||||
-rw-r--r-- | src/background/shared/versions/index.js | 38 | ||||
-rw-r--r-- | src/background/shared/versions/release-notes.js | 8 | ||||
-rw-r--r-- | src/background/shared/versions/storage.js | 10 | ||||
-rw-r--r-- | src/background/usecases/version.js | 41 |
9 files changed, 92 insertions, 68 deletions
diff --git a/src/background/controllers/version.js b/src/background/controllers/version.js new file mode 100644 index 0000000..04d99fe --- /dev/null +++ b/src/background/controllers/version.js @@ -0,0 +1,11 @@ +import VersionInteractor from '../usecases/version'; + +export default class VersionController { + constructor() { + this.versionInteractor = new VersionInteractor(); + } + + notifyIfUpdated() { + this.versionInteractor.notifyIfUpdated(); + } +} diff --git a/src/background/index.js b/src/background/index.js index 69dbe06..e753c48 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -5,25 +5,16 @@ import IndicatorComponent from 'background/components/indicator'; import reducers from 'background/reducers'; import { createStore, applyMiddleware } from 'redux'; import promise from 'redux-promise'; -import * as versions from './shared/versions'; import ContentMessageListener from './infrastructures/content-message-listener'; import SettingController from './controllers/setting'; +import VersionRepository from './controllers/version'; const store = createStore( reducers, applyMiddleware(promise), ); -const checkAndNotifyUpdated = async() => { - let updated = await versions.checkUpdated(); - if (!updated) { - return; - } - await versions.notify(); - await versions.commit(); -}; - /* eslint-disable no-unused-vars */ const backgroundComponent = new BackgroundComponent(store); const operationComponent = new OperationComponent(store); @@ -31,8 +22,7 @@ const tabComponent = new TabComponent(store); const indicatorComponent = new IndicatorComponent(store); /* eslint-enable no-unused-vars */ -checkAndNotifyUpdated(); - new SettingController().reload(); +new VersionRepository().notifyIfUpdated(); new ContentMessageListener().run(); diff --git a/src/background/infrastructures/notifier.js b/src/background/infrastructures/notifier.js new file mode 100644 index 0000000..1eccc47 --- /dev/null +++ b/src/background/infrastructures/notifier.js @@ -0,0 +1,23 @@ +const NOTIFICATION_ID = 'vimvixen-update'; + +export default class Notifier { + notify(title, message, onclick) { + const listener = (id) => { + if (id !== NOTIFICATION_ID) { + return; + } + + onclick(); + + browser.notifications.onClicked.removeListener(listener); + }; + browser.notifications.onClicked.addListener(listener); + + return browser.notifications.create(NOTIFICATION_ID, { + 'type': 'basic', + 'iconUrl': browser.extension.getURL('resources/icon_48x48.png'), + title, + message, + }); + } +} diff --git a/src/background/repositories/tab.js b/src/background/repositories/tab.js new file mode 100644 index 0000000..16989e1 --- /dev/null +++ b/src/background/repositories/tab.js @@ -0,0 +1,5 @@ +export default class TabRepository { + create(url) { + browser.tabs.create({ url, }); + } +} diff --git a/src/background/repositories/version.js b/src/background/repositories/version.js new file mode 100644 index 0000000..4c71d05 --- /dev/null +++ b/src/background/repositories/version.js @@ -0,0 +1,10 @@ +export default class VersionRepository { + async get() { + let { version } = await browser.storage.local.get('version'); + return version; + } + + update(version) { + return browser.storage.local.set({ version }); + } +} diff --git a/src/background/shared/versions/index.js b/src/background/shared/versions/index.js deleted file mode 100644 index aa09c92..0000000 --- a/src/background/shared/versions/index.js +++ /dev/null @@ -1,38 +0,0 @@ -import * as storage from './storage'; -import * as releaseNotes from './release-notes'; -import manifest from '../../../../manifest.json'; - -const NOTIFICATION_ID = 'vimvixen-update'; - -const notificationClickListener = (id) => { - if (id !== NOTIFICATION_ID) { - return; - } - - browser.tabs.create({ url: releaseNotes.url(manifest.version) }); - browser.notifications.onClicked.removeListener(notificationClickListener); -}; - -const checkUpdated = async() => { - let prev = await storage.load(); - if (!prev) { - return true; - } - return manifest.version !== prev; -}; - -const notify = () => { - browser.notifications.onClicked.addListener(notificationClickListener); - return browser.notifications.create(NOTIFICATION_ID, { - 'type': 'basic', - 'iconUrl': browser.extension.getURL('resources/icon_48x48.png'), - 'title': 'Vim Vixen ' + manifest.version + ' has been installed', - 'message': 'Click here to see release notes', - }); -}; - -const commit = () => { - storage.save(manifest.version); -}; - -export { checkUpdated, notify, commit }; diff --git a/src/background/shared/versions/release-notes.js b/src/background/shared/versions/release-notes.js deleted file mode 100644 index 6ef2335..0000000 --- a/src/background/shared/versions/release-notes.js +++ /dev/null @@ -1,8 +0,0 @@ -const url = (version) => { - if (version) { - return 'https://github.com/ueokande/vim-vixen/releases/tag/' + version; - } - return 'https://github.com/ueokande/vim-vixen/releases/'; -}; - -export { url }; diff --git a/src/background/shared/versions/storage.js b/src/background/shared/versions/storage.js deleted file mode 100644 index 7883258..0000000 --- a/src/background/shared/versions/storage.js +++ /dev/null @@ -1,10 +0,0 @@ -const load = async() => { - let { version } = await browser.storage.local.get('version'); - return version; -}; - -const save = (version) => { - return browser.storage.local.set({ version }); -}; - -export { load, save }; diff --git a/src/background/usecases/version.js b/src/background/usecases/version.js new file mode 100644 index 0000000..a681bdf --- /dev/null +++ b/src/background/usecases/version.js @@ -0,0 +1,41 @@ +import manifest from '../../../manifest.json'; +import VersionRepository from '../repositories/version'; +import TabRepository from '../repositories/tab'; +import Notifier from '../infrastructures/notifier'; + +export default class VersionInteractor { + constructor() { + this.versionRepository = new VersionRepository(); + this.tabRepository = new TabRepository(); + this.notifier = new Notifier(); + } + + async notifyIfUpdated() { + if (!await this.checkUpdated()) { + return; + } + + let title = 'Vim Vixen ' + manifest.version + ' has been installed'; + let message = 'Click here to see release notes'; + this.notifier.notify(title, message, () => { + let url = this.releaseNoteUrl(manifest.version); + this.tabRepository.create(url); + }); + this.versionRepository.update(manifest.version); + } + + async checkUpdated() { + let prev = await this.versionRepository.get(); + if (!prev) { + return true; + } + return manifest.version !== prev; + } + + releaseNoteUrl(version) { + if (version) { + return 'https://github.com/ueokande/vim-vixen/releases/tag/' + version; + } + return 'https://github.com/ueokande/vim-vixen/releases/'; + } +} |