Skip to content

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

Add command store

Tianzhou Chenaa4faa93d7c4
diff --git a/frontend/src/store/index.ts b/frontend/src/store/index.ts
index 1bb344151a..aa0d80c2c8 100644
--- a/frontend/src/store/index.ts
+++ b/frontend/src/store/index.ts
@@ -17,6 +17,7 @@ import uistate from "./modules/uistate";
// Following states are only stored in memory
import router from "./modules/router";
+import command from "./modules/command";
import notification from "./modules/notification";
const debug = process.env.NODE_ENV !== "production";
@@ -35,6 +36,7 @@ export const store: Store<any> = createStore({
pipeline,
uistate,
router,
+ command,
notification,
},
strict: debug,
diff --git a/frontend/src/store/modules/command.ts b/frontend/src/store/modules/command.ts
new file mode 100644
index 0000000000..29acd30831
--- /dev/null
+++ b/frontend/src/store/modules/command.ts
@@ -0,0 +1,86 @@
+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/types/bytebase.ts b/frontend/src/types/bytebase.ts
index 79cae64b66..a605e9f4bf 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -20,6 +20,9 @@ export type DataSourceId = {
instanceId: InstanceId;
};
+export type CommandId = string;
+export type CommandRegisterId = string;
+
// Persistent State Models
export type User = ResourceObject & {};
export type NewUser = Omit<User, "id">;
@@ -137,6 +140,12 @@ export type Notification = {
description?: string;
};
+export type Command = {
+ id: CommandId;
+ registerId: CommandRegisterId;
+ run: () => void;
+};
+
// "id" and "createdTs" is auto generated upon the notification store
// receives.
export type NewNotification = Omit<Notification, "id" | "createdTs">;
@@ -193,3 +202,7 @@ export interface RepositoryState {
export interface NotificationState {
notificationByModule: Map<string, Notification[]>;
}
+
+export interface CommandState {
+ commandListById: Map<CommandId, Command[]>;
+}