minor api cleanup stuff

pull/98/head
dperolio 2 years ago
parent ec386fab86
commit 27a40cefb5
No known key found for this signature in database
GPG Key ID: 4191689562D51409

@ -22,7 +22,7 @@ export default class Actions extends API {
/** /**
* All currently registered actions. * All currently registered actions.
*/ */
this._actions = []; this.actions = [];
} }
/** /**
@ -53,7 +53,7 @@ export default class Actions extends API {
throw new TypeError('Action executor must be a function!'); throw new TypeError('Action executor must be a function!');
} }
const caller = getCaller(); const caller = getCaller();
this._actions.push({ this.actions.push({
id: actionId, id: actionId,
executor, executor,
caller caller
@ -106,7 +106,7 @@ export default class Actions extends API {
getAction (filter) { getAction (filter) {
try { try {
if (!filter?.length) return null; if (!filter?.length) return null;
return this._actions.find(filter); return this.actions.find(filter);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('getAction'), err); return this.error(this._labels.concat('getAction'), err);
} }
@ -120,7 +120,7 @@ export default class Actions extends API {
getActionById (actionId) { getActionById (actionId) {
try { try {
assertString(actionId); assertString(actionId);
return this._actions.find(action => action.id === actionId); return this.actions.find(action => action.id === actionId);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('getActionById'), err); return this.error(this._labels.concat('getActionById'), err);
} }
@ -134,7 +134,7 @@ export default class Actions extends API {
getActions (filter) { getActions (filter) {
try { try {
if (!filter?.length) return null; if (!filter?.length) return null;
return this._actions.filter(filter); return this.actions.filter(filter);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('getActions'), err); return this.error(this._labels.concat('getActions'), err);
} }
@ -148,7 +148,7 @@ export default class Actions extends API {
getActionsByCaller (addonId) { getActionsByCaller (addonId) {
try { try {
assertString(addonId); assertString(addonId);
return this._actions.filter(action => action.caller?.id === addonId); return this.actions.filter(action => action.caller?.id === addonId);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('getActionsByCaller'), err); return this.error(this._labels.concat('getActionsByCaller'), err);
} }
@ -160,7 +160,7 @@ export default class Actions extends API {
*/ */
getAllActions () { getAllActions () {
try { try {
return this._actions; return this.actions;
} catch (err) { } catch (err) {
return this.error(this._labels.concat('getAllActions'), err); return this.error(this._labels.concat('getAllActions'), err);
} }
@ -175,7 +175,7 @@ export default class Actions extends API {
try { try {
assertString(actionId); assertString(actionId);
if (this.isAction(actionId)) { if (this.isAction(actionId)) {
this._actions = this.getActions(action => action.id !== actionId); this.actions = this.getActions(action => action.id !== actionId);
this.emit(Events.VIZALITY_ACTION_REMOVE, actionId); this.emit(Events.VIZALITY_ACTION_REMOVE, actionId);
} else { } else {
throw new Error(`Action "${actionId}" is not registered, so it cannot be unregistered!`); throw new Error(`Action "${actionId}" is not registered, so it cannot be unregistered!`);
@ -193,7 +193,7 @@ export default class Actions extends API {
unregisterActionsByCaller (addonId) { unregisterActionsByCaller (addonId) {
try { try {
assertString(addonId); assertString(addonId);
this._actions = this._actions.filter(action => action.caller?.id !== addonId); this.actions = this.actions.filter(action => action.caller?.id !== addonId);
this.emit(Events.VIZALITY_ACTION_REMOVE_ALL_BY_CALLER, addonId); this.emit(Events.VIZALITY_ACTION_REMOVE_ALL_BY_CALLER, addonId);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('unregisterActionsByCaller'), err); return this.error(this._labels.concat('unregisterActionsByCaller'), err);
@ -206,7 +206,7 @@ export default class Actions extends API {
*/ */
unregisterAllActions () { unregisterAllActions () {
try { try {
this._actions = []; this.actions = [];
this.emit(Events.VIZALITY_ACTION_REMOVE_ALL); this.emit(Events.VIZALITY_ACTION_REMOVE_ALL);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('unregisterAllActions'), err); return this.error(this._labels.concat('unregisterAllActions'), err);

@ -8,13 +8,6 @@
* @version 1.0.0 * @version 1.0.0
*/ */
/*
* @todo Work out a better system for providing subcommands to make it much easier and less
* painful; preferably getting rid of the autocomplete property and adding a subcommands array
* property that would contain an array of VizalityCommand and would be processed like normal
* commands, but as subcommands.
*/
/** /**
* Vizality command object. * Vizality command object.
* @typedef VizalityCommand * @typedef VizalityCommand
@ -50,17 +43,19 @@ import { Events } from '@vizality/constants';
import { Icon } from '@vizality/components'; import { Icon } from '@vizality/components';
import { API } from '@vizality/entities'; import { API } from '@vizality/entities';
/**
* All currently registered commands.
* Accessed with `getAllCommands` below.
*/
let commands = [];
/** /**
* @extends API * @extends API
* @extends Events * @extends Events
*/ */
export default class Commands extends API { export default class Commands extends API {
constructor () {
super();
/**
* All currently registered actions.
*/
this.commands = [];
}
/** /**
* Shuts down the API, removing all listeners and stored objects. * Shuts down the API, removing all listeners and stored objects.
*/ */
@ -113,7 +108,7 @@ export default class Commands extends API {
command.aliases = command.aliases.map(alias => alias.toLowerCase()); command.aliases = command.aliases.map(alias => alias.toLowerCase());
} }
command.caller = getCaller(); command.caller = getCaller();
commands.push(command); this.commands.push(command);
this.emit(Events.VIZALITY_COMMAND_ADD, command.command); this.emit(Events.VIZALITY_COMMAND_ADD, command.command);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('registerCommand'), err); return this.error(this._labels.concat('registerCommand'), err);
@ -176,7 +171,7 @@ export default class Commands extends API {
getCommand (filter) { getCommand (filter) {
try { try {
if (!filter?.length) return null; if (!filter?.length) return null;
return commands.find(filter); return this.commands.find(filter);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('getCommand'), err); return this.error(this._labels.concat('getCommand'), err);
} }
@ -190,7 +185,7 @@ export default class Commands extends API {
getCommandByName (commandName) { getCommandByName (commandName) {
try { try {
assertString(commandName); assertString(commandName);
return commands.find(command => command.command === commandName); return this.commands.find(command => command.command === commandName);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('getCommandByName'), err); return this.error(this._labels.concat('getCommandByName'), err);
} }
@ -204,7 +199,7 @@ export default class Commands extends API {
getCommands (filter) { getCommands (filter) {
try { try {
if (!filter?.length) return null; if (!filter?.length) return null;
return commands.filter(filter); return this.commands.filter(filter);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('getCommands'), err); return this.error(this._labels.concat('getCommands'), err);
} }
@ -218,7 +213,7 @@ export default class Commands extends API {
getCommandsByCaller (addonId) { getCommandsByCaller (addonId) {
try { try {
assertString(addonId); assertString(addonId);
return commands.filter(command => command.caller?.id === addonId); return this.commands.filter(command => command.caller?.id === addonId);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('getCommandsByCaller'), err); return this.error(this._labels.concat('getCommandsByCaller'), err);
} }
@ -230,7 +225,7 @@ export default class Commands extends API {
*/ */
getAllCommands () { getAllCommands () {
try { try {
return commands; return this.commands;
} catch (err) { } catch (err) {
return this.error(this._labels.concat('getAllCommands'), err); return this.error(this._labels.concat('getAllCommands'), err);
} }
@ -247,7 +242,7 @@ export default class Commands extends API {
if (!this.isCommand(commandName)) { if (!this.isCommand(commandName)) {
throw new Error(`Command "${commandName}" is not registered, so it cannot be unregistered!`); throw new Error(`Command "${commandName}" is not registered, so it cannot be unregistered!`);
} }
commands = this.getCommands(command => command.command !== commandName); this.commands = this.getCommands(command => command.command !== commandName);
this.emit(Events.VIZALITY_COMMAND_REMOVE, commandName); this.emit(Events.VIZALITY_COMMAND_REMOVE, commandName);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('unregisterCommand'), err); return this.error(this._labels.concat('unregisterCommand'), err);
@ -262,7 +257,7 @@ export default class Commands extends API {
unregisterCommandsByCaller (addonId) { unregisterCommandsByCaller (addonId) {
try { try {
assertString(addonId); assertString(addonId);
commands = commands.filter(command => command.caller?.id !== addonId); this.commands = this.commands.filter(command => command.caller?.id !== addonId);
this.emit(Events.VIZALITY_COMMAND_REMOVE_ALL_BY_CALLER, addonId); this.emit(Events.VIZALITY_COMMAND_REMOVE_ALL_BY_CALLER, addonId);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('unregisterCommandsByCaller'), err); return this.error(this._labels.concat('unregisterCommandsByCaller'), err);
@ -275,7 +270,7 @@ export default class Commands extends API {
*/ */
unregisterAllCommands () { unregisterAllCommands () {
try { try {
commands = []; this.commands = [];
this.emit(Events.VIZALITY_COMMAND_REMOVE_ALL); this.emit(Events.VIZALITY_COMMAND_REMOVE_ALL);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('unregisterAllCommands'), err); return this.error(this._labels.concat('unregisterAllCommands'), err);

@ -39,11 +39,11 @@ export default class Routes extends API {
/** /**
* All currently registered routes. * All currently registered routes.
*/ */
this._routes = []; this.routes = [];
} }
getAllRoutes () { getAllRoutes () {
return this._routes; return this.routes;
} }
/** /**
@ -141,8 +141,8 @@ export default class Routes extends API {
if (!route.hasOwnProperty('sidebar')) { if (!route.hasOwnProperty('sidebar')) {
route.sidebar = Sidebar; route.sidebar = Sidebar;
} }
this._routes.push(route); this.routes.push(route);
if (this._routes[this._routes.length - 1]?.id !== 'dashboard') { if (this.routes[this.routes.length - 1]?.id !== 'dashboard') {
this._reregisterDashboardRoutes(); this._reregisterDashboardRoutes();
} }
this.emit(Events.VIZALITY_ROUTE_ADD, route.id, route.path); this.emit(Events.VIZALITY_ROUTE_ADD, route.id, route.path);
@ -159,7 +159,7 @@ export default class Routes extends API {
getRoutesByCaller (addonId) { getRoutesByCaller (addonId) {
try { try {
assertString(addonId); assertString(addonId);
return this._routes.filter(route => route.caller?.id === addonId); return this.routes.filter(route => route.caller?.id === addonId);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('getRoutesByCaller'), err); return this.error(this._labels.concat('getRoutesByCaller'), err);
} }
@ -173,7 +173,7 @@ export default class Routes extends API {
getRoutes (filter) { getRoutes (filter) {
try { try {
if (!filter?.length) return null; if (!filter?.length) return null;
return this._routes.filter(filter); return this.routes.filter(filter);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('getRoutes'), err); return this.error(this._labels.concat('getRoutes'), err);
} }
@ -190,13 +190,13 @@ export default class Routes extends API {
throw new Error(`Invalid route path or route ID provided!`); throw new Error(`Invalid route path or route ID provided!`);
} }
if (pathOrRouteId.startsWith('/')) { if (pathOrRouteId.startsWith('/')) {
if (!this._routes.find(r => r.path === pathOrRouteId)) { if (!this.routes.find(r => r.path === pathOrRouteId)) {
throw new Error(`Route path "${pathOrRouteId}" is not registered, so it cannot be unregistered!`); throw new Error(`Route path "${pathOrRouteId}" is not registered, so it cannot be unregistered!`);
} }
} else if (!this._routes.find(r => r.id === pathOrRouteId)) { } else if (!this.routes.find(r => r.id === pathOrRouteId)) {
throw new Error(`Route ID "${pathOrRouteId}" is not registered, so it cannot be unregistered!`); throw new Error(`Route ID "${pathOrRouteId}" is not registered, so it cannot be unregistered!`);
} }
this._routes = this.getRoutes(route => route.id !== pathOrRouteId && route.path !== pathOrRouteId); this.routes = this.getRoutes(route => route.id !== pathOrRouteId && route.path !== pathOrRouteId);
this.emit('routeRemove', pathOrRouteId); this.emit('routeRemove', pathOrRouteId);
} catch (err) { } catch (err) {
return this.error(err); return this.error(err);
@ -211,7 +211,7 @@ export default class Routes extends API {
isRoute (routeIdOrPath) { isRoute (routeIdOrPath) {
try { try {
assertString(routeIdOrPath); assertString(routeIdOrPath);
return this._routes.some(route => route.id === routeIdOrPath || route.path === routeIdOrPath); return this.routes.some(route => route.id === routeIdOrPath || route.path === routeIdOrPath);
} catch (err) { } catch (err) {
return this.error(this._labels.concat('isRoute'), err); return this.error(this._labels.concat('isRoute'), err);
} }
@ -377,7 +377,7 @@ export default class Routes extends API {
*/ */
_reregisterDashboardRoutes () { _reregisterDashboardRoutes () {
try { try {
if (!this._routes.find(route => route.id === 'dashboard')) { if (!this.routes.find(route => route.id === 'dashboard')) {
return; return;
} }
this.unregisterRoute('dashboard'); this.unregisterRoute('dashboard');

Loading…
Cancel
Save