implements a first pass at #1237

This commit is contained in:
Josh de Leeuw 2020-12-28 13:59:19 -05:00
parent 42962039af
commit 24369ff282
2 changed files with 70 additions and 0 deletions

View File

@ -334,6 +334,10 @@ window.jsPsych = (function() {
}
}
core.allTimelineVariables = function(){
return timeline.allTimelineVariables();
}
core.addNodeToEndOfTimeline = function(new_timeline, preload_callback){
timeline.insert(new_timeline);
if(typeof preload_callback !== 'undefined'){
@ -617,6 +621,42 @@ window.jsPsych = (function() {
}
}
// recursively get all the timeline variables for this trial
this.allTimelineVariables = function(){
var all_tvs = this.allTimelineVariablesNames();
var all_tvs_vals = {};
for(var i=0; i<all_tvs.length; i++){
all_tvs_vals[all_tvs[i]] = this.timelineVariable(all_tvs[i])
}
return all_tvs_vals;
}
// helper to get all the names at this stage.
this.allTimelineVariablesNames = function(so_far){
if(typeof so_far == 'undefined'){
so_far = [];
}
if(typeof timeline_parameters !== 'undefined'){
so_far = so_far.concat(Object.keys(timeline_parameters.timeline_variables[progress.order[progress.current_variable_set]]));
// if progress.current_location is -1, then the timeline variable is being evaluated
// in a function that runs prior to the trial starting, so we should treat that trial
// as being the active trial for purposes of finding the value of the timeline variable
var loc = Math.max(0, progress.current_location);
// if loc is greater than the number of elements on this timeline, then the timeline
// variable is being evaluated in a function that runs after the trial on the timeline
// are complete but before advancing to the next (like a loop_function).
// treat the last active trial as the active trial for this purpose.
if(loc == timeline_parameters.timeline.length){
loc = loc - 1;
}
// now find the variable
return timeline_parameters.timeline[loc].allTimelineVariablesNames(so_far);
}
if(typeof timeline_parameters == 'undefined'){
return so_far;
}
}
// recursively get the number of **trials** contained in the timeline
// assuming that while loops execute exactly once and if conditionals
// always run

View File

@ -252,3 +252,33 @@ describe('timeline variables are correctly evaluated', function(){
})
describe('allTimelineVariables', function(){
test('gets all timeline variables for a simple timeline', function(){
var t = {
timeline: [{
type: 'html-keyboard-response',
stimulus: 'foo',
on_finish: function(data){
var all_tvs = jsPsych.allTimelineVariables();
Object.assign(data, all_tvs);
}
}],
timeline_variables: [
{a: 1, b: 2},
{a: 2, b: 3}
]
}
jsPsych.init({timeline: [t]});
utils.pressKey(32);
utils.pressKey(32);
var data = jsPsych.data.get().values();
expect(data[0].a).toBe(1);
expect(data[0].b).toBe(2);
expect(data[1].a).toBe(2);
expect(data[1].b).toBe(3);
});
})