mirror of
https://github.com/johndoe6345789/low-code-react-app-b.git
synced 2026-04-24 13:44:54 +00:00
Centralize store action name lookups
This commit is contained in:
48
src/store/actionNames.ts
Normal file
48
src/store/actionNames.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
const itemSlices = [
|
||||
'files',
|
||||
'models',
|
||||
'components',
|
||||
'componentTrees',
|
||||
'workflows',
|
||||
'lambdas',
|
||||
] as const
|
||||
|
||||
const itemChangeActionNames = ['addItem', 'updateItem', 'removeItem'] as const
|
||||
|
||||
export const itemChangeActionTypes = new Set(
|
||||
itemSlices.flatMap((slice) =>
|
||||
itemChangeActionNames.map((actionName) => `${slice}/${actionName}`)
|
||||
)
|
||||
)
|
||||
|
||||
export const persistenceSingleItemActionNames = new Set([
|
||||
'addItem',
|
||||
'updateItem',
|
||||
'saveFile',
|
||||
'saveModel',
|
||||
'saveComponent',
|
||||
'saveComponentTree',
|
||||
'saveWorkflow',
|
||||
'saveLambda',
|
||||
])
|
||||
|
||||
export const persistenceBulkActionNames = new Set([
|
||||
'addItems',
|
||||
'setItems',
|
||||
'setFiles',
|
||||
'setModels',
|
||||
'setComponents',
|
||||
'setComponentTrees',
|
||||
'setWorkflows',
|
||||
'setLambdas',
|
||||
])
|
||||
|
||||
export const persistenceDeleteActionNames = new Set([
|
||||
'removeItem',
|
||||
'deleteFile',
|
||||
'deleteModel',
|
||||
'deleteComponent',
|
||||
'deleteComponentTree',
|
||||
'deleteWorkflow',
|
||||
'deleteLambda',
|
||||
])
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Middleware } from '@reduxjs/toolkit'
|
||||
import { syncToFlaskBulk, checkFlaskConnection } from '../slices/syncSlice'
|
||||
import { RootState } from '../index'
|
||||
import { itemChangeActionTypes } from '../actionNames'
|
||||
|
||||
interface AutoSyncConfig {
|
||||
enabled: boolean
|
||||
@@ -127,28 +128,7 @@ export const createAutoSyncMiddleware = (): Middleware => {
|
||||
})
|
||||
}
|
||||
|
||||
const changeActions = [
|
||||
'files/addItem',
|
||||
'files/updateItem',
|
||||
'files/removeItem',
|
||||
'models/addItem',
|
||||
'models/updateItem',
|
||||
'models/removeItem',
|
||||
'components/addItem',
|
||||
'components/updateItem',
|
||||
'components/removeItem',
|
||||
'componentTrees/addItem',
|
||||
'componentTrees/updateItem',
|
||||
'componentTrees/removeItem',
|
||||
'workflows/addItem',
|
||||
'workflows/updateItem',
|
||||
'workflows/removeItem',
|
||||
'lambdas/addItem',
|
||||
'lambdas/updateItem',
|
||||
'lambdas/removeItem',
|
||||
]
|
||||
|
||||
if (changeActions.includes(action.type)) {
|
||||
if (itemChangeActionTypes.has(action.type)) {
|
||||
autoSyncManager.trackChange()
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,11 @@ import { Middleware } from '@reduxjs/toolkit'
|
||||
import { db } from '@/lib/db'
|
||||
import { syncToFlask } from './flaskSync'
|
||||
import { RootState } from '../index'
|
||||
import {
|
||||
persistenceBulkActionNames,
|
||||
persistenceDeleteActionNames,
|
||||
persistenceSingleItemActionNames,
|
||||
} from '../actionNames'
|
||||
|
||||
interface PersistenceConfig {
|
||||
storeName: string
|
||||
@@ -128,10 +133,7 @@ export const createPersistenceMiddleware = (): Middleware => {
|
||||
if (!sliceState) return result
|
||||
|
||||
try {
|
||||
if (actionName === 'addItem' || actionName === 'updateItem' || actionName === 'saveFile' ||
|
||||
actionName === 'saveModel' || actionName === 'saveComponent' || actionName === 'saveComponentTree' ||
|
||||
actionName === 'saveWorkflow' || actionName === 'saveLambda') {
|
||||
|
||||
if (persistenceSingleItemActionNames.has(actionName)) {
|
||||
const item = action.payload
|
||||
if (item && item.id) {
|
||||
persistenceQueue.enqueue({
|
||||
@@ -144,10 +146,7 @@ export const createPersistenceMiddleware = (): Middleware => {
|
||||
}
|
||||
}
|
||||
|
||||
if (actionName === 'addItems' || actionName === 'setItems' || actionName === 'setFiles' ||
|
||||
actionName === 'setModels' || actionName === 'setComponents' || actionName === 'setComponentTrees' ||
|
||||
actionName === 'setWorkflows' || actionName === 'setLambdas') {
|
||||
|
||||
if (persistenceBulkActionNames.has(actionName)) {
|
||||
const items = action.payload
|
||||
if (Array.isArray(items)) {
|
||||
items.forEach((item: any) => {
|
||||
@@ -164,10 +163,7 @@ export const createPersistenceMiddleware = (): Middleware => {
|
||||
}
|
||||
}
|
||||
|
||||
if (actionName === 'removeItem' || actionName === 'deleteFile' || actionName === 'deleteModel' ||
|
||||
actionName === 'deleteComponent' || actionName === 'deleteComponentTree' ||
|
||||
actionName === 'deleteWorkflow' || actionName === 'deleteLambda') {
|
||||
|
||||
if (persistenceDeleteActionNames.has(actionName)) {
|
||||
const itemId = typeof action.payload === 'string' ? action.payload : action.payload?.id
|
||||
if (itemId) {
|
||||
persistenceQueue.enqueue({
|
||||
|
||||
Reference in New Issue
Block a user