From f961c205a7afbcbfc4cc585fbb0362796043f1a1 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Fri, 15 Feb 2019 21:01:12 +0900 Subject: Use onInstalled event --- src/background/controllers/VersionController.js | 4 ++-- src/background/controllers/version.js | 13 +++++++++++ src/background/index.js | 5 ++++- src/background/infrastructures/Notifier.js | 23 -------------------- src/background/presenters/NotifyPresenter.js | 23 ++++++++++++++++++++ src/background/usecases/VersionUseCase.js | 29 ++++++------------------- 6 files changed, 49 insertions(+), 48 deletions(-) create mode 100644 src/background/controllers/version.js delete mode 100644 src/background/infrastructures/Notifier.js create mode 100644 src/background/presenters/NotifyPresenter.js (limited to 'src/background') diff --git a/src/background/controllers/VersionController.js b/src/background/controllers/VersionController.js index 1bcac4c..c596f9b 100644 --- a/src/background/controllers/VersionController.js +++ b/src/background/controllers/VersionController.js @@ -5,7 +5,7 @@ export default class VersionController { this.versionUseCase = new VersionUseCase(); } - notifyIfUpdated() { - this.versionUseCase.notifyIfUpdated(); + notify() { + this.versionUseCase.notify(); } } diff --git a/src/background/controllers/version.js b/src/background/controllers/version.js new file mode 100644 index 0000000..ec0f634 --- /dev/null +++ b/src/background/controllers/version.js @@ -0,0 +1,13 @@ +import VersionInteractor from '../usecases/version'; + +export default class VersionController { + constructor() { + this.versionInteractor = new VersionInteractor(); + } + + notifyIfUpdated() { + browser.runtime.onInstalled.addListener(() => { + return this.versionInteractor.notify(); + }); + } +} diff --git a/src/background/index.js b/src/background/index.js index b829f99..3e7492d 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -3,6 +3,9 @@ import SettingController from './controllers/SettingController'; import VersionController from './controllers/VersionController'; new SettingController().reload(); -new VersionController().notifyIfUpdated(); + +browser.runtime.onInstalled.addListener(() => { + new VersionController().notify(); +}); new ContentMessageListener().run(); diff --git a/src/background/infrastructures/Notifier.js b/src/background/infrastructures/Notifier.js deleted file mode 100644 index 1eccc47..0000000 --- a/src/background/infrastructures/Notifier.js +++ /dev/null @@ -1,23 +0,0 @@ -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/presenters/NotifyPresenter.js b/src/background/presenters/NotifyPresenter.js new file mode 100644 index 0000000..a81f227 --- /dev/null +++ b/src/background/presenters/NotifyPresenter.js @@ -0,0 +1,23 @@ +const NOTIFICATION_ID = 'vimvixen-update'; + +export default class NotifyPresenter { + 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/usecases/VersionUseCase.js b/src/background/usecases/VersionUseCase.js index 704c808..ed5112b 100644 --- a/src/background/usecases/VersionUseCase.js +++ b/src/background/usecases/VersionUseCase.js @@ -1,40 +1,25 @@ import manifest from '../../../manifest.json'; -import VersionRepository from '../repositories/VersionRepository'; import TabPresenter from '../presenters/TabPresenter'; -import Notifier from '../infrastructures/Notifier'; +import NotifyPresenter from '../presenters/NotifyPresenter'; export default class VersionUseCase { constructor() { - this.versionRepository = new VersionRepository(); this.tabPresenter = new TabPresenter(); - this.notifier = new Notifier(); + this.notifyPresenter = new NotifyPresenter(); } - async notifyIfUpdated() { - if (!await this.checkUpdated()) { - return; - } - - let title = 'Vim Vixen ' + manifest.version + ' has been installed'; + notify() { + 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); + let url = this.releaseNoteUrl(manifest.version); + this.notifyPresenter.notify(title, message, () => { this.tabPresenter.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/tag/${version}`; } return 'https://github.com/ueokande/vim-vixen/releases/'; } -- cgit v1.2.3