From cd1df62848164aa02b837afdb85d8a4defd6999c Mon Sep 17 00:00:00 2001 From: YUUU23 Date: Thu, 8 Aug 2024 14:04:14 -0500 Subject: [PATCH 01/11] ref: window.jspsych initial --- src/App/components/JsPsychExperiment.jsx | 5 +++-- src/experiment/honeycomb.js | 8 +++++--- src/experiment/index.js | 4 ++-- src/experiment/procedures/honeycombProcedure.js | 12 ++++++------ src/experiment/procedures/startProcedure.js | 4 ++-- src/experiment/trials/camera.js | 12 ++++++------ src/experiment/trials/fixation.js | 7 +++++-- src/lib/utils.js | 4 ++++ 8 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/App/components/JsPsychExperiment.jsx b/src/App/components/JsPsychExperiment.jsx index c5dfccd2c..21ff6e059 100644 --- a/src/App/components/JsPsychExperiment.jsx +++ b/src/App/components/JsPsychExperiment.jsx @@ -51,7 +51,7 @@ export default function JsPsychExperiment({ tempJsPsych.data.addProperties({ app_name: import.meta.env.PACKAGE_NAME, app_version: import.meta.env.PACKAGE_VERSION, - app_commit: await window.electronAPI.getCommit(), + // app_commit: await window.electronAPI.getCommit(), study_id: studyID, participant_id: participantID, start_date: startDate, @@ -67,7 +67,8 @@ export default function JsPsychExperiment({ */ React.useEffect(() => { if (jsPsych) { - const timeline = buildTimeline(jsPsych, studyID, participantID); + window.jsPsych = jsPsych; + const timeline = buildTimeline(studyID, participantID); jsPsych.run(timeline); } }, [jsPsych]); diff --git a/src/experiment/honeycomb.js b/src/experiment/honeycomb.js index b38355e2c..b6e84868e 100644 --- a/src/experiment/honeycomb.js +++ b/src/experiment/honeycomb.js @@ -34,12 +34,14 @@ export const honeycombOptions = { * @param {Object} jsPsych The jsPsych instance being used to run the task * @returns {Object} A jsPsych timeline object */ -export function buildHoneycombTimeline(jsPsych) { +export function buildHoneycombTimeline() { + const jsPsych = window.jsPsych; + console.log(jsPsych); // Build the trials that make up the start procedure - const startProcedure = buildStartProcedure(jsPsych); + const startProcedure = buildStartProcedure(); // Build the trials that make up the task procedure - const honeycombProcedure = buildHoneycombProcedure(jsPsych); + const honeycombProcedure = buildHoneycombProcedure(); // Builds the trial needed to debrief the participant on their performance const debriefTrial = buildDebriefTrial(jsPsych); diff --git a/src/experiment/index.js b/src/experiment/index.js index 494f22953..50d8fc252 100644 --- a/src/experiment/index.js +++ b/src/experiment/index.js @@ -25,13 +25,13 @@ export const jsPsychOptions = honeycombOptions; * @param {string} participantID The ID of the participant that was just logged in * @returns The timeline for JsPsych to run */ -export function buildTimeline(jsPsych, studyID, participantID) { +export function buildTimeline(studyID, participantID) { console.log(`Building timeline for participant ${participantID} on study ${studyID}`); /** * ! Your timeline should be built in a newly created function, not this one * https://brown-ccv.github.io/honeycomb-docs/docs/quick_start#2-add-a-file-for-the-task */ - const timeline = buildHoneycombTimeline(jsPsych); + const timeline = buildHoneycombTimeline(); return timeline; } diff --git a/src/experiment/procedures/honeycombProcedure.js b/src/experiment/procedures/honeycombProcedure.js index a836073f8..1645f8877 100644 --- a/src/experiment/procedures/honeycombProcedure.js +++ b/src/experiment/procedures/honeycombProcedure.js @@ -15,10 +15,10 @@ import { buildFixationTrial } from "../trials/fixation"; * @param {Object} jsPsych The jsPsych instance being used to run the task * @returns {Object} A jsPsych (nested) timeline object */ -export function buildHoneycombProcedure(jsPsych) { +export function buildHoneycombProcedure() { const honeycombSettings = SETTINGS.honeycomb; - - const fixationTrial = buildFixationTrial(jsPsych); + console.log("hi"); + const fixationTrial = buildFixationTrial(); /** * Displays a colored circle and waits for participant to response with a keyboard press @@ -31,7 +31,7 @@ export function buildHoneycombProcedure(jsPsych) { const taskTrial = { type: imageKeyboardResponse, // Display the image passed as a timeline variable - stimulus: jsPsych.timelineVariable("stimulus"), + stimulus: window.jsPsych.timelineVariable("stimulus"), prompt: function () { // Conditionally displays the photodiodeGhostBox if (ENV.USE_PHOTODIODE) return photodiodeGhostBox; @@ -42,7 +42,7 @@ export function buildHoneycombProcedure(jsPsych) { data: { // Record the correct_response passed as a timeline variable code: eventCodes.honeycomb, - correct_response: jsPsych.timelineVariable("correct_response"), + correct_response: window.jsPsych.timelineVariable("correct_response"), }, on_load: function () { // Conditionally flashes the photodiode when the trial first loads @@ -50,7 +50,7 @@ export function buildHoneycombProcedure(jsPsych) { }, // Add a boolean value ("correct") to the data - if the user responded with the correct key or not on_finish: function (data) { - data.correct = jsPsych.pluginAPI.compareKeys(data.response, data.correct_response); + data.correct = window.jsPsych.pluginAPI.compareKeys(data.response, data.correct_response); }, }; diff --git a/src/experiment/procedures/startProcedure.js b/src/experiment/procedures/startProcedure.js index f292f38ff..c6decf267 100644 --- a/src/experiment/procedures/startProcedure.js +++ b/src/experiment/procedures/startProcedure.js @@ -18,7 +18,7 @@ import { introductionTrial } from "../trials/introduction"; * @param {Object} jsPsych The jsPsych instance being used to run the task * @returns {Object} A jsPsych (nested) timeline object */ -export function buildStartProcedure(jsPsych) { +export function buildStartProcedure() { const procedure = [nameTrial, enterFullscreenTrial, introductionTrial]; // Conditionally add the photodiode setup trials @@ -29,7 +29,7 @@ export function buildStartProcedure(jsPsych) { // Conditionally add the camera setup trials if (ENV.USE_CAMERA) { - procedure.push(buildCameraStartTrial(jsPsych)); + procedure.push(buildCameraStartTrial()); } // Return the block as a nested timeline diff --git a/src/experiment/trials/camera.js b/src/experiment/trials/camera.js index c1665377d..5d95aaca9 100644 --- a/src/experiment/trials/camera.js +++ b/src/experiment/trials/camera.js @@ -15,7 +15,7 @@ const WEBCAM_ID = "webcam"; // TODO @brown-ccv #301: Use jsPsych extension, deprecate this function // TODO @brown-ccv #343: We should be able to make this work on both electron and browser? // TODO @brown-ccv #301: Rolling save to the deployment (webm is a subset of mkv) -export function buildCameraStartTrial(jsPsych) { +export function buildCameraStartTrial() { return { timeline: [ { @@ -48,7 +48,7 @@ export function buildCameraStartTrial(jsPsych) { throw new Error("video recording is only available when running inside Electron"); } - const cameraRecorder = jsPsych.pluginAPI.getCameraRecorder(); + const cameraRecorder = window.jsPsych.pluginAPI.getCameraRecorder(); if (!cameraRecorder) { console.error("Camera is not initialized, no data will be recorded."); return; @@ -76,11 +76,11 @@ export function buildCameraStartTrial(jsPsych) { // Assign camera feed to the