From 3e833670cb83eda1422a0142ac74121e525de627 Mon Sep 17 00:00:00 2001 From: Martin Grewe Date: Thu, 13 Oct 2022 17:36:29 +0200 Subject: [PATCH] feat: add callbacks on experiment finish --- packages/jspsych/src/JsPsych.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/jspsych/src/JsPsych.ts b/packages/jspsych/src/JsPsych.ts index 6dff7ee0a7..9c0e5ede6e 100644 --- a/packages/jspsych/src/JsPsych.ts +++ b/packages/jspsych/src/JsPsych.ts @@ -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; @@ -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;