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.
*/
this._actions = [];
this.actions = [];
}
/**
@ -53,7 +53,7 @@ export default class Actions extends API {
throw new TypeError('Action executor must be a function!');
}
const caller = getCaller();
this._actions.push({
this.actions.push({
id: actionId,
executor,
caller
@ -106,7 +106,7 @@ export default class Actions extends API {
getAction (filter) {
try {
if (!filter?.length) return null;
return this._actions.find(filter);
return this.actions.find(filter);
} catch (err) {
return this.error(this._labels.concat('getAction'), err);
}
@ -120,7 +120,7 @@ export default class Actions extends API {
getActionById (actionId) {
try {
assertString(actionId);
return this._actions.find(action => action.id === actionId);
return this.actions.find(action => action.id === actionId);
} catch (err) {
return this.error(this._labels.concat('getActionById'), err);
}
@ -134,7 +134,7 @@ export default class Actions extends API {
getActions (filter) {
try {
if (!filter?.length) return null;
return this._actions.filter(filter);
return this.actions.filter(filter);
} catch (err) {
return this.error(this._labels.concat('getActions'), err);
}
@ -148,7 +148,7 @@ export default class Actions extends API {
getActionsByCaller (addonId) {
try {
assertString(addonId);
return this._actions.filter(action => action.caller?.id === addonId);
return this.actions.filter(action => action.caller?.id === addonId);
} catch (err) {
return this.error(this._labels.concat('getActionsByCaller'), err);
}
@ -160,7 +160,7 @@ export default class Actions extends API {
*/
getAllActions () {
try {
return this._actions;
return this.actions;
} catch (err) {
return this.error(this._labels.concat('getAllActions'), err);
}
@ -175,7 +175,7 @@ export default class Actions extends API {
try {
assertString(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);
} else {
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) {
try {
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);
} catch (err) {
return this.error(this._labels.concat('unregisterActionsByCaller'), err);
@ -206,7 +206,7 @@ export default class Actions extends API {
*/
unregisterAllActions () {
try {
this._actions = [];
this.actions = [];
this.emit(Events.VIZALITY_ACTION_REMOVE_ALL);
} catch (err) {
return this.error(this._labels.concat('unregisterAllActions'), err);

@ -8,13 +8,6 @@
* @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.
* @typedef VizalityCommand
@ -50,17 +43,19 @@ import { Events } from '@vizality/constants';
import { Icon } from '@vizality/components';
import { API } from '@vizality/entities';
/**
* All currently registered commands.
* Accessed with `getAllCommands` below.
*/
let commands = [];
/**
* @extends API
* @extends Events
*/
export default class Commands extends API {
constructor () {
super();
/**
* All currently registered actions.
*/
this.commands = [];
}
/**
* 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.caller = getCaller();
commands.push(command);
this.commands.push(command);
this.emit(Events.VIZALITY_COMMAND_ADD, command.command);
} catch (err) {
return this.error(this._labels.concat('registerCommand'), err);
@ -176,7 +171,7 @@ export default class Commands extends API {
getCommand (filter) {
try {
if (!filter?.length) return null;
return commands.find(filter);
return this.commands.find(filter);
} catch (err) {
return this.error(this._labels.concat('getCommand'), err);
}
@ -190,7 +185,7 @@ export default class Commands extends API {
getCommandByName (commandName) {
try {
assertString(commandName);
return commands.find(command => command.command === commandName);
return this.commands.find(command => command.command === commandName);
} catch (err) {
return this.error(this._labels.concat('getCommandByName'), err);
}
@ -204,7 +199,7 @@ export default class Commands extends API {
getCommands (filter) {
try {
if (!filter?.length) return null;
return commands.filter(filter);
return this.commands.filter(filter);
} catch (err) {
return this.error(this._labels.concat('getCommands'), err);
}
@ -218,7 +213,7 @@ export default class Commands extends API {
getCommandsByCaller (addonId) {
try {
assertString(addonId);
return commands.filter(command => command.caller?.id === addonId);
return this.commands.filter(command => command.caller?.id === addonId);
} catch (err) {
return this.error(this._labels.concat('getCommandsByCaller'), err);
}
@ -230,7 +225,7 @@ export default class Commands extends API {
*/
getAllCommands () {
try {
return commands;
return this.commands;
} catch (err) {
return this.error(this._labels.concat('getAllCommands'), err);
}
@ -247,7 +242,7 @@ export default class Commands extends API {
if (!this.isCommand(commandName)) {
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);
} catch (err) {
return this.error(this._labels.concat('unregisterCommand'), err);
@ -262,7 +257,7 @@ export default class Commands extends API {
unregisterCommandsByCaller (addonId) {
try {
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);
} catch (err) {
return this.error(this._labels.concat('unregisterCommandsByCaller'), err);
@ -275,7 +270,7 @@ export default class Commands extends API {
*/
unregisterAllCommands () {
try {
commands = [];
this.commands = [];
this.emit(Events.VIZALITY_COMMAND_REMOVE_ALL);
} catch (err) {
return this.error(this._labels.concat('unregisterAllCommands'), err);

@ -39,11 +39,11 @@ export default class Routes extends API {
/**
* All currently registered routes.
*/
this._routes = [];
this.routes = [];
}
getAllRoutes () {
return this._routes;
return this.routes;
}
/**
@ -141,8 +141,8 @@ export default class Routes extends API {
if (!route.hasOwnProperty('sidebar')) {
route.sidebar = Sidebar;
}
this._routes.push(route);
if (this._routes[this._routes.length - 1]?.id !== 'dashboard') {
this.routes.push(route);
if (this.routes[this.routes.length - 1]?.id !== 'dashboard') {
this._reregisterDashboardRoutes();
}
this.emit(Events.VIZALITY_ROUTE_ADD, route.id, route.path);
@ -159,7 +159,7 @@ export default class Routes extends API {
getRoutesByCaller (addonId) {
try {
assertString(addonId);
return this._routes.filter(route => route.caller?.id === addonId);
return this.routes.filter(route => route.caller?.id === addonId);
} catch (err) {
return this.error(this._labels.concat('getRoutesByCaller'), err);
}
@ -173,7 +173,7 @@ export default class Routes extends API {
getRoutes (filter) {
try {
if (!filter?.length) return null;
return this._routes.filter(filter);
return this.routes.filter(filter);
} catch (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!`);
}
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!`);
}
} 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!`);
}
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);
} catch (err) {
return this.error(err);
@ -211,7 +211,7 @@ export default class Routes extends API {
isRoute (routeIdOrPath) {
try {
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) {
return this.error(this._labels.concat('isRoute'), err);
}
@ -377,7 +377,7 @@ export default class Routes extends API {
*/
_reregisterDashboardRoutes () {
try {
if (!this._routes.find(route => route.id === 'dashboard')) {
if (!this.routes.find(route => route.id === 'dashboard')) {
return;
}
this.unregisterRoute('dashboard');

Loading…
Cancel
Save