diff options
-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 | ||||
-rw-r--r-- | test/background/repositories/version.js (renamed from test/background/shared/versions/storage.test.js) | 18 | ||||
-rw-r--r-- | test/background/shared/versions/index.test.js | 40 |
11 files changed, 104 insertions, 114 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/'; + } +} diff --git a/test/background/shared/versions/storage.test.js b/test/background/repositories/version.js index f452516..8510ae6 100644 --- a/test/background/shared/versions/storage.test.js +++ b/test/background/repositories/version.js @@ -1,14 +1,20 @@ -import * as storage from 'background/shared/versions/storage'; +import VersionRepository from 'background/repositories/version'; -describe("shared/versions/storage", () => { - describe('#load', () => { +describe("background/repositories/version", () => { + let versionRepository; + + beforeEach(() => { + versionRepository = new VersionRepository; + }); + + describe('#get', () => { beforeEach(() => { return browser.storage.local.remove('version'); }); it('loads saved version', async() => { await browser.storage.local.set({ version: '1.2.3' }); - let version = await storage.load(); + let version = await this.versionRepository.get(); expect(version).to.equal('1.2.3'); }); @@ -18,9 +24,9 @@ describe("shared/versions/storage", () => { }); }); - describe('#save', () => { + describe('#update', () => { it('saves version string', async() => { - await storage.save('2.3.4'); + await versionRepository.update('2.3.4'); let { version } = await browser.storage.local.get('version'); expect(version).to.equal('2.3.4'); }); diff --git a/test/background/shared/versions/index.test.js b/test/background/shared/versions/index.test.js deleted file mode 100644 index d65dd9a..0000000 --- a/test/background/shared/versions/index.test.js +++ /dev/null @@ -1,40 +0,0 @@ -import * as versions from 'background/shared/versions'; -import manifest from '../../../../manifest.json'; - -describe("shared/versions/storage", () => { - describe('#checkUpdated', () => { - beforeEach(() => { - return browser.storage.local.remove('version'); - }); - - it('return true if no previous versions', async() => { - let updated = await versions.checkUpdated(); - expect(updated).to.be.true; - }); - - it('return true if updated', async() => { - await browser.storage.local.set({ version: '0.001' }); - let updated = await versions.checkUpdated(); - expect(updated).to.be.true; - }); - - it('return false if not updated', async() => { - await browser.storage.local.set({ version: manifest.version }); - let updated = await versions.checkUpdated(); - expect(updated).to.be.false; - }); - }); - - describe('#commit', () => { - beforeEach(() => { - return browser.storage.local.remove('version'); - }); - - it('saves current version from manifest.json', async() => { - await versions.commit(); - let { version } = await browser.storage.local.get('version'); - expect(version).to.be.a('string'); - expect(version).to.equal(manifest.version); - }); - }); -}); |