Here's a declarative version using modern JavaScript features:
/**
* Creates a WebSocket route handler that intercepts messages for a specific connector
* and modifies the field_PersistedSize for the last 20 values in the statistics payload.
*
* @param {string} connectorId - The connector ID to match in the WebSocket messages.
* @param {number} persistedSize - The desired value to set for field_PersistedSize.
* @returns {Function} A route handler function for Playwright's WebSocket route.
*/
const createConnectorPersistedSizeHandler = (connectorId, persistedSize) => (route) => {
const server = route.connectToServer();
server.onMessage((message) => {
const messageObject = JSON.parse(message);
const getStatsMsg = messageObject?.payload?.data?.GetStats;
if (getStatsMsg) {
const values = getStatsMsg.Values;
const startIndex = Math.max(0, values.length - 20);
getStatsMsg.Values = [
...values.slice(0, startIndex),
...values.slice(startIndex).map(value => [value[0], persistedSize, ...value.slice(2)])
];
message = JSON.stringify(messageObject);
console.log(`Modified last ${values.length - startIndex} values with persistedSize ${persistedSize} for connector ID: ${connectorId}`);
} else {
console.log(`Unmodified message for connector ID: ${connectorId}`);
}
return route.send(message);
});
};
Key improvements:
- Replaced the
for loop with functional programming concepts
- Used array spread operator (
...)
- Used
slice() and map() for immutable array operations
- Preserved all other array elements using spread operator
- Maintained the same functionality in a more declarative way
Alternative version with even more modern features:
/**
* Creates a WebSocket route handler that intercepts messages for a specific connector
* and modifies the field_PersistedSize for the last 20 values in the statistics payload.
*
* @param {string} connectorId - The connector ID to match in the WebSocket messages.
* @param {number} persistedSize - The desired value to set for field_PersistedSize.
* @returns {Function} A route handler function for Playwright's WebSocket route.
*/
const createConnectorPersistedSizeHandler = (connectorId, persistedSize) => (route) => {
const server = route.connectToServer();
const updatePersistedSize = values => {
const startIndex = Math.max(0, values.length - 20);
return [
...values.slice(0, startIndex),
...values.slice(startIndex).map(([time, _, ...rest]) => [time, persistedSize, ...rest])
];
};
server.onMessage((message) => {
const messageObject = JSON.parse(message);
const values = messageObject?.payload?.data?.GetStats?.Values;
const modifiedMessage = values
? {
...messageObject,
payload: {
...messageObject.payload,
data: {
...messageObject.payload.data,
GetStats: {
...messageObject.payload.data.GetStats,
Values: updatePersistedSize(values)
}
}
}
}
: messageObject;
const modifiedCount = values?.length ? Math.min(20, values.length) : 0;
console.log(
modifiedCount
? `Modified last ${modifiedCount} values with persistedSize ${persistedSize} for connector ID: ${connectorId}`
: `Unmodified message for connector ID: ${connectorId}`
);
return route.send(JSON.stringify(modifiedMessage));
});
};