Send message updates (#864)
* Use _sendMessageTab * Move _sendMessageAllTabs next to _sendMessageTab * Rename function * Add and use _sendMessageTabIgnoreResponse * Add and use _sendMessageIgnoreResponse * Always include params * Update function consistency
This commit is contained in:
parent
cab5daa22e
commit
f0fc4bfee6
@ -201,9 +201,8 @@ class Backend {
|
|||||||
|
|
||||||
this._clipboardMonitor.on('change', this._onClipboardTextChange.bind(this));
|
this._clipboardMonitor.on('change', this._onClipboardTextChange.bind(this));
|
||||||
|
|
||||||
this._sendMessageAllTabs('backendReady');
|
this._sendMessageAllTabsIgnoreResponse('backendReady', {});
|
||||||
const callback = () => this._checkLastError(chrome.runtime.lastError);
|
this._sendMessageIgnoreResponse({action: 'backendReady', params: {}});
|
||||||
chrome.runtime.sendMessage({action: 'backendReady'}, callback);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
yomichan.logError(e);
|
yomichan.logError(e);
|
||||||
throw e;
|
throw e;
|
||||||
@ -350,21 +349,19 @@ class Backend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_onZoomChange({tabId, oldZoomFactor, newZoomFactor}) {
|
_onZoomChange({tabId, oldZoomFactor, newZoomFactor}) {
|
||||||
const callback = () => this._checkLastError(chrome.runtime.lastError);
|
this._sendMessageTabIgnoreResponse(tabId, {action: 'zoomChanged', params: {oldZoomFactor, newZoomFactor}});
|
||||||
chrome.tabs.sendMessage(tabId, {action: 'zoomChanged', params: {oldZoomFactor, newZoomFactor}}, callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message handlers
|
// Message handlers
|
||||||
|
|
||||||
_onApiRequestBackendReadySignal(_params, sender) {
|
_onApiRequestBackendReadySignal(_params, sender) {
|
||||||
// tab ID isn't set in background (e.g. browser_action)
|
// tab ID isn't set in background (e.g. browser_action)
|
||||||
const callback = () => this._checkLastError(chrome.runtime.lastError);
|
const data = {action: 'backendReady', params: {}};
|
||||||
const data = {action: 'backendReady'};
|
|
||||||
if (typeof sender.tab === 'undefined') {
|
if (typeof sender.tab === 'undefined') {
|
||||||
chrome.runtime.sendMessage(data, callback);
|
this._sendMessageIgnoreResponse(data);
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
chrome.tabs.sendMessage(sender.tab.id, data, callback);
|
this._sendMessageTabIgnoreResponse(sender.tab.id, data);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -509,8 +506,7 @@ class Backend {
|
|||||||
|
|
||||||
const tabId = sender.tab.id;
|
const tabId = sender.tab.id;
|
||||||
const frameId = sender.frameId;
|
const frameId = sender.frameId;
|
||||||
const callback = () => this._checkLastError(chrome.runtime.lastError);
|
this._sendMessageTabIgnoreResponse(tabId, {action, params, frameId}, {frameId: targetFrameId});
|
||||||
chrome.tabs.sendMessage(tabId, {action, params, frameId}, {frameId: targetFrameId}, callback);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -521,8 +517,7 @@ class Backend {
|
|||||||
|
|
||||||
const tabId = sender.tab.id;
|
const tabId = sender.tab.id;
|
||||||
const frameId = sender.frameId;
|
const frameId = sender.frameId;
|
||||||
const callback = () => this._checkLastError(chrome.runtime.lastError);
|
this._sendMessageTabIgnoreResponse(tabId, {action, params, frameId});
|
||||||
chrome.tabs.sendMessage(tabId, {action, params, frameId}, callback);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -858,7 +853,7 @@ class Backend {
|
|||||||
const tab = tabs[0];
|
const tab = tabs[0];
|
||||||
await this._waitUntilTabFrameIsReady(tab.id, 0, 2000);
|
await this._waitUntilTabFrameIsReady(tab.id, 0, 2000);
|
||||||
|
|
||||||
await this._sendMessageTab(
|
await this._sendMessageTabPromise(
|
||||||
tab.id,
|
tab.id,
|
||||||
{action: 'setMode', params: {mode: 'popup'}},
|
{action: 'setMode', params: {mode: 'popup'}},
|
||||||
{frameId: 0}
|
{frameId: 0}
|
||||||
@ -869,22 +864,13 @@ class Backend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_updateSearchQuery(tabId, text, animate) {
|
_updateSearchQuery(tabId, text, animate) {
|
||||||
return this._sendMessageTab(
|
return this._sendMessageTabPromise(
|
||||||
tabId,
|
tabId,
|
||||||
{action: 'updateSearchQuery', params: {text, animate}},
|
{action: 'updateSearchQuery', params: {text, animate}},
|
||||||
{frameId: 0}
|
{frameId: 0}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
_sendMessageAllTabs(action, params={}) {
|
|
||||||
const callback = () => this._checkLastError(chrome.runtime.lastError);
|
|
||||||
chrome.tabs.query({}, (tabs) => {
|
|
||||||
for (const tab of tabs) {
|
|
||||||
chrome.tabs.sendMessage(tab.id, {action, params}, callback);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
_applyOptions(source) {
|
_applyOptions(source) {
|
||||||
const options = this.getOptions({current: true});
|
const options = this.getOptions({current: true});
|
||||||
this._updateBadge();
|
this._updateBadge();
|
||||||
@ -904,7 +890,7 @@ class Backend {
|
|||||||
this._clipboardMonitor.stop();
|
this._clipboardMonitor.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
this._sendMessageAllTabs('optionsUpdated', {source});
|
this._sendMessageAllTabsIgnoreResponse('optionsUpdated', {source});
|
||||||
}
|
}
|
||||||
|
|
||||||
_getProfile(optionsContext, useSchema=false) {
|
_getProfile(optionsContext, useSchema=false) {
|
||||||
@ -1269,7 +1255,7 @@ class Backend {
|
|||||||
|
|
||||||
async _getTabUrl(tabId) {
|
async _getTabUrl(tabId) {
|
||||||
try {
|
try {
|
||||||
const {url} = await this._sendMessageTab(
|
const {url} = await this._sendMessageTabPromise(
|
||||||
tabId,
|
tabId,
|
||||||
{action: 'getUrl', params: {}},
|
{action: 'getUrl', params: {}},
|
||||||
{frameId: 0}
|
{frameId: 0}
|
||||||
@ -1387,20 +1373,15 @@ class Backend {
|
|||||||
|
|
||||||
chrome.runtime.onMessage.addListener(onMessage);
|
chrome.runtime.onMessage.addListener(onMessage);
|
||||||
|
|
||||||
chrome.tabs.sendMessage(tabId, {action: 'isReady'}, {frameId}, (response) => {
|
this._sendMessageTabPromise(tabId, {action: 'isReady'}, {frameId})
|
||||||
const error = chrome.runtime.lastError;
|
.then(
|
||||||
if (error) { return; }
|
(value) => {
|
||||||
|
if (!value) { return; }
|
||||||
try {
|
cleanup();
|
||||||
const value = yomichan.getMessageResponseResult(response);
|
resolve();
|
||||||
if (!value) { return; }
|
},
|
||||||
|
() => {} // NOP
|
||||||
cleanup();
|
);
|
||||||
resolve();
|
|
||||||
} catch (e) {
|
|
||||||
// NOP
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (timeout !== null) {
|
if (timeout !== null) {
|
||||||
timer = setTimeout(() => {
|
timer = setTimeout(() => {
|
||||||
@ -1427,7 +1408,26 @@ class Backend {
|
|||||||
return await (json ? response.json() : response.text());
|
return await (json ? response.json() : response.text());
|
||||||
}
|
}
|
||||||
|
|
||||||
_sendMessageTab(...args) {
|
_sendMessageIgnoreResponse(...args) {
|
||||||
|
const callback = () => this._checkLastError(chrome.runtime.lastError);
|
||||||
|
chrome.runtime.sendMessage(...args, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
_sendMessageTabIgnoreResponse(...args) {
|
||||||
|
const callback = () => this._checkLastError(chrome.runtime.lastError);
|
||||||
|
chrome.tabs.sendMessage(...args, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
_sendMessageAllTabsIgnoreResponse(action, params) {
|
||||||
|
const callback = () => this._checkLastError(chrome.runtime.lastError);
|
||||||
|
chrome.tabs.query({}, (tabs) => {
|
||||||
|
for (const tab of tabs) {
|
||||||
|
chrome.tabs.sendMessage(tab.id, {action, params}, callback);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
_sendMessageTabPromise(...args) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const callback = (response) => {
|
const callback = (response) => {
|
||||||
try {
|
try {
|
||||||
@ -1482,7 +1482,7 @@ class Backend {
|
|||||||
if (typeof tabId === 'number' && typeof ownerFrameId === 'number') {
|
if (typeof tabId === 'number' && typeof ownerFrameId === 'number') {
|
||||||
const action = 'setAllVisibleOverride';
|
const action = 'setAllVisibleOverride';
|
||||||
const params = {value: false, priority: 0, awaitFrame: true};
|
const params = {value: false, priority: 0, awaitFrame: true};
|
||||||
token = await this._sendMessageTab(tabId, {action, params}, {frameId: ownerFrameId});
|
token = await this._sendMessageTabPromise(tabId, {action, params}, {frameId: ownerFrameId});
|
||||||
}
|
}
|
||||||
|
|
||||||
return await new Promise((resolve, reject) => {
|
return await new Promise((resolve, reject) => {
|
||||||
@ -1500,7 +1500,7 @@ class Backend {
|
|||||||
const action = 'clearAllVisibleOverride';
|
const action = 'clearAllVisibleOverride';
|
||||||
const params = {token};
|
const params = {token};
|
||||||
try {
|
try {
|
||||||
await this._sendMessageTab(tabId, {action, params}, {frameId: ownerFrameId});
|
await this._sendMessageTabPromise(tabId, {action, params}, {frameId: ownerFrameId});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// NOP
|
// NOP
|
||||||
}
|
}
|
||||||
@ -1651,7 +1651,7 @@ class Backend {
|
|||||||
|
|
||||||
_triggerDatabaseUpdated(type, cause) {
|
_triggerDatabaseUpdated(type, cause) {
|
||||||
this._translator.clearDatabaseCaches();
|
this._translator.clearDatabaseCaches();
|
||||||
this._sendMessageAllTabs('databaseUpdated', {type, cause});
|
this._sendMessageAllTabsIgnoreResponse('databaseUpdated', {type, cause});
|
||||||
}
|
}
|
||||||
|
|
||||||
async _saveOptions(source) {
|
async _saveOptions(source) {
|
||||||
|
Loading…
Reference in New Issue
Block a user