Worker message with data to serialize.
Set tracking active task IDs.
Void; posts JSON_SERIALIZE_RESULT or ERROR.
export function handleJsonSerialize(
message: JSONSerializeMessage,
activeTasks: Set<string>,
): void {
const { taskId, data, replacerKeys, space } = message.payload;
activeTasks.add(taskId);
try {
console.info(`[Worker] 📝 Starting JSON serialization for task ${taskId}`);
const startTime = performance.now();
const replacer = replacerKeys
? (key: string, value: unknown) => {
if (key === "" || replacerKeys.includes(key)) {
return value;
}
return undefined;
}
: undefined;
const json = JSON.stringify(data, replacer, space);
const serializationTimeMs = performance.now() - startTime;
const sizeBytes = new Blob([json]).size;
globalThis.postMessage({
type: "JSON_SERIALIZE_RESULT",
payload: {
taskId,
json,
sizeBytes,
timing: {
serializationTimeMs,
},
},
});
console.info(
`[Worker] ✅ JSON serialization task ${taskId} completed (${serializationTimeMs.toFixed(2)}ms, ${sizeBytes} bytes)`,
);
} catch (error) {
console.error(
`[Worker] ❌ Error in JSON serialization task ${taskId}:`,
error,
);
globalThis.postMessage({
type: "ERROR",
payload: {
taskId,
error: getErrorDetails(error),
},
});
} finally {
activeTasks.delete(taskId);
}
}
Serializes data to JSON in a worker for heavy payloads.