jsPsych icon indicating copy to clipboard operation
jsPsych copied to clipboard

Fix preload plugin onerror callback

Open javidalpe opened this issue 3 years ago • 3 comments

Fix MediaAPI preloadImages onerror callback.

Expected Behavior

Given a preload trial:

var preload = {
      type: jsPsychPreload,
      images: ['blue.png', 'orange.png'],
      continue_after_error: true,
    };

With a 404 response code for blue.png and orange.png.

I'd expect the preload trial data to contains: failed_images:

  • "https://vecvqftksb.cognitive.test/preview1661654938625/orange.png"
  • "https://vecvqftksb.cognitive.test/preview1661654938625/blue.png"

Current Behavior

Preload trial data has:

  • "https://vecvqftksb.cognitive.test/preview1661654938625/orange.png"
  • "https://vecvqftksb.cognitive.test/preview1661654938625/orange.png"

Possible Solution

Implement Javascript closure on media onerror callback by replacing function syntax with arrow functions.

Steps to Reproduce

  1. Follow Simple Reaction Time tutorial steps to create a task.
  2. Do not save orange.png and blue.png in your server.
  3. Modify preload trial to include continue_after_error: true setting.
  4. Run the experiment.
  5. Once you see 'Welcome to the experiment.', check Jspsych stored data.

Context (Environment)

When you are preloading 1000 stimuli, the experiment will fail if one of them is missing.

At Cognition.run I'm trying to help users detect which stimuli is missing.

Fixing this issue would allow me to capture the list of missing stimuli and display them on the UI.

Detailed Description

The problem is about scopes and callback functions. A wrong variable scope is causing multiple callbacks to share the same variable. Each callback should has his own variables and scopes.

What I'm doing here is to change all media preloading functions (audio, image and video), so the onerror callbacks use arrow functions instead of function syntax. Why? Because this creates a closure that keeps the value of source variable overtime for this specific callback.

javidalpe avatar Aug 28 '22 03:08 javidalpe

🦋 Changeset detected

Latest commit: 6bf9ea973e7a0309dd1f5ea06043a27ea2f991bf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
jspsych Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Aug 28 '22 03:08 changeset-bot[bot]

Thanks for contributing this, and for the detailed explanation. Much appreciated!

@jodeleeuw are you happy for me to review and test this?

becky-gilbert avatar Sep 02 '22 23:09 becky-gilbert

That would be great, thanks @becky-gilbert ! and thanks @javidalpe !

jodeleeuw avatar Sep 04 '22 15:09 jodeleeuw