I converted one of my existing application to use the "worker" to fetch data from database tables, massage the data to the required format, and to finally send the data to the main-thread for displaying on the UI (User Interface).
The required data format for the UI is an array of JSON objects. [JSON{}, JSON{}, ....]. I am able to send the data from the worker thread to the main thread using the post-message which is using the "structured cloning" copying method. I found that using the "worker" to fetch the data, takes slightly more performance-time than implementing the data-fetching directly in the main-thread itself. I am hoping that by using the "Transferable Objects", I might be able to bring the performance time equal or better than implementing the data fetching in the main-thread.
I tried to use the "Transferable Objects" for transfering the data from the worker to the main thread. But I am not successful since only ArrayBuffer is transferable. The data I fetch from the database, and processing in the worker for sending to the main-thread, are mainly text or string data. Each database row that is fetched is an appointment which is converted (after massaging) into a JSON object as displayed in the code below. I have nearly a thousand appointments to process. When I send the data as single JSON{} to the main thread, it takes considerably more time. Sending the [JSON{}] as an array to the main-thread is much faster. Hence I am looking for ways to send the array of [JSON{}] from the worker to the main-thread and then to retrieve the array in the main-thread for passing to the UI in-turn.
Could anyone please throw some light on how to use the Transferable object in this case?
let gl_calendarEventArray = [];
//In a loop the array is filled with JSON objects
gl_calendarEventArray.push({
id: appointment.appointment_id,
clientIssueFlag: appointment.clientIssueFlag,
apptIssueFlag: appointment.apptIssueFlag,
issueTypeFlag: appointment.issueTypeFlag,
recur_appt_id: appointment.recurring_appointment_id,
appt_type_id: appointment.appointment_type_id,
branch_id: appointment.branch_id,
resourceId: appointment.tutor_id,
student_branch_id:appointment.student_branch_id,
student_id: appointment.student_id,
title: title,
tutor_id: appointment.tutor_id,
tutor: appointment.tutor_name,
service: appointment.service_name,
student: appointment.student_name,
start: appointment.start_date_time,
demo: appointment.demo,
end: appointment.end_date_time,
status_id: appointment.appointment_status_id,
duration: duration,
area_of_focus: area_of_focus,
student_notes: student_notes,
appointment_notes: appointment_notes,
primary_parent_name: primary_parent_name,
primary_parent_email: primary_parent_email,
primary_parent_phone: primary_parent_phone,
secondary_parent_name: secondary_parent_name,
secondary_parent_email: secondary_parent_email,
secondary_parent_phone: secondary_parent_phone,
current_grade_level: current_grade_level,
school_name: school_name,
color: color,
textColor: textColor,
additional_info:additional_info
});
//After the loop is completed, the data is sent to the main-thread
//by a postMessage which is using the structured copy now.
self.postMessage({msgType: 'CalendarEvent', msg: gl_calendarEventArray});