Skip to content

Commit

Permalink
client/commands: statically import commands
Browse files Browse the repository at this point in the history
Dynamic imports won't work very well with modules and we don't
really need them, it's just there to save us an import statement.

Let's flip this to a static version.
  • Loading branch information
brunnre8 committed May 4, 2024
1 parent 9ae9482 commit 3fbbc39
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 39 deletions.
7 changes: 2 additions & 5 deletions client/components/ChatInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
import Mousetrap from "mousetrap";
import {wrapCursor} from "undate";
import autocompletion from "../js/autocompletion";
import commands from "../js/commands/index";
import {commands} from "../js/commands/index";
import socket from "../js/socket";
import upload from "../js/upload";
import eventbus from "../js/eventbus";
Expand Down Expand Up @@ -185,10 +185,7 @@ export default defineComponent({
return false;
}
if (
Object.prototype.hasOwnProperty.call(commands, cmd) &&
commands[cmd].input(args)
) {
if (Object.prototype.hasOwnProperty.call(commands, cmd) && commands[cmd](args)) {
return false;
}
}
Expand Down
6 changes: 2 additions & 4 deletions client/js/commands/collapse.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import socket from "../socket";
import {store} from "../store";

function input() {
export function input(): boolean {
if (!store.state.activeChannel) {
return;
return false;
}

const messageIds: number[] = [];
Expand Down Expand Up @@ -34,5 +34,3 @@ function input() {

return true;
}

export default {input};
6 changes: 2 additions & 4 deletions client/js/commands/expand.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import socket from "../socket";
import {store} from "../store";

function input() {
export function input(): boolean {
if (!store.state.activeChannel) {
return;
return false;
}

const messageIds: number[] = [];
Expand Down Expand Up @@ -34,5 +34,3 @@ function input() {

return true;
}

export default {input};
30 changes: 11 additions & 19 deletions client/js/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
// Taken from views/index.js

// This creates a version of `require()` in the context of the current
// directory, so we iterate over its content, which is a map statically built by
// Webpack.
// Second argument says it's recursive, third makes sure we only load javascript.
const commands = require.context("./", true, /\.ts$/);

export default commands.keys().reduce<Record<string, unknown>>((acc, path) => {
const command = path.substring(2, path.length - 3);

if (command === "index") {
return acc;
}

acc[command] = commands(path).default;

return acc;
}, {});
import {input as collapse} from "./collapse";
import {input as expand} from "./expand";
import {input as join} from "./join";
import {input as search} from "./search";

export const commands = {
collapse: collapse,
expand: expand,
join: join,
search: search,
};
9 changes: 5 additions & 4 deletions client/js/commands/join.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import socket from "../socket";
import {store} from "../store";
import {switchToChannel} from "../router";
import {ChanType} from "../../../shared/types/chan";

function input(args: string[]) {
export function input(args: string[]): boolean {
if (args.length > 0) {
let channels = args[0];

Expand Down Expand Up @@ -35,7 +36,7 @@ function input(args: string[]) {
return true;
}
}
} else if (store.state.activeChannel?.channel.type === "channel") {
} else if (store.state.activeChannel?.channel.type === ChanType.CHANNEL) {
// If `/join` command is used without any arguments, re-join current channel
socket.emit("input", {
target: store.state.activeChannel.channel.id,
Expand All @@ -44,6 +45,6 @@ function input(args: string[]) {

return true;
}
}

export default {input};
return false;
}
4 changes: 1 addition & 3 deletions client/js/commands/search.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {store} from "../store";
import {router} from "../router";

function input(args: string[]) {
export function input(args: string[]): boolean {
if (!store.state.settings.searchEnabled) {
return false;
}
Expand All @@ -23,5 +23,3 @@ function input(args: string[]) {

return true;
}

export default {input};

0 comments on commit 3fbbc39

Please sign in to comment.