From 25b93e9f695d43e2def16c1195fff842e4a88c6d Mon Sep 17 00:00:00 2001 From: YUUU23 Date: Wed, 14 Aug 2024 16:21:59 -0500 Subject: [PATCH] ref: change window.jsPsych to use util functions --- src/App/components/JsPsychExperiment.jsx | 3 ++- src/experiment/procedures/endProcedure.js | 1 - src/experiment/procedures/honeycombProcedure.js | 8 ++++---- src/experiment/procedures/startProcedure.js | 1 - src/experiment/trials/camera.js | 9 +++++---- src/experiment/trials/fixation.js | 3 ++- src/experiment/trials/honeycombTrials.js | 3 ++- 7 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/App/components/JsPsychExperiment.jsx b/src/App/components/JsPsychExperiment.jsx index 56ce1e68..353e43bc 100644 --- a/src/App/components/JsPsychExperiment.jsx +++ b/src/App/components/JsPsychExperiment.jsx @@ -5,6 +5,7 @@ import React from "react"; import { ENV } from "../../config/"; import { buildTimeline, jsPsychOptions } from "../../experiment"; import { initParticipant } from "../deployments/firebase"; +import { getJsPsych } from "../../lib/utils"; // ID used to identify the DOM element that holds the experiment. const EXPERIMENT_ID = "experiment-window"; @@ -70,7 +71,7 @@ export default function JsPsychExperiment({ // set up jsPsych object as global variable window.jsPsych = jsPsych; const timeline = buildTimeline(studyID, participantID); - window.jsPsych.run(timeline); + getJsPsych().run(timeline); } }, [jsPsych]); diff --git a/src/experiment/procedures/endProcedure.js b/src/experiment/procedures/endProcedure.js index 090dc3ca..29b7b5ca 100644 --- a/src/experiment/procedures/endProcedure.js +++ b/src/experiment/procedures/endProcedure.js @@ -8,7 +8,6 @@ import { exitFullscreenTrial } from "../trials/fullscreen"; * 1) Trial used to complete the user's camera recording is displayed * 2) The experiment exits fullscreen * - * @param {Object} jsPsych The jsPsych instance being used to run the task * @returns {Object} A jsPsych (nested) timeline object */ export function buildEndProcedure() { diff --git a/src/experiment/procedures/honeycombProcedure.js b/src/experiment/procedures/honeycombProcedure.js index a2fd0f33..b941443e 100644 --- a/src/experiment/procedures/honeycombProcedure.js +++ b/src/experiment/procedures/honeycombProcedure.js @@ -4,6 +4,7 @@ import { ENV, SETTINGS } from "../../config/"; import { eventCodes } from "../../config/trigger"; import { pdSpotEncode, photodiodeGhostBox } from "../../lib/markup/photodiode"; import { buildFixationTrial } from "../trials/fixation"; +import { getJsPsych } from "../../lib/utils"; /** * Builds the block of trials that form the core of the Honeycomb experiment @@ -12,7 +13,6 @@ import { buildFixationTrial } from "../trials/fixation"; * * Note that the block is conditionally rendered and repeated based on the task settings * - * @param {Object} jsPsych The jsPsych instance being used to run the task * @returns {Object} A jsPsych (nested) timeline object */ export function buildHoneycombProcedure() { @@ -29,7 +29,7 @@ export function buildHoneycombProcedure() { const taskTrial = { type: imageKeyboardResponse, // Display the image passed as a timeline variable - stimulus: window.jsPsych.timelineVariable("stimulus"), + stimulus: getJsPsych().timelineVariable("stimulus"), prompt: function () { // Conditionally displays the photodiodeGhostBox if (ENV.USE_PHOTODIODE) return photodiodeGhostBox; @@ -40,7 +40,7 @@ export function buildHoneycombProcedure() { data: { // Record the correct_response passed as a timeline variable code: eventCodes.honeycomb, - correct_response: window.jsPsych.timelineVariable("correct_response"), + correct_response: getJsPsych().timelineVariable("correct_response"), }, on_load: function () { // Conditionally flashes the photodiode when the trial first loads @@ -48,7 +48,7 @@ export function buildHoneycombProcedure() { }, // Add a boolean value ("correct") to the data - if the user responded with the correct key or not on_finish: function (data) { - data.correct = window.jsPsych.pluginAPI.compareKeys(data.response, data.correct_response); + data.correct = getJsPsych().pluginAPI.compareKeys(data.response, data.correct_response); }, }; diff --git a/src/experiment/procedures/startProcedure.js b/src/experiment/procedures/startProcedure.js index c6decf26..ebe4bc45 100644 --- a/src/experiment/procedures/startProcedure.js +++ b/src/experiment/procedures/startProcedure.js @@ -15,7 +15,6 @@ import { introductionTrial } from "../trials/introduction"; * 4) Trials used to set up a photodiode and trigger box are displayed (if applicable) * 5) Trials used to set up the user's camera are displayed (if applicable) * - * @param {Object} jsPsych The jsPsych instance being used to run the task * @returns {Object} A jsPsych (nested) timeline object */ export function buildStartProcedure() { diff --git a/src/experiment/trials/camera.js b/src/experiment/trials/camera.js index 5d95aaca..d338d439 100644 --- a/src/experiment/trials/camera.js +++ b/src/experiment/trials/camera.js @@ -4,6 +4,7 @@ import initializeCamera from "@jspsych/plugin-initialize-camera"; import { LANGUAGE, ENV } from "../../config/"; import { div, h1, p, tag } from "../../lib/markup/tags"; +import { getJsPsych } from "../../lib/utils"; const WEBCAM_ID = "webcam"; @@ -48,7 +49,7 @@ export function buildCameraStartTrial() { throw new Error("video recording is only available when running inside Electron"); } - const cameraRecorder = window.jsPsych.pluginAPI.getCameraRecorder(); + const cameraRecorder = getJsPsych().pluginAPI.getCameraRecorder(); if (!cameraRecorder) { console.error("Camera is not initialized, no data will be recorded."); return; @@ -76,11 +77,11 @@ export function buildCameraStartTrial() { // Assign camera feed to the