@discoveryjs/discovery
Version:
Frontend framework for rapid data (JSON) analysis, shareable serverless reports and dashboards
41 lines (40 loc) • 982 B
JavaScript
import { Dictionary } from "./dict.js";
export class ActionManager extends Dictionary {
#actionMap;
constructor() {
super(true);
this.#actionMap = /* @__PURE__ */ Object.create(null);
}
define(name, callback) {
if (typeof callback !== "function") {
throw new Error("callback is not a function");
}
this.#actionMap = Object.freeze({
...this.#actionMap,
[name]: callback
});
return ActionManager.define(this, name, Object.freeze({
name,
callback
}));
}
revoke(name) {
if (this.has(name)) {
const map = { ...this.#actionMap };
delete map[name];
this.#actionMap = Object.freeze(map);
}
super.revoke(name);
}
get actionMap() {
return this.#actionMap;
}
call(name, ...args) {
const action = this.get(name);
if (action === void 0) {
throw new Error(`action "${name}" doesn't exist`);
}
const { callback } = action;
return callback(...args);
}
}