Skip to content

feat: add callbacks on experiment finish #2822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/jspsych/src/JsPsych.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ export class JsPsych {
*/
private simulation_options;

/**
* Callbacks for extensions/plugins called on finish of the experiments.
* Can be used to cleanup stuff
*/

private onfinish_callbacks = new Array<() => void>();

// storing a single webaudio context to prevent problems with multiple inits
// of jsPsych
webaudio_context: AudioContext = null;
Expand Down Expand Up @@ -507,9 +514,17 @@ export class JsPsych {
this.doTrial(this.timeline.trial());
}

public addFinishExperimentCallback(callback: () => void) {
this.onfinish_callbacks.push(callback);
}

private finishExperiment() {
const finish_result = this.opts.on_finish(this.data.get());

for (var callback of this.onfinish_callbacks) {
callback();
}

const done_handler = () => {
if (typeof this.timeline.end_message !== "undefined") {
this.DOM_target.innerHTML = this.timeline.end_message;
Expand Down