Select last tab on presenter

This commit is contained in:
Shin'ya Ueoka 2018-07-29 11:17:01 +09:00
parent 09c5247dba
commit 0ca138cba4
2 changed files with 26 additions and 17 deletions

View file

@ -1,3 +1,8 @@
import MemoryStorage from '../infrastructures/memory-storage';
const CURRENT_SELECTED_KEY = 'tabs.current.selected';
const LAST_SELECTED_KEY = 'tabs.last.selected';
export default class TabPresenter {
open(url, tabId) {
return browser.tabs.update(tabId, { url });
@ -18,6 +23,15 @@ export default class TabPresenter {
return browser.tabs.query({ currentWindow: true });
}
async getLastSelectedId() {
let cache = new MemoryStorage();
let tabId = await cache.get(LAST_SELECTED_KEY);
if (tabId === null || typeof tabId === 'undefined') {
return;
}
return tabId;
}
async getByKeyword(keyword, excludePinned = false) {
let tabs = await browser.tabs.query({ currentWindow: true });
return tabs.filter((t) => {
@ -99,3 +113,14 @@ export default class TabPresenter {
browser.tabs.onActivated.addListener(listener);
}
}
let tabPresenter = new TabPresenter();
tabPresenter.onSelected((tab) => {
let cache = new MemoryStorage();
let lastId = cache.get(CURRENT_SELECTED_KEY);
if (lastId) {
cache.set(LAST_SELECTED_KEY, lastId);
}
cache.set(CURRENT_SELECTED_KEY, tab.tabId);
});