Skip to content

Commit

Permalink
Merge pull request jspsych#3339 from jspsych/feature-default-finish-t…
Browse files Browse the repository at this point in the history
…imeline

Make `finishTrial` clear the display and remove timeouts
  • Loading branch information
jodeleeuw authored Jul 12, 2024
2 parents 29e7451 + 74b4adc commit 7679794
Show file tree
Hide file tree
Showing 56 changed files with 81 additions and 203 deletions.
58 changes: 58 additions & 0 deletions .changeset/rotten-mails-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
"jspsych": major
"@jspsych/plugin-animation": major
"@jspsych/plugin-audio-button-response": major
"@jspsych/plugin-audio-keyboard-response": major
"@jspsych/plugin-audio-slider-response": major
"@jspsych/plugin-browser-check": major
"@jspsych/plugin-call-function": major
"@jspsych/plugin-canvas-button-response": major
"@jspsych/plugin-canvas-keyboard-response": major
"@jspsych/plugin-canvas-slider-response": major
"@jspsych/plugin-categorize-animation": major
"@jspsych/plugin-categorize-html": major
"@jspsych/plugin-categorize-image": major
"@jspsych/plugin-cloze": major
"@jspsych/plugin-external-html": major
"@jspsych/plugin-free-sort": major
"@jspsych/plugin-fullscreen": major
"@jspsych/plugin-html-audio-response": major
"@jspsych/plugin-html-button-response": major
"@jspsych/plugin-html-keyboard-response": major
"@jspsych/plugin-html-slider-response": major
"@jspsych/plugin-html-video-response": major
"@jspsych/plugin-iat-html": major
"@jspsych/plugin-iat-image": major
"@jspsych/plugin-image-button-response": major
"@jspsych/plugin-image-keyboard-response": major
"@jspsych/plugin-image-slider-response": major
"@jspsych/plugin-initialize-camera": major
"@jspsych/plugin-initialize-microphone": major
"@jspsych/plugin-instructions": major
"@jspsych/plugin-maxdiff": major
"@jspsych/plugin-mirror-camera": major
"@jspsych/plugin-preload": major
"@jspsych/plugin-reconstruction": major
"@jspsych/plugin-resize": major
"@jspsych/plugin-same-different-html": major
"@jspsych/plugin-same-different-image": major
"@jspsych/plugin-serial-reaction-time": major
"@jspsych/plugin-serial-reaction-time-mouse": major
"@jspsych/plugin-sketchpad": major
"@jspsych/plugin-survey": major
"@jspsych/plugin-survey-html-form": major
"@jspsych/plugin-survey-likert": major
"@jspsych/plugin-survey-multi-choice": major
"@jspsych/plugin-survey-multi-select": major
"@jspsych/plugin-survey-text": major
"@jspsych/plugin-video-button-response": major
"@jspsych/plugin-video-keyboard-response": major
"@jspsych/plugin-video-slider-response": major
"@jspsych/plugin-virtual-chinrest": major
"@jspsych/plugin-visual-search-circle": major
"@jspsych/plugin-webgazer-calibrate": major
"@jspsych/plugin-webgazer-init-camera": major
"@jspsych/plugin-webgazer-validate": major
---

`finishTrial()` now clears the display and any timeouts set with `pluginApi.setTimeout()`
2 changes: 2 additions & 0 deletions packages/jspsych/src/JsPsych.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ export class JsPsych {
getDefaultIti: () => this.getInitSettings().default_iti,

finishTrialPromise: this.finishTrialPromise,

clearAllTimeouts: () => this.pluginAPI.clearAllTimeouts(),
};

private extensionManagerDependencies: ExtensionManagerDependencies = {
Expand Down
11 changes: 11 additions & 0 deletions packages/jspsych/src/timeline/Trial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ export class Trial extends TimelineNode {
result = await trialPromise;
}

// The trial has finished, time to clean up.
this.cleanupTrial();

return result;
}

Expand Down Expand Up @@ -147,6 +150,14 @@ export class Trial extends TimelineNode {
};
}

/**
* Cleanup the trial by removing the display element and removing event listeners
*/
private cleanupTrial() {
this.dependencies.clearAllTimeouts();
this.dependencies.getDisplayElement().innerHTML = "";
}

/**
* Add the CSS classes from the `css_classes` parameter to the display element
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/jspsych/src/timeline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ export interface TimelineNodeDependencies {
* is called.
*/
finishTrialPromise: PromiseWrapper<TrialResult | void>;

/**
* Clear all of the timeouts
*/
clearAllTimeouts: () => void;
}

