From a26d8a8a1bed48a77e062914c120a23ace7bb8cf Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 24 Feb 2019 20:54:35 +0900 Subject: Capitalize background scripts --- src/background/repositories/BookmarkRepository.js | 13 +++++++++ .../repositories/CompletionsRepository.js | 31 ++++++++++++++++++++ src/background/repositories/FindRepository.js | 19 +++++++++++++ src/background/repositories/MarkRepository.js | 33 ++++++++++++++++++++++ .../repositories/PersistentSettingRepository.js | 16 +++++++++++ src/background/repositories/SettingRepository.js | 23 +++++++++++++++ src/background/repositories/VersionRepository.js | 10 +++++++ src/background/repositories/bookmark.js | 13 --------- src/background/repositories/completions.js | 31 -------------------- src/background/repositories/find.js | 19 ------------- src/background/repositories/mark.js | 33 ---------------------- src/background/repositories/persistent-setting.js | 16 ----------- src/background/repositories/setting.js | 23 --------------- src/background/repositories/version.js | 10 ------- 14 files changed, 145 insertions(+), 145 deletions(-) create mode 100644 src/background/repositories/BookmarkRepository.js create mode 100644 src/background/repositories/CompletionsRepository.js create mode 100644 src/background/repositories/FindRepository.js create mode 100644 src/background/repositories/MarkRepository.js create mode 100644 src/background/repositories/PersistentSettingRepository.js create mode 100644 src/background/repositories/SettingRepository.js create mode 100644 src/background/repositories/VersionRepository.js delete mode 100644 src/background/repositories/bookmark.js delete mode 100644 src/background/repositories/completions.js delete mode 100644 src/background/repositories/find.js delete mode 100644 src/background/repositories/mark.js delete mode 100644 src/background/repositories/persistent-setting.js delete mode 100644 src/background/repositories/setting.js delete mode 100644 src/background/repositories/version.js (limited to 'src/background/repositories') diff --git a/src/background/repositories/BookmarkRepository.js b/src/background/repositories/BookmarkRepository.js new file mode 100644 index 0000000..99f7ec4 --- /dev/null +++ b/src/background/repositories/BookmarkRepository.js @@ -0,0 +1,13 @@ +export default class BookmarkRepository { + async create(title, url) { + let item = await browser.bookmarks.create({ + type: 'bookmark', + title, + url, + }); + if (!item) { + throw new Error('Could not create a bookmark'); + } + return item; + } +} diff --git a/src/background/repositories/CompletionsRepository.js b/src/background/repositories/CompletionsRepository.js new file mode 100644 index 0000000..1318d36 --- /dev/null +++ b/src/background/repositories/CompletionsRepository.js @@ -0,0 +1,31 @@ +export default class CompletionsRepository { + async queryBookmarks(keywords) { + let items = await browser.bookmarks.search({ query: keywords }); + return items.filter((item) => { + let url = undefined; + try { + url = new URL(item.url); + } catch (e) { + return false; + } + return item.type === 'bookmark' && url.protocol !== 'place:'; + }); + } + + queryHistories(keywords) { + return browser.history.search({ + text: keywords, + startTime: 0, + }); + } + + async queryTabs(keywords, excludePinned) { + let tabs = await browser.tabs.query({ currentWindow: true }); + return tabs.filter((t) => { + return t.url.toLowerCase().includes(keywords.toLowerCase()) || + t.title && t.title.toLowerCase().includes(keywords.toLowerCase()); + }).filter((t) => { + return !(excludePinned && t.pinned); + }); + } +} diff --git a/src/background/repositories/FindRepository.js b/src/background/repositories/FindRepository.js new file mode 100644 index 0000000..74ec914 --- /dev/null +++ b/src/background/repositories/FindRepository.js @@ -0,0 +1,19 @@ +import MemoryStorage from '../infrastructures/MemoryStorage'; + +const FIND_KEYWORD_KEY = 'find-keyword'; + +export default class FindRepository { + constructor() { + this.cache = new MemoryStorage(); + } + + getKeyword() { + return Promise.resolve(this.cache.get(FIND_KEYWORD_KEY)); + } + + setKeyword(keyword) { + this.cache.set(FIND_KEYWORD_KEY, keyword); + return Promise.resolve(); + } +} + diff --git a/src/background/repositories/MarkRepository.js b/src/background/repositories/MarkRepository.js new file mode 100644 index 0000000..282c712 --- /dev/null +++ b/src/background/repositories/MarkRepository.js @@ -0,0 +1,33 @@ +import MemoryStorage from '../infrastructures/MemoryStorage'; +import GlobalMark from '../domains/GlobalMark'; + +const MARK_KEY = 'mark'; + +export default class MarkRepository { + constructor() { + this.cache = new MemoryStorage(); + } + + getMark(key) { + let marks = this.getOrEmptyMarks(); + let data = marks[key]; + if (!data) { + return Promise.resolve(undefined); + } + let mark = new GlobalMark(data.tabId, data.url, data.x, data.y); + return Promise.resolve(mark); + } + + setMark(key, mark) { + let marks = this.getOrEmptyMarks(); + marks[key] = { tabId: mark.tabId, url: mark.url, x: mark.x, y: mark.y }; + this.cache.set(MARK_KEY, marks); + + return Promise.resolve(); + } + + getOrEmptyMarks() { + return this.cache.get(MARK_KEY) || {}; + } +} + diff --git a/src/background/repositories/PersistentSettingRepository.js b/src/background/repositories/PersistentSettingRepository.js new file mode 100644 index 0000000..639bcd1 --- /dev/null +++ b/src/background/repositories/PersistentSettingRepository.js @@ -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); + } +} + diff --git a/src/background/repositories/SettingRepository.js b/src/background/repositories/SettingRepository.js new file mode 100644 index 0000000..c4667a9 --- /dev/null +++ b/src/background/repositories/SettingRepository.js @@ -0,0 +1,23 @@ +import MemoryStorage from '../infrastructures/MemoryStorage'; + +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); + } + + async setProperty(name, value) { + let current = await this.get(); + current.properties[name] = value; + return this.update(current); + } +} diff --git a/src/background/repositories/VersionRepository.js b/src/background/repositories/VersionRepository.js new file mode 100644 index 0000000..4c71d05 --- /dev/null +++ b/src/background/repositories/VersionRepository.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/repositories/bookmark.js b/src/background/repositories/bookmark.js deleted file mode 100644 index 99f7ec4..0000000 --- a/src/background/repositories/bookmark.js +++ /dev/null @@ -1,13 +0,0 @@ -export default class BookmarkRepository { - async create(title, url) { - let item = await browser.bookmarks.create({ - type: 'bookmark', - title, - url, - }); - if (!item) { - throw new Error('Could not create a bookmark'); - } - return item; - } -} diff --git a/src/background/repositories/completions.js b/src/background/repositories/completions.js deleted file mode 100644 index 1318d36..0000000 --- a/src/background/repositories/completions.js +++ /dev/null @@ -1,31 +0,0 @@ -export default class CompletionsRepository { - async queryBookmarks(keywords) { - let items = await browser.bookmarks.search({ query: keywords }); - return items.filter((item) => { - let url = undefined; - try { - url = new URL(item.url); - } catch (e) { - return false; - } - return item.type === 'bookmark' && url.protocol !== 'place:'; - }); - } - - queryHistories(keywords) { - return browser.history.search({ - text: keywords, - startTime: 0, - }); - } - - async queryTabs(keywords, excludePinned) { - let tabs = await browser.tabs.query({ currentWindow: true }); - return tabs.filter((t) => { - return t.url.toLowerCase().includes(keywords.toLowerCase()) || - t.title && t.title.toLowerCase().includes(keywords.toLowerCase()); - }).filter((t) => { - return !(excludePinned && t.pinned); - }); - } -} diff --git a/src/background/repositories/find.js b/src/background/repositories/find.js deleted file mode 100644 index 6656c58..0000000 --- a/src/background/repositories/find.js +++ /dev/null @@ -1,19 +0,0 @@ -import MemoryStorage from '../infrastructures/memory-storage'; - -const FIND_KEYWORD_KEY = 'find-keyword'; - -export default class FindRepository { - constructor() { - this.cache = new MemoryStorage(); - } - - getKeyword() { - return Promise.resolve(this.cache.get(FIND_KEYWORD_KEY)); - } - - setKeyword(keyword) { - this.cache.set(FIND_KEYWORD_KEY, keyword); - return Promise.resolve(); - } -} - diff --git a/src/background/repositories/mark.js b/src/background/repositories/mark.js deleted file mode 100644 index 339a660..0000000 --- a/src/background/repositories/mark.js +++ /dev/null @@ -1,33 +0,0 @@ -import MemoryStorage from '../infrastructures/memory-storage'; -import GlobalMark from 'background/domains/global-mark'; - -const MARK_KEY = 'mark'; - -export default class MarkRepository { - constructor() { - this.cache = new MemoryStorage(); - } - - getMark(key) { - let marks = this.getOrEmptyMarks(); - let data = marks[key]; - if (!data) { - return Promise.resolve(undefined); - } - let mark = new GlobalMark(data.tabId, data.url, data.x, data.y); - return Promise.resolve(mark); - } - - setMark(key, mark) { - let marks = this.getOrEmptyMarks(); - marks[key] = { tabId: mark.tabId, url: mark.url, x: mark.x, y: mark.y }; - this.cache.set(MARK_KEY, marks); - - return Promise.resolve(); - } - - getOrEmptyMarks() { - return this.cache.get(MARK_KEY) || {}; - } -} - diff --git a/src/background/repositories/persistent-setting.js b/src/background/repositories/persistent-setting.js deleted file mode 100644 index 247ea6f..0000000 --- a/src/background/repositories/persistent-setting.js +++ /dev/null @@ -1,16 +0,0 @@ -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); - } -} - diff --git a/src/background/repositories/setting.js b/src/background/repositories/setting.js deleted file mode 100644 index 6d48525..0000000 --- a/src/background/repositories/setting.js +++ /dev/null @@ -1,23 +0,0 @@ -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); - } - - async setProperty(name, value) { - let current = await this.get(); - current.properties[name] = value; - return this.update(current); - } -} diff --git a/src/background/repositories/version.js b/src/background/repositories/version.js deleted file mode 100644 index 4c71d05..0000000 --- a/src/background/repositories/version.js +++ /dev/null @@ -1,10 +0,0 @@ -export default class VersionRepository { - async get() { - let { version } = await browser.storage.local.get('version'); - return version; - } - - update(version) { - return browser.storage.local.set({ version }); - } -} -- cgit v1.2.3