adnbn
Version:
Addon Bone - Cross-browser web extension framework with shared code base
56 lines • 1.61 kB
JavaScript
import { onActionClicked, onSpecificCommand } from "@addon-core/browser";
import Builder from "./../../entry/core/Builder.js";
import { isValidCommandExecuteFunction, isValidCommandName } from "./resolvers/index.js";
import {
CommandExecuteActionName
} from "./../../types/command.js";
class Builder_default extends Builder {
definition;
unsubscribe;
constructor(definition) {
super();
const { name, execute } = definition;
if (!isValidCommandExecuteFunction(execute)) {
throw new Error("The command entrypoint must export a execute function");
}
if (!isValidCommandName(name)) {
throw new Error("The command entrypoint must export a name string");
}
this.definition = {
...definition,
name,
execute
};
}
async build() {
await this.destroy();
const { name } = this.definition;
if (name == CommandExecuteActionName) {
this.unsubscribe = onActionClicked((tab) => {
this.handle(tab);
});
return;
}
this.unsubscribe = onSpecificCommand(name, (tab) => {
this.handle(tab);
});
}
async destroy() {
var _a;
(_a = this.unsubscribe) == null ? void 0 : _a.call(this);
}
handle(tab) {
const { name, execute, ...options } = this.definition;
try {
Promise.resolve(execute(tab, { name, ...options })).catch((e) => {
console.error("The command execute async function crashed:", e);
});
} catch (e) {
console.error("The command execute function crashed:", e);
}
}
}
export {
Builder_default as default
};
//# sourceMappingURL=Builder.js.map