Version as Clean Architecture

jh-changes
Shin'ya Ueoka 6 years ago
parent a1e5e97200
commit 87ed1f43a9
  1. 11
      src/background/controllers/version.js
  2. 14
      src/background/index.js
  3. 23
      src/background/infrastructures/notifier.js
  4. 5
      src/background/repositories/tab.js
  5. 10
      src/background/repositories/version.js
  6. 38
      src/background/shared/versions/index.js
  7. 8
      src/background/shared/versions/release-notes.js
  8. 10
      src/background/shared/versions/storage.js
  9. 41
      src/background/usecases/version.js
  10. 18
      test/background/repositories/version.js
  11. 40
      test/background/shared/versions/index.test.js

@ -0,0 +1,11 @@
import VersionInteractor from '../usecases/version';
export default class VersionController {
constructor() {
this.versionInteractor = new VersionInteractor();
}
notifyIfUpdated() {
this.versionInteractor.notifyIfUpdated();
}
}

@ -5,25 +5,16 @@ import IndicatorComponent from 'background/components/indicator';
import reducers from 'background/reducers'; import reducers from 'background/reducers';
import { createStore, applyMiddleware } from 'redux'; import { createStore, applyMiddleware } from 'redux';
import promise from 'redux-promise'; import promise from 'redux-promise';
import * as versions from './shared/versions';
import ContentMessageListener from './infrastructures/content-message-listener'; import ContentMessageListener from './infrastructures/content-message-listener';
import SettingController from './controllers/setting'; import SettingController from './controllers/setting';
import VersionRepository from './controllers/version';
const store = createStore( const store = createStore(
reducers, reducers,
applyMiddleware(promise), applyMiddleware(promise),
); );
const checkAndNotifyUpdated = async() => {
let updated = await versions.checkUpdated();
if (!updated) {
return;
}
await versions.notify();
await versions.commit();
};
/* eslint-disable no-unused-vars */ /* eslint-disable no-unused-vars */
const backgroundComponent = new BackgroundComponent(store); const backgroundComponent = new BackgroundComponent(store);
const operationComponent = new OperationComponent(store); const operationComponent = new OperationComponent(store);
@ -31,8 +22,7 @@ const tabComponent = new TabComponent(store);
const indicatorComponent = new IndicatorComponent(store); const indicatorComponent = new IndicatorComponent(store);
/* eslint-enable no-unused-vars */ /* eslint-enable no-unused-vars */
checkAndNotifyUpdated();
new SettingController().reload(); new SettingController().reload();
new VersionRepository().notifyIfUpdated();
new ContentMessageListener().run(); new ContentMessageListener().run();

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

@ -0,0 +1,5 @@
export default class TabRepository {
create(url) {
browser.tabs.create({ url, });
}
}

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

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

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

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

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

@ -1,14 +1,20 @@
import * as storage from 'background/shared/versions/storage'; import VersionRepository from 'background/repositories/version';
describe("background/repositories/version", () => {
let versionRepository;
describe("shared/versions/storage", () => { beforeEach(() => {
describe('#load', () => { versionRepository = new VersionRepository;
});
describe('#get', () => {
beforeEach(() => { beforeEach(() => {
return browser.storage.local.remove('version'); return browser.storage.local.remove('version');
}); });
it('loads saved version', async() => { it('loads saved version', async() => {
await browser.storage.local.set({ version: '1.2.3' }); 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'); expect(version).to.equal('1.2.3');
}); });
@ -18,9 +24,9 @@ describe("shared/versions/storage", () => {
}); });
}); });
describe('#save', () => { describe('#update', () => {
it('saves version string', async() => { 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'); let { version } = await browser.storage.local.get('version');
expect(version).to.equal('2.3.4'); expect(version).to.equal('2.3.4');
}); });

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