Refactor simulation store structures to use arrays instead of records for conveyors, machines, storage units, vehicles, and products; remove useSimulationStore; enhance state management and helper functions for better performance and readability.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { create } from 'zustand';
|
||||
import { immer } from 'zustand/middleware/immer';
|
||||
|
||||
interface StorageUnitStatus extends StorageSchemaEvent {
|
||||
interface StorageUnitStatus extends StorageEventSchema {
|
||||
productId: string;
|
||||
isActive: boolean;
|
||||
idleTime: number;
|
||||
@@ -10,10 +10,10 @@ interface StorageUnitStatus extends StorageSchemaEvent {
|
||||
}
|
||||
|
||||
interface StorageUnitStore {
|
||||
storageUnits: Record<string, StorageUnitStatus>;
|
||||
storageUnits: StorageUnitStatus[];
|
||||
|
||||
// Actions
|
||||
addStorageUnit: (productId: string, storageUnit: StorageSchemaEvent) => void;
|
||||
addStorageUnit: (productId: string, storageUnit: StorageEventSchema) => void;
|
||||
removeStorageUnit: (modelUuid: string) => void;
|
||||
updateStorageUnit: (
|
||||
modelUuid: string,
|
||||
@@ -42,34 +42,34 @@ interface StorageUnitStore {
|
||||
|
||||
export const useStorageUnitStore = create<StorageUnitStore>()(
|
||||
immer((set, get) => ({
|
||||
storageUnits: {},
|
||||
storageUnits: [],
|
||||
|
||||
// Actions
|
||||
addStorageUnit: (productId, storageUnit) => {
|
||||
set((state) => {
|
||||
state.storageUnits[storageUnit.modelUuid] = {
|
||||
state.storageUnits.push({
|
||||
...storageUnit,
|
||||
productId,
|
||||
isActive: false,
|
||||
idleTime: 0,
|
||||
activeTime: 0,
|
||||
currentLoad: 0, // Initialize currentLoad to 0
|
||||
currentLoad: 0,
|
||||
state: 'idle',
|
||||
};
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
removeStorageUnit: (modelUuid) => {
|
||||
set((state) => {
|
||||
delete state.storageUnits[modelUuid];
|
||||
state.storageUnits = state.storageUnits.filter(s => s.modelUuid !== modelUuid);
|
||||
});
|
||||
},
|
||||
|
||||
updateStorageUnit: (modelUuid, updates) => {
|
||||
set((state) => {
|
||||
const storageUnit = state.storageUnits[modelUuid];
|
||||
if (storageUnit) {
|
||||
Object.assign(storageUnit, updates);
|
||||
const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
|
||||
if (unit) {
|
||||
Object.assign(unit, updates);
|
||||
}
|
||||
});
|
||||
},
|
||||
@@ -77,18 +77,18 @@ export const useStorageUnitStore = create<StorageUnitStore>()(
|
||||
// Status updates
|
||||
setStorageUnitActive: (modelUuid, isActive) => {
|
||||
set((state) => {
|
||||
const storageUnit = state.storageUnits[modelUuid];
|
||||
if (storageUnit) {
|
||||
storageUnit.isActive = isActive;
|
||||
const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
|
||||
if (unit) {
|
||||
unit.isActive = isActive;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
setStorageUnitState: (modelUuid, newState) => {
|
||||
set((state) => {
|
||||
const storageUnit = state.storageUnits[modelUuid];
|
||||
if (storageUnit) {
|
||||
storageUnit.state = newState;
|
||||
const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
|
||||
if (unit) {
|
||||
unit.state = newState;
|
||||
}
|
||||
});
|
||||
},
|
||||
@@ -96,9 +96,9 @@ export const useStorageUnitStore = create<StorageUnitStore>()(
|
||||
// Load updates
|
||||
updateStorageUnitLoad: (modelUuid, load) => {
|
||||
set((state) => {
|
||||
const storageUnit = state.storageUnits[modelUuid];
|
||||
if (storageUnit) {
|
||||
storageUnit.currentLoad = load;
|
||||
const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
|
||||
if (unit) {
|
||||
unit.currentLoad = load;
|
||||
}
|
||||
});
|
||||
},
|
||||
@@ -106,53 +106,47 @@ export const useStorageUnitStore = create<StorageUnitStore>()(
|
||||
// Time tracking
|
||||
incrementActiveTime: (modelUuid, incrementBy) => {
|
||||
set((state) => {
|
||||
const storageUnit = state.storageUnits[modelUuid];
|
||||
if (storageUnit) {
|
||||
storageUnit.activeTime += incrementBy;
|
||||
const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
|
||||
if (unit) {
|
||||
unit.activeTime += incrementBy;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
incrementIdleTime: (modelUuid, incrementBy) => {
|
||||
set((state) => {
|
||||
const storageUnit = state.storageUnits[modelUuid];
|
||||
if (storageUnit) {
|
||||
storageUnit.idleTime += incrementBy;
|
||||
const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
|
||||
if (unit) {
|
||||
unit.idleTime += incrementBy;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Helpers
|
||||
getStorageUnitById: (modelUuid) => {
|
||||
return get().storageUnits[modelUuid];
|
||||
return get().storageUnits.find(s => s.modelUuid === modelUuid);
|
||||
},
|
||||
|
||||
getStorageUnitsByProduct: (productId) => {
|
||||
return Object.values(get().storageUnits).filter(
|
||||
s => s.productId === productId
|
||||
);
|
||||
return get().storageUnits.filter(s => s.productId === productId);
|
||||
},
|
||||
|
||||
getActiveStorageUnits: () => {
|
||||
return Object.values(get().storageUnits).filter(s => s.isActive);
|
||||
return get().storageUnits.filter(s => s.isActive);
|
||||
},
|
||||
|
||||
getIdleStorageUnits: () => {
|
||||
return Object.values(get().storageUnits).filter(
|
||||
s => !s.isActive && s.state === 'idle'
|
||||
);
|
||||
return get().storageUnits.filter(s => !s.isActive && s.state === 'idle');
|
||||
},
|
||||
|
||||
getFullStorageUnits: () => {
|
||||
return Object.values(get().storageUnits).filter(
|
||||
return get().storageUnits.filter(
|
||||
s => s.currentLoad >= s.point.action.storageCapacity
|
||||
);
|
||||
},
|
||||
|
||||
getEmptyStorageUnits: () => {
|
||||
return Object.values(get().storageUnits).filter(
|
||||
s => s.currentLoad === 0
|
||||
);
|
||||
return get().storageUnits.filter(s => s.currentLoad === 0);
|
||||
},
|
||||
}))
|
||||
);
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user