Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: re-implement command store module with pinia (#993)

Liu Ji16d95fead46a
diff --git a/frontend/src/components/QuickActionPanel.vue b/frontend/src/components/QuickActionPanel.vue
index 84b8997678..4b64191dec 100644
--- a/frontend/src/components/QuickActionPanel.vue
+++ b/frontend/src/components/QuickActionPanel.vue
@@ -269,6 +269,7 @@ import { idFromSlug } from "../utils";
import { Action, defineAction, useRegisterActions } from "@bytebase/vue-kbar";
import { useI18n } from "vue-i18n";
import { Subscription } from "../types";
+import { useCommandStore } from "@/store";
interface LocalState {
showModal: boolean;
@@ -298,6 +299,7 @@ export default defineComponent({
const { t } = useI18n();
const router = useRouter();
const store = useStore();
+ const commandStore = useCommandStore();
const state = reactive<LocalState>({
showModal: false,
@@ -381,11 +383,11 @@ export default defineComponent({
};
const createEnvironment = () => {
- store.dispatch("command/dispatchCommand", "bb.environment.create");
+ commandStore.dispatchCommand("bb.environment.create");
};
const reorderEnvironment = () => {
- store.dispatch("command/dispatchCommand", "bb.environment.reorder");
+ commandStore.dispatchCommand("bb.environment.reorder");
};
const QuickActionMap: Record<string, Partial<Action>> = {
diff --git a/frontend/src/store/modules/command.ts b/frontend/src/store/modules/command.ts
deleted file mode 100644
index 4e50939d8b..0000000000
--- a/frontend/src/store/modules/command.ts
+++ /dev/null
@@ -1,89 +0,0 @@
-import {
- Command,
- CommandId,
- CommandRegisterId,
- CommandState,
-} from "../../types";
-
-const state: () => CommandState = () => ({
- commandListById: new Map(),
-});
-
-const actions = {
- registerCommand({ commit }: any, newCommand: Command) {
- commit("appendCommand", newCommand);
- return newCommand;
- },
-
- unregisterCommand(
- { commit }: any,
- {
- id,
- registerId,
- }: {
- id: CommandId;
- registerId: CommandRegisterId;
- }
- ) {
- commit("removeCommand", {
- id,
- registerId,
- });
- },
-
- dispatchCommand({ state }: any, commandId: CommandId) {
- const list = state.commandListById.get(commandId);
- list?.forEach((cmd: Command) => {
- cmd.run();
- });
- },
-};
-
-const mutations = {
- appendCommand(state: CommandState, command: Command) {
- const list = state.commandListById.get(command.id);
- if (list) {
- const i = list.findIndex(
- (item) => item.registerId === command.registerId
- );
- if (i > -1) {
- throw new Error(
- `'${command.registerId}' has already registered command '${command.id}', this is likely a programming error.`
- );
- }
- list.push(command);
- } else {
- state.commandListById.set(command.id, [command]);
- }
- },
-
- removeCommand(
- state: CommandState,
- {
- id,
- registerId,
- }: {
- id: CommandId;
- registerId: CommandRegisterId;
- }
- ) {
- const list = state.commandListById.get(id);
- if (list) {
- const i = list.findIndex((item) => item.registerId === registerId);
- if (i >= 0) {
- list.splice(i, 1);
- return;
- }
- }
- throw new Error(
- `'${registerId}' attempts to unregister command '${id}' which is not registered before, this is likely a programming error.`
- );
- },
-};
-
-export default {
- namespaced: true,
- state,
- actions,
- mutations,
-};
diff --git a/frontend/src/store/pinia-modules/command.ts b/frontend/src/store/pinia-modules/command.ts
new file mode 100644
index 0000000000..141f438752
--- /dev/null
+++ b/frontend/src/store/pinia-modules/command.ts
@@ -0,0 +1,65 @@
+import { Command, CommandId, CommandRegisterId, CommandState } from "@/types";
+import { defineStore } from "pinia";
+import { onMounted, onUnmounted } from "vue";
+
+export const useCommandStore = defineStore("command", {
+ state: (): CommandState => ({
+ commandListById: new Map(),
+ }),
+ actions: {
+ registerCommand(command: Command) {
+ const list = this.commandListById.get(command.id);
+ if (list) {
+ const index = list.findIndex(
+ (item) => item.registerId === command.registerId
+ );
+ if (index > -1) {
+ throw new Error(
+ `'${command.registerId}' has already registered command '${command.id}', this is likely a programming error.`
+ );
+ }
+ list.push(command);
+ } else {
+ this.commandListById.set(command.id, [command]);
+ }
+ return command;
+ },
+
+ unregisterCommand({
+ id,
+ registerId,
+ }: {
+ id: CommandId;
+ registerId: CommandRegisterId;
+ }) {
+ const list = this.commandListById.get(id);
+ if (list) {
+ const index = list.findIndex((item) => item.registerId === registerId);
+ if (index >= 0) {
+ list.splice(index, 1);
+ return;
+ }
+ }
+ throw new Error(
+ `'${registerId}' attempts to unregister command '${id}' which is not registered before, this is likely a programming error.`
+ );
+ },
+
+ dispatchCommand(commandId: CommandId) {
+ const list = this.commandListById.get(commandId);
+ list?.forEach((cmd: Command) => {
+ cmd.run();
+ });
+ },
+ },
+});
+
+export const useRegisterCommand = (command: Command) => {
+ const store = useCommandStore();
+ onMounted(() => {
+ store.registerCommand(command);
+ });
+ onUnmounted(() => {
+ store.unregisterCommand(command);
+ });
+};
diff --git a/frontend/src/store/pinia-modules/index.ts b/frontend/src/store/pinia-modules/index.ts
index 1cec719dde..2f17c4301f 100644
--- a/frontend/src/store/pinia-modules/index.ts
+++ b/frontend/src/store/pinia-modules/index.ts
@@ -1,4 +1,5 @@
export * from "./actuator";
+export * from "./command";
export * from "./debug";
-export * from "./tab";
export * from "./label";
+export * from "./tab";
diff --git a/frontend/src/types/id.ts b/frontend/src/types/id.ts
index 85fdca5971..0cf41e7a41 100644
--- a/frontend/src/types/id.ts
+++ b/frontend/src/types/id.ts
@@ -65,8 +65,8 @@ export type BackupSettingId = IdType;
export type AnomalyId = IdType;
-export type CommandId = IdType;
-export type CommandRegisterId = IdType;
+export type CommandId = string;
+export type CommandRegisterId = string;
export type SheetId = IdType;
diff --git a/frontend/src/views/EnvironmentDashboard.vue b/frontend/src/views/EnvironmentDashboard.vue
index 3ea240b28e..8f62624311 100644
--- a/frontend/src/views/EnvironmentDashboard.vue
+++ b/frontend/src/views/EnvironmentDashboard.vue
@@ -76,7 +76,7 @@
</template>
<script lang="ts">
-import { onMounted, onUnmounted, computed, reactive, watch } from "vue";
+import { onMounted, computed, reactive, watch, defineComponent } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { array_swap } from "../utils";
@@ -91,6 +91,7 @@ import {
DefaultSchedulePolicy,
} from "../types";
import { BBTabItem } from "../bbkit/types";
+import { useRegisterCommand } from "@/store";
const DEFAULT_NEW_ENVIRONMENT: EnvironmentCreate = {
name: "New Env",
@@ -121,7 +122,7 @@ interface LocalState {
| "bb.feature.backup-policy";
}
-export default {
+export default defineComponent({
name: "EnvironmentDashboard",
components: {
EnvironmentDetail,
@@ -159,21 +160,6 @@ export default {
};
onMounted(() => {
- store.dispatch("command/registerCommand", {
- id: "bb.environment.create",
- registerId: "environment.dashboard",
- run: () => {
- createEnvironment();
- },
- });
- store.dispatch("command/registerCommand", {
- id: "bb.environment.reorder",
- registerId: "environment.dashboard",
- run: () => {
- startReorder();
- },
- });
-
selectEnvironmentOnHash();
if (!store.getters["uistate/introStateByKey"]("guide.environment")) {
@@ -187,15 +173,19 @@ export default {
}
});
- onUnmounted(() => {
- store.dispatch("command/unregisterCommand", {
- id: "bb.environment.create",
- registerId: "environment.dashboard",
- });
- store.dispatch("command/unregisterCommand", {
- id: "bb.environment.reorder",
- registerId: "environment.dashboard",
- });
+ useRegisterCommand({
+ id: "bb.environment.create",
+ registerId: "environment.dashboard",
+ run: () => {
+ createEnvironment();
+ },
+ });
+ useRegisterCommand({
+ id: "bb.environment.reorder",
+ registerId: "environment.dashboard",
+ run: () => {
+ startReorder();
+ },
});
watch(
@@ -356,5 +346,5 @@ export default {
tabClass,
};
},
-};
+});
</script>