allow parameters for extensions in a trial

This commit is contained in:
Josh de Leeuw 2021-02-05 14:15:51 -05:00
parent c6fe79549c
commit 18014cbe5f
2 changed files with 15 additions and 13 deletions

View File

@ -17,16 +17,18 @@ To use an extension in an experiment, you'll load the extension file via a `<scr
jsPsych.init({ jsPsych.init({
timeline: [...], timeline: [...],
extensions: [ extensions: [
{type: 'some-extension', params: {...} {type: 'some-extension', params: {...} }
] ]
}) })
``` ```
To enable an extension during a trial, add the extension name to the `extensions` parameter list for the trial: To enable an extension during a trial, add the extension to the `extensions` list for the trial. Some extensions may also support or require an object of parameters to configure the extension:
```js ```js
var trial = { var trial = {
extensions: ['some-extension'] extensions: [
{type: 'some-extension', params: {...} }
]
} }
``` ```
@ -49,15 +51,15 @@ jsPsych.extensions['new-extension'] = (function () {
} }
extension.on_start = function(){ extension.on_start = function(params){
} }
extension.on_load = function(){ extension.on_load = function(params){
} }
extension.on_finish = function(){ extension.on_finish = function(params){
return { return {
// any data that the extension will add to a trial // any data that the extension will add to a trial
} }
@ -69,12 +71,12 @@ jsPsych.extensions['new-extension'] = (function () {
The four events that an extension must support are shown in the sample code. The four events that an extension must support are shown in the sample code.
`extension.initialize` is called with `jsPsych.init()`. This is where setup code for the extension can happen. This event will happen once per experiment, unlike the other events which occur with each trial. The `params` object can include whatever parameters are necessary to configure the extension. The params object is passed from the call to `jsPsych.init()` to the `extension.initialize` method. This method must return a `Promise` that resolves when the extension is finished initializing. `extension.initialize` is called with `jsPsych.init()`. This is where setup code for the extension can happen. This event will happen once per experiment, unlike the other events which occur with each trial. The `params` object can include whatever parameters are necessary to configure the extension. The `params` object is passed from the call to `jsPsych.init()` to the `extension.initialize` method. `extension.initialize` must return a `Promise` that resolves when the extension is finished initializing.
`extension.on_start` is called at the start of the plugin execution, prior to calling `plugin.trial`. This is where trial-specific initialization can happen, such as creating empty containers to hold data or resetting internal state. `extension.on_start` is called at the start of the plugin execution, prior to calling `plugin.trial`. This is where trial-specific initialization can happen, such as creating empty containers to hold data or resetting internal state. The `params` object is passed from the declaration of the extension in the trial object. You can use `params` to customize the behavior of the extension for each trial.
`extension.on_load` is called after `plugin.trial` has executed, which is typically when the plugin has finished executing initial DOM-modifying code and has set up various event listeners. This is where the extension can begin actively interacting with the DOM and recording data. `extension.on_load` is called after `plugin.trial` has executed, which is typically when the plugin has finished executing initial DOM-modifying code and has set up various event listeners. This is where the extension can begin actively interacting with the DOM and recording data. The `params` object is passed from the declaration of the extension in the trial object. You can use `params` to customize the behavior of the extension for each trial.
`extension.on_finish` is called after the plugin completes. This can be used for any teardown at the end of the trial. This method should return an object of data to append to the plugin's data. Note that this event fires *before* the `on_finish` event for the plugin, so data added by the extension is accessible in any trial `on_finish` event handlers. `extension.on_finish` is called after the plugin completes. This can be used for any teardown at the end of the trial. This method should return an object of data to append to the plugin's data. Note that this event fires *before* the `on_finish` event for the plugin, so data added by the extension is accessible in any trial `on_finish` event handlers. The `params` object is passed from the declaration of the extension in the trial object. You can use `params` to customize the behavior of the extension for each trial.
The extension can also include any additional methods that are necessary for interacting with it. See the [webgazer extension](/extensions/jspsych-ext-webgazer.md) for an example. The extension can also include any additional methods that are necessary for interacting with it. See the [webgazer extension](/extensions/jspsych-ext-webgazer.md) for an example.

View File

@ -296,7 +296,7 @@ window.jsPsych = (function() {
// handle extension callbacks // handle extension callbacks
if(Array.isArray(current_trial.extensions)){ if(Array.isArray(current_trial.extensions)){
for(var i=0; i<current_trial.extensions.length; i++){ for(var i=0; i<current_trial.extensions.length; i++){
var ext_data_values = jsPsych.extensions[current_trial.extensions[i]].on_finish(); var ext_data_values = jsPsych.extensions[current_trial.extensions[i].type].on_finish(current_trial.extensions[i].params);
Object.assign(trial_data_values, ext_data_values); Object.assign(trial_data_values, ext_data_values);
} }
} }
@ -910,7 +910,7 @@ window.jsPsych = (function() {
// call any on_start functions for extensions // call any on_start functions for extensions
if(Array.isArray(trial.extensions)){ if(Array.isArray(trial.extensions)){
for(var i=0; i<trial.extensions.length; i++){ for(var i=0; i<trial.extensions.length; i++){
jsPsych.extensions[trial.extensions[i]].on_start(); jsPsych.extensions[trial.extensions[i].type].on_start(current_trial.extensions[i].params);
} }
} }
@ -931,7 +931,7 @@ window.jsPsych = (function() {
// call any on_load functions for extensions // call any on_load functions for extensions
if(Array.isArray(trial.extensions)){ if(Array.isArray(trial.extensions)){
for(var i=0; i<trial.extensions.length; i++){ for(var i=0; i<trial.extensions.length; i++){
jsPsych.extensions[trial.extensions[i]].on_load(); jsPsych.extensions[trial.extensions[i].type].on_load(current_trial.extensions[i].params);
} }
} }
} }