Use async/await on background
This commit is contained in:
parent
88238005ab
commit
48e4bccf0d
12 changed files with 273 additions and 316 deletions
|
@ -5,56 +5,63 @@ import * as bookmarks from '../shared/bookmarks';
|
|||
import * as parsers from 'shared/commands/parsers';
|
||||
import * as properties from 'shared/settings/properties';
|
||||
|
||||
const openCommand = (url) => {
|
||||
return browser.tabs.query({
|
||||
const openCommand = async(url) => {
|
||||
let got = await browser.tabs.query({
|
||||
active: true, currentWindow: true
|
||||
}).then((gotTabs) => {
|
||||
if (gotTabs.length > 0) {
|
||||
return browser.tabs.update(gotTabs[0].id, { url: url });
|
||||
}
|
||||
});
|
||||
if (got.length > 0) {
|
||||
return browser.tabs.update(got[0].id, { url: url });
|
||||
}
|
||||
};
|
||||
|
||||
const tabopenCommand = (url) => {
|
||||
return browser.tabs.create({ url: url });
|
||||
};
|
||||
|
||||
const tabcloseCommand = () => {
|
||||
return browser.tabs.query({
|
||||
const tabcloseCommand = async() => {
|
||||
let got = await browser.tabs.query({
|
||||
active: true, currentWindow: true
|
||||
}).then((tabList) => {
|
||||
return browser.tabs.remove(tabList.map(tab => tab.id));
|
||||
});
|
||||
return browser.tabs.remove(got.map(tab => tab.id));
|
||||
};
|
||||
|
||||
const winopenCommand = (url) => {
|
||||
return browser.windows.create({ url });
|
||||
};
|
||||
|
||||
const bufferCommand = (keywords) => {
|
||||
const bufferCommand = async(keywords) => {
|
||||
if (keywords.length === 0) {
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
let keywordsStr = keywords.join(' ');
|
||||
return browser.tabs.query({
|
||||
let got = await browser.tabs.query({
|
||||
active: true, currentWindow: true
|
||||
}).then((gotTabs) => {
|
||||
if (gotTabs.length > 0) {
|
||||
});
|
||||
if (got.length === 0) {
|
||||
return;
|
||||
}
|
||||
if (isNaN(keywordsStr)) {
|
||||
return tabs.selectByKeyword(gotTabs[0], keywordsStr);
|
||||
return tabs.selectByKeyword(got[0], keywordsStr);
|
||||
}
|
||||
let index = parseInt(keywordsStr, 10) - 1;
|
||||
return tabs.selectAt(index);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const addBookmarkCommand = (tab, args) => {
|
||||
const addbookmarkCommand = async(tab, args) => {
|
||||
if (!args[0]) {
|
||||
return Promise.resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
return bookmarks.create(args.join(' '), tab.url);
|
||||
let item = await bookmarks.create(args.join(' '), tab.url);
|
||||
if (!item) {
|
||||
return browser.tabs.sendMessage(tab.id, {
|
||||
type: messages.CONSOLE_SHOW_ERROR,
|
||||
text: 'Could not create a bookmark',
|
||||
});
|
||||
}
|
||||
return browser.tabs.sendMessage(tab.id, {
|
||||
type: messages.CONSOLE_SHOW_INFO,
|
||||
text: 'Saved current page: ' + item.url,
|
||||
});
|
||||
};
|
||||
|
||||
const setCommand = (args) => {
|
||||
|
@ -100,18 +107,7 @@ const exec = (tab, line, settings) => {
|
|||
case 'bdeletes!':
|
||||
return tabs.closeTabsByKeywordsForce(args.join(' '));
|
||||
case 'addbookmark':
|
||||
return addBookmarkCommand(tab, args).then((item) => {
|
||||
if (!item) {
|
||||
return browser.tabs.sendMessage(tab.id, {
|
||||
type: messages.CONSOLE_SHOW_ERROR,
|
||||
text: 'Could not create a bookmark',
|
||||
});
|
||||
}
|
||||
return browser.tabs.sendMessage(tab.id, {
|
||||
type: messages.CONSOLE_SHOW_INFO,
|
||||
text: 'Saved current page: ' + item.url,
|
||||
});
|
||||
});
|
||||
return addbookmarkCommand(tab, args);
|
||||
case 'set':
|
||||
return setCommand(args);
|
||||
case 'q':
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
import actions from '../actions';
|
||||
import * as settingsStorage from 'shared/settings/storage';
|
||||
|
||||
const load = () => {
|
||||
return settingsStorage.loadValue().then((value) => {
|
||||
const load = async() => {
|
||||
let value = await settingsStorage.loadValue();
|
||||
return {
|
||||
type: actions.SETTING_SET_SETTINGS,
|
||||
value,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
const setProperty = (name, value) => {
|
||||
|
|
|
@ -1,19 +1,20 @@
|
|||
import actions from './index';
|
||||
|
||||
const openNewTab = (url, openerTabId, background = false, adjacent = false) => {
|
||||
if (adjacent) {
|
||||
return browser.tabs.query({
|
||||
const openNewTab = async(
|
||||
url, openerTabId, background = false, adjacent = false
|
||||
) => {
|
||||
if (!adjacent) {
|
||||
return browser.tabs.create({ url, active: !background });
|
||||
}
|
||||
let tabs = await browser.tabs.query({
|
||||
active: true, currentWindow: true
|
||||
}).then((tabs) => {
|
||||
});
|
||||
return browser.tabs.create({
|
||||
url,
|
||||
openerTabId,
|
||||
active: !background,
|
||||
index: tabs[0].index + 1
|
||||
});
|
||||
});
|
||||
}
|
||||
return browser.tabs.create({ url, active: !background });
|
||||
};
|
||||
|
||||
const openToTab = (url, tab) => {
|
||||
|
|
|
@ -56,13 +56,12 @@ export default class BackgroundComponent {
|
|||
}
|
||||
}
|
||||
|
||||
broadcastSettingsChanged() {
|
||||
return browser.tabs.query({}).then((tabs) => {
|
||||
async broadcastSettingsChanged() {
|
||||
let tabs = await browser.tabs.query({});
|
||||
for (let tab of tabs) {
|
||||
browser.tabs.sendMessage(tab.id, {
|
||||
type: messages.SETTINGS_CHANGED,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,19 +8,17 @@ export default class IndicatorComponent {
|
|||
messages.onMessage(this.onMessage.bind(this));
|
||||
|
||||
browser.browserAction.onClicked.addListener(this.onClicked);
|
||||
browser.tabs.onActivated.addListener((info) => {
|
||||
return browser.tabs.query({ currentWindow: true }).then(() => {
|
||||
browser.tabs.onActivated.addListener(async(info) => {
|
||||
await browser.tabs.query({ currentWindow: true });
|
||||
return this.onTabActivated(info);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
onTabActivated(info) {
|
||||
return browser.tabs.sendMessage(info.tabId, {
|
||||
async onTabActivated(info) {
|
||||
let { enabled } = await browser.tabs.sendMessage(info.tabId, {
|
||||
type: messages.ADDON_ENABLED_QUERY,
|
||||
}).then((resp) => {
|
||||
return this.updateIndicator(resp.enabled);
|
||||
});
|
||||
return this.updateIndicator(enabled);
|
||||
}
|
||||
|
||||
onClicked(tab) {
|
||||
|
|
|
@ -4,11 +4,10 @@ export default class TabComponent {
|
|||
constructor(store) {
|
||||
this.store = store;
|
||||
|
||||
browser.tabs.onActivated.addListener((info) => {
|
||||
return browser.tabs.query({ currentWindow: true }).then(() => {
|
||||
browser.tabs.onActivated.addListener(async(info) => {
|
||||
await browser.tabs.query({ currentWindow: true });
|
||||
return this.onTabActivated(info);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
onTabActivated(info) {
|
||||
|
|
|
@ -18,6 +18,15 @@ const store = createStore(reducers, (e, sender) => {
|
|||
}
|
||||
});
|
||||
|
||||
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);
|
||||
|
@ -27,11 +36,4 @@ const indicatorComponent = new IndicatorComponent(store);
|
|||
|
||||
store.dispatch(settingActions.load());
|
||||
|
||||
versions.checkUpdated().then((updated) => {
|
||||
if (!updated) {
|
||||
return;
|
||||
}
|
||||
return versions.notify();
|
||||
}).then(() => {
|
||||
return versions.commit();
|
||||
});
|
||||
checkAndNotifyUpdated();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const getCompletions = (keywords) => {
|
||||
return browser.bookmarks.search({ query: keywords }).then((items) => {
|
||||
const getCompletions = async(keywords) => {
|
||||
let items = await browser.bookmarks.search({ query: keywords });
|
||||
return items.filter((item) => {
|
||||
let url = undefined;
|
||||
try {
|
||||
|
@ -9,7 +9,6 @@ const getCompletions = (keywords) => {
|
|||
}
|
||||
return item.type === 'bookmark' && url.protocol !== 'place:';
|
||||
}).slice(0, 10);
|
||||
});
|
||||
};
|
||||
|
||||
export { getCompletions };
|
||||
|
|
|
@ -61,11 +61,11 @@ const reduceByOrigin = (items, min) => {
|
|||
return filtered;
|
||||
};
|
||||
|
||||
const getCompletions = (keyword) => {
|
||||
return browser.history.search({
|
||||
const getCompletions = async(keyword) => {
|
||||
let historyItems = await browser.history.search({
|
||||
text: keyword,
|
||||
startTime: 0,
|
||||
}).then((historyItems) => {
|
||||
});
|
||||
return [historyItems.map(item => [item, new URL(item.url)])]
|
||||
.map(filterEmptyTitle)
|
||||
.map(filterHttp)
|
||||
|
@ -77,7 +77,6 @@ const getCompletions = (keyword) => {
|
|||
.slice(0, 10)
|
||||
.map(item => item[0])
|
||||
)[0];
|
||||
});
|
||||
};
|
||||
|
||||
export { getCompletions };
|
||||
|
|
|
@ -12,36 +12,30 @@ const getSearchCompletions = (command, keywords, searchConfig) => {
|
|||
return Promise.resolve(engineItems);
|
||||
};
|
||||
|
||||
const getHistoryCompletions = (command, keywords) => {
|
||||
return histories.getCompletions(keywords).then((pages) => {
|
||||
return pages.map((page) => {
|
||||
const getHistoryCompletions = async(command, keywords) => {
|
||||
let items = await histories.getCompletions(keywords);
|
||||
return items.map((page) => {
|
||||
return {
|
||||
caption: page.title,
|
||||
content: command + ' ' + page.url,
|
||||
url: page.url
|
||||
};
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const getBookmarksCompletions = (command, keywords) => {
|
||||
return bookmarks.getCompletions(keywords).then((items) => {
|
||||
return items.map((item) => {
|
||||
return {
|
||||
const getBookmarksCompletions = async(command, keywords) => {
|
||||
let items = await bookmarks.getCompletions(keywords);
|
||||
return items.map(item => ({
|
||||
caption: item.title,
|
||||
content: command + ' ' + item.url,
|
||||
url: item.url,
|
||||
};
|
||||
});
|
||||
});
|
||||
}));
|
||||
};
|
||||
|
||||
const getOpenCompletions = (command, keywords, searchConfig) => {
|
||||
return Promise.all([
|
||||
getSearchCompletions(command, keywords, searchConfig),
|
||||
getHistoryCompletions(command, keywords),
|
||||
getBookmarksCompletions(command, keywords),
|
||||
]).then(([engineItems, historyItems, bookmarkItems]) => {
|
||||
const getOpenCompletions = async(command, keywords, searchConfig) => {
|
||||
let engineItems = await getSearchCompletions(command, keywords, searchConfig);
|
||||
let historyItems = await getHistoryCompletions(command, keywords);
|
||||
let bookmarkItems = await getBookmarksCompletions(command, keywords);
|
||||
let completions = [];
|
||||
if (engineItems.length > 0) {
|
||||
completions.push({
|
||||
|
@ -62,26 +56,22 @@ const getOpenCompletions = (command, keywords, searchConfig) => {
|
|||
});
|
||||
}
|
||||
return completions;
|
||||
});
|
||||
};
|
||||
|
||||
const getBufferCompletions = (command, keywords, excludePinned) => {
|
||||
return tabs.getCompletions(keywords, excludePinned).then((got) => {
|
||||
let items = got.map((tab) => {
|
||||
return {
|
||||
const getBufferCompletions = async(command, keywords, excludePinned) => {
|
||||
let items = await tabs.getCompletions(keywords, excludePinned);
|
||||
items = items.map(tab => ({
|
||||
caption: tab.title,
|
||||
content: command + ' ' + tab.title,
|
||||
url: tab.url,
|
||||
icon: tab.favIconUrl
|
||||
};
|
||||
});
|
||||
}));
|
||||
return [
|
||||
{
|
||||
name: 'Buffers',
|
||||
items: items
|
||||
}
|
||||
];
|
||||
});
|
||||
};
|
||||
|
||||
const getCompletions = (line, settings) => {
|
||||
|
|
|
@ -1,69 +1,60 @@
|
|||
import * as tabCompletions from './completions/tabs';
|
||||
|
||||
const closeTab = (id) => {
|
||||
return browser.tabs.get(id).then((tab) => {
|
||||
const closeTab = async(id) => {
|
||||
let tab = await browser.tabs.get(id);
|
||||
if (!tab.pinned) {
|
||||
return browser.tabs.remove(id);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const closeTabForce = (id) => {
|
||||
return browser.tabs.remove(id);
|
||||
};
|
||||
|
||||
const queryByKeyword = (keyword, excludePinned = false) => {
|
||||
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
|
||||
const queryByKeyword = async(keyword, excludePinned = false) => {
|
||||
let tabs = await browser.tabs.query({ currentWindow: true });
|
||||
return tabs.filter((t) => {
|
||||
return t.url.toLowerCase().includes(keyword.toLowerCase()) ||
|
||||
t.title && t.title.toLowerCase().includes(keyword.toLowerCase());
|
||||
}).filter((t) => {
|
||||
return !(excludePinned && t.pinned);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const closeTabByKeywords = (keyword) => {
|
||||
return queryByKeyword(keyword, false).then((tabs) => {
|
||||
const closeTabByKeywords = async(keyword) => {
|
||||
let tabs = await queryByKeyword(keyword, false);
|
||||
if (tabs.length === 0) {
|
||||
throw new Error('No matching buffer for ' + keyword);
|
||||
} else if (tabs.length > 1) {
|
||||
throw new Error('More than one match for ' + keyword);
|
||||
}
|
||||
browser.tabs.remove(tabs[0].id);
|
||||
});
|
||||
return browser.tabs.remove(tabs[0].id);
|
||||
};
|
||||
|
||||
const closeTabByKeywordsForce = (keyword) => {
|
||||
return queryByKeyword(keyword, true).then((tabs) => {
|
||||
const closeTabByKeywordsForce = async(keyword) => {
|
||||
let tabs = await queryByKeyword(keyword, true);
|
||||
if (tabs.length === 0) {
|
||||
throw new Error('No matching buffer for ' + keyword);
|
||||
} else if (tabs.length > 1) {
|
||||
throw new Error('More than one match for ' + keyword);
|
||||
}
|
||||
browser.tabs.remove(tabs[0].id);
|
||||
});
|
||||
return browser.tabs.remove(tabs[0].id);
|
||||
};
|
||||
|
||||
const closeTabsByKeywords = (keyword) => {
|
||||
tabCompletions.getCompletions(keyword).then((tabs) => {
|
||||
let tabs2 = tabs.filter(tab => !tab.pinned);
|
||||
browser.tabs.remove(tabs2.map(tab => tab.id));
|
||||
});
|
||||
const closeTabsByKeywords = async(keyword) => {
|
||||
let tabs = await tabCompletions.getCompletions(keyword);
|
||||
tabs = tabs.filter(tab => !tab.pinned);
|
||||
return browser.tabs.remove(tabs.map(tab => tab.id));
|
||||
};
|
||||
|
||||
const closeTabsByKeywordsForce = (keyword) => {
|
||||
tabCompletions.getCompletions(keyword).then((tabs) => {
|
||||
browser.tabs.remove(tabs.map(tab => tab.id));
|
||||
});
|
||||
const closeTabsByKeywordsForce = async(keyword) => {
|
||||
let tabs = await tabCompletions.getCompletions(keyword);
|
||||
return browser.tabs.remove(tabs.map(tab => tab.id));
|
||||
};
|
||||
|
||||
const reopenTab = () => {
|
||||
let window = null;
|
||||
return browser.windows.getCurrent().then().then((w) => {
|
||||
window = w;
|
||||
return browser.sessions.getRecentlyClosed();
|
||||
}).then((sessions) => {
|
||||
const reopenTab = async() => {
|
||||
let window = await browser.windows.getCurrent();
|
||||
let sessions = await browser.sessions.getRecentlyClosed();
|
||||
let session = sessions.find((s) => {
|
||||
return s.tab && s.tab.windowId === window.id;
|
||||
});
|
||||
|
@ -74,11 +65,10 @@ const reopenTab = () => {
|
|||
return browser.sessions.restore(session.tab.sessionId);
|
||||
}
|
||||
return browser.sessions.restore(session.window.sessionId);
|
||||
});
|
||||
};
|
||||
|
||||
const selectAt = (index) => {
|
||||
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
|
||||
const selectAt = async(index) => {
|
||||
let tabs = await browser.tabs.query({ currentWindow: true });
|
||||
if (tabs.length < 2) {
|
||||
return;
|
||||
}
|
||||
|
@ -87,11 +77,10 @@ const selectAt = (index) => {
|
|||
}
|
||||
let id = tabs[index].id;
|
||||
return browser.tabs.update(id, { active: true });
|
||||
});
|
||||
};
|
||||
|
||||
const selectByKeyword = (current, keyword) => {
|
||||
return queryByKeyword(keyword).then((tabs) => {
|
||||
const selectByKeyword = async(current, keyword) => {
|
||||
let tabs = await queryByKeyword(keyword);
|
||||
if (tabs.length === 0) {
|
||||
throw new RangeError('No matching buffer for ' + keyword);
|
||||
}
|
||||
|
@ -101,43 +90,38 @@ const selectByKeyword = (current, keyword) => {
|
|||
}
|
||||
}
|
||||
return browser.tabs.update(tabs[0].id, { active: true });
|
||||
});
|
||||
};
|
||||
|
||||
const selectPrevTab = (current, count) => {
|
||||
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
|
||||
const selectPrevTab = async(current, count) => {
|
||||
let tabs = await browser.tabs.query({ currentWindow: true });
|
||||
if (tabs.length < 2) {
|
||||
return;
|
||||
}
|
||||
let select = (current - count + tabs.length) % tabs.length;
|
||||
let id = tabs[select].id;
|
||||
return browser.tabs.update(id, { active: true });
|
||||
});
|
||||
};
|
||||
|
||||
const selectNextTab = (current, count) => {
|
||||
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
|
||||
const selectNextTab = async(current, count) => {
|
||||
let tabs = await browser.tabs.query({ currentWindow: true });
|
||||
if (tabs.length < 2) {
|
||||
return;
|
||||
}
|
||||
let select = (current + count) % tabs.length;
|
||||
let id = tabs[select].id;
|
||||
return browser.tabs.update(id, { active: true });
|
||||
});
|
||||
};
|
||||
|
||||
const selectFirstTab = () => {
|
||||
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
|
||||
const selectFirstTab = async() => {
|
||||
let tabs = await browser.tabs.query({ currentWindow: true });
|
||||
let id = tabs[0].id;
|
||||
return browser.tabs.update(id, { active: true });
|
||||
});
|
||||
};
|
||||
|
||||
const selectLastTab = () => {
|
||||
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
|
||||
const selectLastTab = async() => {
|
||||
let tabs = await browser.tabs.query({ currentWindow: true });
|
||||
let id = tabs[tabs.length - 1].id;
|
||||
return browser.tabs.update(id, { active: true });
|
||||
});
|
||||
};
|
||||
|
||||
const selectTab = (id) => {
|
||||
|
@ -152,14 +136,11 @@ const reload = (current, cache) => {
|
|||
};
|
||||
|
||||
const updateTabPinned = (current, pinned) => {
|
||||
return browser.tabs.query({ currentWindow: true, active: true })
|
||||
.then(() => {
|
||||
return browser.tabs.update(current.id, { pinned: pinned });
|
||||
});
|
||||
return browser.tabs.update(current.id, { pinned });
|
||||
};
|
||||
|
||||
const toggleTabPinned = (current) => {
|
||||
updateTabPinned(current, !current.pinned);
|
||||
return updateTabPinned(current, !current.pinned);
|
||||
};
|
||||
|
||||
const duplicate = (id) => {
|
||||
|
|
|
@ -9,26 +9,20 @@ const ZOOM_SETTINGS = [
|
|||
1.10, 1.25, 1.50, 1.75, 2.00, 2.50, 3.00
|
||||
];
|
||||
|
||||
const zoomIn = (tabId = undefined) => {
|
||||
return browser.tabs.getZoom(tabId).then((factor) => {
|
||||
for (let f of ZOOM_SETTINGS) {
|
||||
if (f > factor) {
|
||||
browser.tabs.setZoom(tabId, f);
|
||||
break;
|
||||
const zoomIn = async(tabId = undefined) => {
|
||||
let current = await browser.tabs.getZoom(tabId);
|
||||
let factor = ZOOM_SETTINGS.find(f => f > current);
|
||||
if (factor) {
|
||||
return browser.tabs.setZoom(tabId, factor);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const zoomOut = (tabId = undefined) => {
|
||||
return browser.tabs.getZoom(tabId).then((factor) => {
|
||||
for (let f of [].concat(ZOOM_SETTINGS).reverse()) {
|
||||
if (f < factor) {
|
||||
browser.tabs.setZoom(tabId, f);
|
||||
break;
|
||||
const zoomOut = async(tabId = undefined) => {
|
||||
let current = await browser.tabs.getZoom(tabId);
|
||||
let factor = [].concat(ZOOM_SETTINGS).reverse().find(f => f < current);
|
||||
if (factor) {
|
||||
return browser.tabs.setZoom(tabId, factor);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const neutral = (tabId = undefined) => {
|
||||
|
|
Reference in a new issue