export type TrialResult = Record<string, any>;
Expand Down
2 changes: 1 addition & 1 deletion packages/jspsych/tests/core/abortexperiment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test("if on_finish returns a Promise, wait for resolve before showing end messag
const { getHTML, expectFinished, expectRunning } = await startTimeline(timeline, jsPsych);

expect(getHTML()).toMatch("foo");
pressKey("a");
await pressKey("a");
expect(getHTML()).not.toMatch("foo");
expect(getHTML()).not.toMatch("bar");

Expand Down
2 changes: 2 additions & 0 deletions packages/jspsych/tests/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export class TimelineNodeDependenciesMock implements TimelineNodeDependencies {
getDefaultIti = jest.fn(() => 0);

finishTrialPromise = new PromiseWrapper<TrialResult>();

clearAllTimeouts = jest.fn();
}

/**
Expand Down
6 changes: 0 additions & 6 deletions packages/plugin-audio-button-response/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,6 @@ class AudioButtonResponsePlugin implements JsPsychPlugin<Info> {

// function to end trial when it is time
const end_trial = () => {
// kill any remaining setTimeout handlers
this.jsPsych.pluginAPI.clearAllTimeouts();

// stop the audio file if it is playing
// remove end event listeners if they exist
if (context !== null) {
Expand All @@ -299,9 +296,6 @@ class AudioButtonResponsePlugin implements JsPsychPlugin<Info> {
response: response.button,
};

// clear the display
display_element.innerHTML = "";

// move on to the next trial
this.jsPsych.finishTrial(trial_data);

Expand Down
6 changes: 0 additions & 6 deletions packages/plugin-audio-keyboard-response/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ class AudioKeyboardResponsePlugin implements JsPsychPlugin<Info> {

// function to end trial when it is time
const end_trial = () => {
// kill any remaining setTimeout handlers
this.jsPsych.pluginAPI.clearAllTimeouts();

// stop the audio file if it is playing
// remove end event listeners if they exist
if (context !== null) {
Expand All @@ -206,9 +203,6 @@ class AudioKeyboardResponsePlugin implements JsPsychPlugin<Info> {
response: response.key,
};

// clear the display
display_element.innerHTML = "";

// move on to the next trial
this.jsPsych.finishTrial(trial_data);

Expand Down
5 changes: 0 additions & 5 deletions packages/plugin-audio-slider-response/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,6 @@ class AudioSliderResponsePlugin implements JsPsychPlugin<Info> {
}

const end_trial = () => {
// kill any remaining setTimeout handlers
this.jsPsych.pluginAPI.clearAllTimeouts();

// stop the audio file if it is playing
// remove end event listeners if they exist
if (context !== null) {
Expand All @@ -362,8 +359,6 @@ class AudioSliderResponsePlugin implements JsPsychPlugin<Info> {
response: response.response,
};

display_element.innerHTML = "";

// next trial
this.jsPsych.finishTrial(trialdata);

Expand Down
2 changes: 0 additions & 2 deletions packages/plugin-browser-check/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,6 @@ class BrowserCheckPlugin implements JsPsychPlugin<Info> {
}

private end_trial(feature_data) {
this.jsPsych.getDisplayElement().innerHTML = "";

const trial_data = { ...Object.fromEntries(feature_data) };

this.jsPsych.finishTrial(trial_data);
Expand Down
6 changes: 0 additions & 6 deletions packages/plugin-canvas-button-response/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,12 @@ class CanvasButtonResponsePlugin implements JsPsychPlugin<Info> {

// function to end trial when it is time
const end_trial = () => {
// kill any remaining setTimeout handlers
this.jsPsych.pluginAPI.clearAllTimeouts();

// gather the data to store for the trial
var trial_data = {
rt: response.rt,
response: response.button,
};

// clear the display
display_element.innerHTML = "";

// move on to the next trial
this.jsPsych.finishTrial(trial_data);
};
Expand Down
6 changes: 0 additions & 6 deletions packages/plugin-canvas-keyboard-response/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ class CanvasKeyboardResponsePlugin implements JsPsychPlugin<Info> {

// function to end trial when it is time
const end_trial = () => {
// kill any remaining setTimeout handlers
this.jsPsych.pluginAPI.clearAllTimeouts();

// kill keyboard listeners
if (typeof keyboardListener !== "undefined") {
this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
Expand All @@ -138,9 +135,6 @@ class CanvasKeyboardResponsePlugin implements JsPsychPlugin<Info> {
response: response.key,
};

// clear the display
display_element.innerHTML = "";

// move on to the next trial
this.jsPsych.finishTrial(trial_data);
};
Expand Down
4 changes: 0 additions & 4 deletions packages/plugin-canvas-slider-response/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,13 @@ class CanvasSliderResponsePlugin implements JsPsychPlugin<Info> {
};

const end_trial = () => {
this.jsPsych.pluginAPI.clearAllTimeouts();

// save data
var trialdata = {
rt: response.rt,
response: response.response,
slider_start: trial.slider_start,
};

display_element.innerHTML = "";

// next trial
this.jsPsych.finishTrial(trialdata);
};
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-categorize-animation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ class CategorizeAnimationPlugin implements JsPsychPlugin<Info> {

const endTrial = () => {
clearInterval(animate_interval); // stop animation!
display_element.innerHTML = ""; // clear everything
this.jsPsych.finishTrial(trial_data);
};

Expand Down
6 changes: 0 additions & 6 deletions packages/plugin-categorize-html/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ class CategorizeHtmlPlugin implements JsPsychPlugin<Info> {

// create response function
const after_response = (info: { key: string; rt: number }) => {
// kill any remaining setTimeout handlers
this.jsPsych.pluginAPI.clearAllTimeouts();

// clear keyboard listener
this.jsPsych.pluginAPI.cancelAllKeyboardResponses();

Expand All @@ -153,8 +150,6 @@ class CategorizeHtmlPlugin implements JsPsychPlugin<Info> {
response: info.key,
};

display_element.innerHTML = "";

var timeout = info.rt == null;
doFeedback(correct, timeout);
};
Expand All @@ -177,7 +172,6 @@ class CategorizeHtmlPlugin implements JsPsychPlugin<Info> {
}

const endTrial = () => {
display_element.innerHTML = "";
this.jsPsych.finishTrial(trial_data);
};

Expand Down
6 changes: 0 additions & 6 deletions packages/plugin-categorize-image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@ class CategorizeImagePlugin implements JsPsychPlugin<Info> {

// create response function
const after_response = (info: { key: string; rt: number }) => {
// kill any remaining setTimeout handlers
this.jsPsych.pluginAPI.clearAllTimeouts();

// clear keyboard listener
this.jsPsych.pluginAPI.cancelAllKeyboardResponses();

Expand All @@ -154,8 +151,6 @@ class CategorizeImagePlugin implements JsPsychPlugin<Info> {
response: info.key,
};

display_element.innerHTML = "";

var timeout = info.rt == null;
doFeedback(correct, timeout);
};
Expand All @@ -178,7 +173,6 @@ class CategorizeImagePlugin implements JsPsychPlugin<Info> {
}

const endTrial = () => {
display_element.innerHTML = "";
this.jsPsych.finishTrial(trial_data);
};

Expand Down
1 change: 0 additions & 1 deletion packages/plugin-cloze/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ class ClozePlugin implements JsPsychPlugin<Info> {
response: answers,
};

display_element.innerHTML = "";
this.jsPsych.finishTrial(trial_data);
}
};
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-external-html/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ class ExternalHtmlPlugin implements JsPsychPlugin<Info> {
rt: Math.round(performance.now() - t0),
url: trial.url,
};
display_element.innerHTML = "";
this.jsPsych.finishTrial(trial_data);
trial_complete();
};
Expand Down
2 changes: 0 additions & 2 deletions packages/plugin-free-sort/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,6 @@ class FreeSortPlugin implements JsPsychPlugin<Info> {
rt: rt,
};

// advance to next part
display_element.innerHTML = "";
this.jsPsych.finishTrial(trial_data);
}
});
Expand Down
6 changes: 0 additions & 6 deletions packages/plugin-html-audio-response/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,6 @@ class HtmlAudioResponsePlugin implements JsPsychPlugin<Info> {
this.recorder.removeEventListener("start", this.start_event_handler);
this.recorder.removeEventListener("stop", this.stop_event_handler);

// kill any remaining setTimeout handlers
this.jsPsych.pluginAPI.clearAllTimeouts();

// gather the data to store for the trial
var trial_data: any = {
rt: this.rt,
Expand All @@ -286,9 +283,6 @@ class HtmlAudioResponsePlugin implements JsPsychPlugin<Info> {
URL.revokeObjectURL(this.audio_url);
}

// clear the display
display_element.innerHTML = "";

// move on to the next trial
this.jsPsych.finishTrial(trial_data);
}
Expand Down
6 changes: 0 additions & 6 deletions packages/plugin-html-button-response/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,13 @@ class HtmlButtonResponsePlugin implements JsPsychPlugin<Info> {

// function to end trial when it is time
const end_trial = () => {
// kill any remaining setTimeout handlers
this.jsPsych.pluginAPI.clearAllTimeouts();

// gather the data to store for the trial
var trial_data = {
rt: response.rt,
stimulus: trial.stimulus,
response: response.button,
};

// clear the display
display_element.innerHTML = "";

// move on to the next trial
this.jsPsych.finishTrial(trial_data);
};
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-html-keyboard-response/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe("html-keyboard-response", () => {
);

await pressKey("f");
expect(getHTML()).toBe("");
await expectFinished();
});

Expand Down
Loading

0 comments on commit 7679794

Please sign in to comment.