aboutsummaryrefslogtreecommitdiff
path: root/ext/js/comm/api.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2024-01-20 23:13:17 -0500
committerGitHub <noreply@github.com>2024-01-21 04:13:17 +0000
commit6ba1ffe74558dd174e3308d48885fb068fa37d55 (patch)
tree5519bbf972096e0e3370171d2b62a26d6164d671 /ext/js/comm/api.js
parentebdde1ee612a262256ad0384131e53bc29b1e10f (diff)
WebExtension class (#551)
* Add WebExtension class * Use WebExtension class * Use WebExtension instance for all runtime message sending * Use getUrl * Add a sendMessage variant which ignores the response and error
Diffstat (limited to 'ext/js/comm/api.js')
-rw-r--r--ext/js/comm/api.js27
1 files changed, 11 insertions, 16 deletions
diff --git a/ext/js/comm/api.js b/ext/js/comm/api.js
index 50814aa2..2e1e8826 100644
--- a/ext/js/comm/api.js
+++ b/ext/js/comm/api.js
@@ -20,11 +20,11 @@ import {ExtensionError} from '../core/extension-error.js';
export class API {
/**
- * @param {import('../yomitan.js').Yomitan} yomitan
+ * @param {import('../extension/web-extension.js').WebExtension} webExtension
*/
- constructor(yomitan) {
- /** @type {import('../yomitan.js').Yomitan} */
- this._yomitan = yomitan;
+ constructor(webExtension) {
+ /** @type {import('../extension/web-extension.js').WebExtension} */
+ this._webExtension = webExtension;
}
/**
@@ -375,13 +375,15 @@ export class API {
const data = {action, params};
return new Promise((resolve, reject) => {
try {
- this._yomitan.sendMessage(data, (response) => {
- this._checkLastError(chrome.runtime.lastError);
+ this._webExtension.sendMessage(data, (response) => {
+ this._webExtension.getLastError();
if (response !== null && typeof response === 'object') {
- if (typeof response.error !== 'undefined') {
- reject(ExtensionError.deserialize(response.error));
+ const {error} = /** @type {import('core').UnknownObject} */ (response);
+ if (typeof error !== 'undefined') {
+ reject(ExtensionError.deserialize(/** @type {import('core').SerializedError} */ (error)));
} else {
- resolve(response.result);
+ const {result} = /** @type {import('core').UnknownObject} */ (response);
+ resolve(/** @type {import('api').ApiReturn<TAction>} */ (result));
}
} else {
const message = response === null ? 'Unexpected null response' : `Unexpected response of type ${typeof response}`;
@@ -393,11 +395,4 @@ export class API {
}
});
}
-
- /**
- * @param {chrome.runtime.LastError|undefined} _ignore
- */
- _checkLastError(_ignore) {
- // NOP
- }
}