1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-12 08:38:10 +00:00

added fromSnapshot static method to TrialHandler to restore internal state from snapshot; should fix the nested loop thisN issue

This commit is contained in:
Alain Pitiot 2021-06-11 07:20:30 +02:00
parent 725e5ca28f
commit fcd7642bee
2 changed files with 32 additions and 1 deletions

View File

@ -212,6 +212,7 @@ export class TrialHandler extends PsychObject
/**
* @typedef {Object} Snapshot
* @property {TrialHandler} handler - the trialHandler
* @property {string} name - the trialHandler name
* @property {number} nStim - the number of stimuli
* @property {number} nTotal - the total number of trials that will be run
@ -237,6 +238,7 @@ export class TrialHandler extends PsychObject
const currentIndex = this.thisIndex;
const snapshot = {
handler: this,
name: this.name,
nStim: this.nStim,
nTotal: this.nTotal,
@ -250,6 +252,8 @@ export class TrialHandler extends PsychObject
getCurrentTrial: () => this.getTrial(currentIndex),
getTrial: (index = 0) => this.getTrial(index),
addData: (key, value) => this.addData(key, value)
};
this._snapshots.push(snapshot);
@ -258,6 +262,33 @@ export class TrialHandler extends PsychObject
}
/**
* Set the internal state of this trial handler from the given snapshot.
*
* @public
* @static
* @param {Snapshot} snapshot - the snapshot from which to update the current internal state.
*/
static fromSnapshot(snapshot)
{
// if snapshot is undefined, do nothing:
if (typeof snapshot === 'undefined')
{
return;
}
snapshot.handler.nStim = snapshot.nStim;
snapshot.handler.nTotal = snapshot.nTotal;
snapshot.handler.nRemaining = snapshot.nRemaining;
snapshot.handler.thisRepN = snapshot.thisRepN;
snapshot.handler.thisTrialN = snapshot.thisTrialN;
snapshot.handler.thisN = snapshot.thisN;
snapshot.handler.thisIndex = snapshot.thisIndex;
snapshot.handler.ran = snapshot.ran;
snapshot.handler._finished = snapshot._finished;
}
/**
* Setter for the finished attribute.
*

View File

@ -1,3 +1,3 @@
export * from './ExperimentHandler.js';
export * from './TrialHandler.js';
export * from './Shelf.js';
//export * from './Shelf.js';