Don't use parse to improve performance

This commit is contained in:
Icedsoda committed 2025-06-16 17:59:21 +02:00
1 parent 232e74c659
commit 22d131e9c4
4 files changed
+60 -95

No files matched your search

@@ -5,7 +5,6 @@ import {
EventHandler,
eventSchema,
Event,
handleEventValidationError,
NamespaceId,
createNamespaceHookImplementation,
} from "../../../client/socketioStore";
@@ -113,68 +112,68 @@ export function extruder2MessageHandler(
} else if (eventName == "RotationStateEvent") {
store.setState((state) => ({
...state,
rotationState: inverterRotationEventSchema.parse(event),
rotationState: event as InverterRotationEvent,
}));
} else if (eventName == "ModeStateEvent") {
store.setState((state) => ({
...state,
modeState: modeStateEventSchema.parse(event),
modeState: event as ModeStateEvent,
}));
} else if (eventName == "FrontHeatingStateEvent") {
const parsed = heatingStateEventSchema.parse(event);
const heatingEvent = event as HeatingStateEvent;
const timeseriesValue: TimeSeriesValue = {
value: parsed.data.temperature,
value: heatingEvent.data.temperature,
timestamp: event.ts,
};
store.setState((state) => ({
...state,
heatingFrontState: parsed,
heatingFrontState: heatingEvent,
frontTemperature: addFrontTemperature(
state.frontTemperature,
timeseriesValue,
),
}));
} else if (eventName == "NozzleHeatingStateEvent") {
const parsed = heatingStateEventSchema.parse(event);
const heatingEvent = event as HeatingStateEvent;
const timeseriesValue: TimeSeriesValue = {
value: parsed.data.temperature,
value: heatingEvent.data.temperature,
timestamp: event.ts,
};
store.setState((state) => ({
...state,
heatingNozzleState: parsed,
heatingNozzleState: heatingEvent,
nozzleTemperature: addNozzleTemperature(
state.nozzleTemperature,
timeseriesValue,
),
}));
} else if (eventName == "BackHeatingStateEvent") {
const parsed = heatingStateEventSchema.parse(event);
const heatingEvent = event as HeatingStateEvent;
const timeseriesValue: TimeSeriesValue = {
value: parsed.data.temperature,
value: heatingEvent.data.temperature,
timestamp: event.ts,
};
store.setState((state) => ({
...state,
heatingBackState: parsed,
heatingBackState: heatingEvent,
backTemperature: addBackTemperature(
state.backTemperature,
timeseriesValue,
),
}));
} else if (eventName == "MiddleHeatingStateEvent") {
const parsed = heatingStateEventSchema.parse(event);
const heatingEvent = event as HeatingStateEvent;
const timeseriesValue: TimeSeriesValue = {
value: parsed.data.temperature,
value: heatingEvent.data.temperature,
timestamp: event.ts,
};
store.setState((state) => ({
...state,
heatingMiddleState: parsed,
heatingMiddleState: heatingEvent,
middleTemperature: addMiddleTemperature(
state.middleTemperature,
timeseriesValue,
@@ -227,40 +226,36 @@ export function extruder2MessageHandler(
} else if (eventName == "RegulationStateEvent") {
store.setState((state) => ({
...state,
motorRegulationState: motorRegulationEventSchema.parse(event),
motorRegulationState: event as MotorRegulationStateEvent,
}));
} else if (eventName == "PressureStateEvent") {
const parsed = motorPressureStateEventSchema.parse(event);
const pressureEvent = event as MotorPressureStateEvent;
const timeseriesValue: TimeSeriesValue = {
value: parsed.data.bar,
value: pressureEvent.data.bar,
timestamp: event.ts,
};
store.setState((state) => ({
...state,
motorBarState: parsed,
motorBarState: pressureEvent,
bar: addBar(state.bar, timeseriesValue),
}));
} else if (eventName == "ScrewStateEvent") {
const parsed = motorScrewStateEventSchema.parse(event);
const screwEvent = event as MotorScrewStateEvent;
const timeseriesValue: TimeSeriesValue = {
value: parsed.data.rpm,
value: screwEvent.data.rpm,
timestamp: event.ts,
};
store.setState((state) => ({
...state,
motorRpmState: parsed,
motorRpmState: screwEvent,
rpm: addRpm(state.rpm, timeseriesValue),
}));
}
} catch (error) {
if (error instanceof z.ZodError) {
handleEventValidationError(error, eventName);
} else {
console.error(`Unexpected error processing ${eventName} event:`, error);
throw error;
}
console.error(`Unexpected error processing ${eventName} event:`, error);
throw error;
}
};
}
@@ -10,7 +10,6 @@ import {
EventHandler,
eventSchema,
Event,
handleEventValidationError,
handleUnhandledEventError,
NamespaceId,
createNamespaceHookImplementation,
@@ -90,18 +89,14 @@ export function laser1MessageHandler(
if (eventName === "LaserStateEvent") {
store.setState((state) => ({
...state,
dreState: {
name: event.name,
data: laserStateEventDataSchema.parse(event.data),
ts: event.ts,
},
laserState: event as LaserStateEvent,
}));
}
// Metric events (keep for 1 hour)
else if (eventName === "DiameterEvent") {
const parsed = diameterEventSchema.parse(event);
const diameterEvent = event as DiameterEvent;
const timeseriesValue: TimeSeriesValue = {
value: parsed.data.diameter,
value: diameterEvent.data.diameter,
timestamp: event.ts,
};
store.setState((state) => ({
@@ -112,12 +107,8 @@ export function laser1MessageHandler(
handleUnhandledEventError(eventName);
}
} catch (error) {
if (error instanceof z.ZodError) {
handleEventValidationError(error, eventName);
} else {
console.error(`Unexpected error processing ${eventName} event:`, error);
throw error;
}
console.error(`Unexpected error processing ${eventName} event:`, error);
throw error;
}
};
}
@@ -10,7 +10,6 @@ import {
EventHandler,
eventSchema,
Event,
handleEventValidationError,
handleUnhandledEventError,
NamespaceId,
createNamespaceHookImplementation,
@@ -108,20 +107,18 @@ export function mock1MessageHandler(
try {
// Apply appropriate caching strategy based on event type
if (eventName === "MockStateEvent") {
const parsed = mockStateEventSchema.parse(event);
console.log("MockStateEvent", parsed);
console.log("MockStateEvent", event);
store.setState((state) => ({
...state,
mockState: parsed,
mockState: event as MockStateEvent,
}));
}
// Mode state events (latest only)
else if (eventName === "ModeStateEvent") {
const parsed = modeStateEventSchema.parse(event);
console.log("ModeStateEvent", parsed);
console.log("ModeStateEvent", event);
store.setState((state) => ({
...state,
modeState: parsed,
modeState: event as ModeStateEvent,
}));
}
// Metric events (keep for 1 hour)
@@ -138,12 +135,8 @@ export function mock1MessageHandler(
handleUnhandledEventError(eventName);
}
} catch (error) {
if (error instanceof z.ZodError) {
handleEventValidationError(error, eventName);
} else {
console.error(`Unexpected error processing ${eventName} event:`, error);
throw error;
}
console.error(`Unexpected error processing ${eventName} event:`, error);
throw error;
}
};
}
@@ -10,7 +10,6 @@ import {
EventHandler,
eventSchema,
Event,
handleEventValidationError,
handleUnhandledEventError,
NamespaceId,
createNamespaceHookImplementation,
@@ -161,25 +160,21 @@ export const tensionArmStateEventSchema = eventSchema(
// ========== Type Inferences ==========
export type TraversePositionEvent = z.infer<
typeof traversePositionEventDataSchema
>;
export type TraversePositionEvent = z.infer<typeof traversePositionEventSchema>;
export type TraverseStateEvent = z.infer<typeof traverseStateEventSchema>;
export type PullerStateEvent = z.infer<typeof pullerStateEventSchema>;
export type PullerSpeedEvent = z.infer<typeof pullerSpeedEventSchema>;
export type AutostopWoundedLengthEvent = z.infer<
typeof autostopWoundedLengthEventDataSchema
typeof autostopWoundedLengthEventSchema
>;
export type AutostopTransition = z.infer<typeof autostopTransitionSchema>;
export type AutostopStateEvent = z.infer<typeof autostopStateEventSchema>;
export type Mode = z.infer<typeof modeSchema>;
export type ModeStateEvent = z.infer<typeof modeStateEventSchema>;
export type SpoolStateEvent = z.infer<typeof spoolStateEventSchema>;
export type MeasurementsWindingRpmEvent = z.infer<
typeof spoolRpmEventDataSchema
>;
export type MeasurementsWindingRpmEvent = z.infer<typeof spoolRpmEventSchema>;
export type MeasurementsTensionArmEvent = z.infer<
typeof tensionArmAngleEventDataSchema
typeof tensionArmAngleEventSchema
>;
export type TensionArmStateEvent = z.infer<typeof tensionArmStateEventSchema>;
@@ -263,46 +258,41 @@ export function winder2MessageHandler(
// Apply appropriate caching strategy based on event type
// State events (keep only the latest)
if (eventName === "TraverseStateEvent") {
const parsed = traverseStateEventSchema.parse(event);
console.log("TraverseStateEvent", parsed);
console.log("TraverseStateEvent", event);
store.setState((state) => ({
...state,
traverseState: parsed,
traverseState: event as TraverseStateEvent,
}));
} else if (eventName === "PullerStateEvent") {
const parsed = pullerStateEventSchema.parse(event);
console.log("PullerStateEvent", parsed);
console.log("PullerStateEvent", event);
store.setState((state) => ({
...state,
pullerState: parsed,
pullerState: event as PullerStateEvent,
}));
} else if (eventName === "AutostopStateEvent") {
const parsed = autostopStateEventSchema.parse(event);
console.log("AutostopStateEvent", parsed);
console.log("AutostopStateEvent", event);
store.setState((state) => ({
...state,
autostopState: parsed,
autostopState: event as AutostopStateEvent,
}));
} else if (eventName === "ModeStateEvent") {
const parsed = modeStateEventSchema.parse(event);
console.log("ModeStateEvent", parsed);
console.log("ModeStateEvent", event);
store.setState((state) => ({
...state,
modeState: parsed,
modeState: event as ModeStateEvent,
}));
} else if (eventName === "TensionArmStateEvent") {
const parsed = tensionArmStateEventSchema.parse(event);
console.log("TensionArmStateEvent", parsed);
console.log("TensionArmStateEvent", event);
store.setState((state) => ({
...state,
tensionArmState: parsed,
tensionArmState: event as TensionArmStateEvent,
}));
}
// Metric events (keep for 1 hour)
else if (eventName === "TraversePositionEvent") {
const parsed = traversePositionEventSchema.parse(event);
const positionEvent = event as TraversePositionEvent;
const timeseriesValue: TimeSeriesValue = {
value: parsed.data.position ?? 0,
value: positionEvent.data.position ?? 0,
timestamp: event.ts,
};
store.setState((state) => ({
@@ -313,9 +303,9 @@ export function winder2MessageHandler(
),
}));
} else if (eventName === "PullerSpeedEvent") {
const parsed = pullerSpeedEventSchema.parse(event);
const speedEvent = event as PullerSpeedEvent;
const timeseriesValue: TimeSeriesValue = {
value: parsed.data.speed,
value: speedEvent.data.speed,
timestamp: event.ts,
};
store.setState((state) => ({
@@ -323,9 +313,9 @@ export function winder2MessageHandler(
pullerSpeed: addPullerSpeed(state.pullerSpeed, timeseriesValue),
}));
} else if (eventName === "AutostopWoundedLengthEvent") {
const parsed = autostopWoundedLengthEventSchema.parse(event);
const woundedEvent = event as AutostopWoundedLengthEvent;
const timeseriesValue: TimeSeriesValue = {
value: parsed.data.wounded_length,
value: woundedEvent.data.wounded_length,
timestamp: event.ts,
};
store.setState((state) => ({
@@ -336,9 +326,9 @@ export function winder2MessageHandler(
),
}));
} else if (eventName === "SpoolRpmEvent") {
const parsed = spoolRpmEventSchema.parse(event);
const rpmEvent = event as MeasurementsWindingRpmEvent;
const timeseriesValue: TimeSeriesValue = {
value: parsed.data.rpm,
value: rpmEvent.data.rpm,
timestamp: event.ts,
};
store.setState((state) => ({
@@ -346,9 +336,9 @@ export function winder2MessageHandler(
spoolRpm: addSpoolRpm(state.spoolRpm, timeseriesValue),
}));
} else if (eventName === "TensionArmAngleEvent") {
const parsed = tensionArmAngleEventSchema.parse(event);
const angleEvent = event as MeasurementsTensionArmEvent;
const timeseriesValue: TimeSeriesValue = {
value: parsed.data.degree,
value: angleEvent.data.degree,
timestamp: event.ts,
};
store.setState((state) => ({
@@ -362,12 +352,8 @@ export function winder2MessageHandler(
handleUnhandledEventError(eventName);
}
} catch (error) {
if (error instanceof z.ZodError) {
handleEventValidationError(error, eventName);
} else {
console.error(`Unexpected error processing ${eventName} event:`, error);
throw error;
}
console.error(`Unexpected error processing ${eventName} event:`, error);
throw error;
}
};
}