diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-01 11:04:24 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-02 22:07:53 +0900 |
commit | 678020a3a27713e77ec8d74483122b4258fbc829 (patch) | |
tree | 6c84d0965f05918e6fd58a0a155c15035474d563 /src/background/presenters/TabPresenter.ts | |
parent | 0cffb09e249832291be73be039dc1b9bb38115f9 (diff) |
Types on src/background
Diffstat (limited to 'src/background/presenters/TabPresenter.ts')
-rw-r--r-- | src/background/presenters/TabPresenter.ts | 44 |
1 files changed, 25 insertions, 19 deletions
diff --git a/src/background/presenters/TabPresenter.ts b/src/background/presenters/TabPresenter.ts index 744be39..33c6513 100644 --- a/src/background/presenters/TabPresenter.ts +++ b/src/background/presenters/TabPresenter.ts @@ -3,27 +3,29 @@ import MemoryStorage from '../infrastructures/MemoryStorage'; const CURRENT_SELECTED_KEY = 'tabs.current.selected'; const LAST_SELECTED_KEY = 'tabs.last.selected'; +type Tab = browser.tabs.Tab; + export default class TabPresenter { - open(url, tabId) { + open(url: string, tabId?: number): Promise<Tab> { return browser.tabs.update(tabId, { url }); } - create(url, opts) { + create(url: string, opts?: object): Promise<Tab> { return browser.tabs.create({ url, ...opts }); } - async getCurrent() { + async getCurrent(): Promise<Tab> { let tabs = await browser.tabs.query({ active: true, currentWindow: true }); return tabs[0]; } - getAll() { + getAll(): Promise<Tab[]> { return browser.tabs.query({ currentWindow: true }); } - async getLastSelectedId() { + async getLastSelectedId(): Promise<number | undefined> { let cache = new MemoryStorage(); let tabId = await cache.get(LAST_SELECTED_KEY); if (tabId === null || typeof tabId === 'undefined') { @@ -32,25 +34,25 @@ export default class TabPresenter { return tabId; } - async getByKeyword(keyword, excludePinned = false) { + async getByKeyword(keyword: string, excludePinned = false): Promise<Tab[]> { let tabs = await browser.tabs.query({ currentWindow: true }); return tabs.filter((t) => { - return t.url.toLowerCase().includes(keyword.toLowerCase()) || + return t.url && t.url.toLowerCase().includes(keyword.toLowerCase()) || t.title && t.title.toLowerCase().includes(keyword.toLowerCase()); }).filter((t) => { return !(excludePinned && t.pinned); }); } - select(tabId) { + select(tabId: number): Promise<Tab> { return browser.tabs.update(tabId, { active: true }); } - remove(ids) { + remove(ids: number[]): Promise<void> { return browser.tabs.remove(ids); } - async reopen() { + async reopen(): Promise<any> { let window = await browser.windows.getCurrent(); let sessions = await browser.sessions.getRecentlyClosed(); let session = sessions.find((s) => { @@ -59,39 +61,43 @@ export default class TabPresenter { if (!session) { return; } - if (session.tab) { + if (session.tab && session.tab.sessionId) { return browser.sessions.restore(session.tab.sessionId); } - return browser.sessions.restore(session.window.sessionId); + if (session.window && session.window.sessionId) { + return browser.sessions.restore(session.window.sessionId); + } } - reload(tabId, cache) { + reload(tabId: number, cache: boolean): Promise<void> { return browser.tabs.reload(tabId, { bypassCache: cache }); } - setPinned(tabId, pinned) { + setPinned(tabId: number, pinned: boolean): Promise<Tab> { return browser.tabs.update(tabId, { pinned }); } - duplicate(id) { + duplicate(id: number): Promise<Tab> { return browser.tabs.duplicate(id); } - getZoom(tabId) { + getZoom(tabId: number): Promise<number> { return browser.tabs.getZoom(tabId); } - setZoom(tabId, factor) { + setZoom(tabId: number, factor: number): Promise<void> { return browser.tabs.setZoom(tabId, factor); } - onSelected(listener) { + onSelected( + listener: (arg: { tabId: number, windowId: number}) => void, + ): void { browser.tabs.onActivated.addListener(listener); } } let tabPresenter = new TabPresenter(); -tabPresenter.onSelected((tab) => { +tabPresenter.onSelected((tab: any) => { let cache = new MemoryStorage(); let lastId = cache.get(CURRENT_SELECTED_KEY); |