mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-10 11:10:54 +00:00
Respect dynamically added trial/timeline descriptions
This commit is contained in:
parent
06f6be0961
commit
4e88af18c1
@ -58,6 +58,23 @@ describe("Timeline", () => {
|
||||
expect((children[1] as Timeline).children.map((child) => child.index)).toEqual([1, 2]);
|
||||
});
|
||||
|
||||
it("respects dynamically added child node descriptions", async () => {
|
||||
TestPlugin.setManualFinishTrialMode();
|
||||
|
||||
const timelineDescription: TimelineArray = [{ type: TestPlugin }];
|
||||
const timeline = createTimeline(timelineDescription);
|
||||
|
||||
const runPromise = timeline.run();
|
||||
expect(timeline.children.length).toEqual(1);
|
||||
|
||||
timelineDescription.push({ timeline: [{ type: TestPlugin }] });
|
||||
await TestPlugin.finishTrial();
|
||||
await TestPlugin.finishTrial();
|
||||
await runPromise;
|
||||
|
||||
expect(timeline.children.length).toEqual(2);
|
||||
});
|
||||
|
||||
describe("with `pause()` and `resume()` calls`", () => {
|
||||
beforeEach(() => {
|
||||
TestPlugin.setManualFinishTrialMode();
|
||||
@ -84,12 +101,9 @@ describe("Timeline", () => {
|
||||
|
||||
await TestPlugin.finishTrial();
|
||||
expect(timeline.children[1].getStatus()).toBe(TimelineNodeStatus.COMPLETED);
|
||||
expect(timeline.children[2].getStatus()).toBe(TimelineNodeStatus.PENDING);
|
||||
|
||||
// Resolving the next trial promise shouldn't continue the experiment since no trial should be running.
|
||||
await TestPlugin.finishTrial();
|
||||
|
||||
expect(timeline.children[2].getStatus()).toBe(TimelineNodeStatus.PENDING);
|
||||
// The timeline is paused, so it shouldn't have instantiated the next child node yet.
|
||||
expect(timeline.children.length).toEqual(2);
|
||||
|
||||
timeline.resume();
|
||||
await flushPromises();
|
||||
|
@ -75,7 +75,9 @@ export class Timeline extends TimelineNode {
|
||||
for (const timelineVariableIndex of timelineVariableOrder) {
|
||||
this.setCurrentTimelineVariablesByIndex(timelineVariableIndex);
|
||||
|
||||
for (const childNode of this.instantiateChildNodes()) {
|
||||
for (const childNodeDescription of this.description.timeline) {
|
||||
const childNode = this.instantiateChildNode(childNodeDescription);
|
||||
|
||||
const previousChild = this.currentChild;
|
||||
this.currentChild = childNode;
|
||||
childNode.index = previousChild
|
||||
@ -151,14 +153,15 @@ export class Timeline extends TimelineNode {
|
||||
}
|
||||
}
|
||||
|
||||
private instantiateChildNodes() {
|
||||
const newChildNodes = this.description.timeline.map((childDescription) => {
|
||||
return isTimelineDescription(childDescription)
|
||||
private instantiateChildNode(
|
||||
childDescription: TimelineDescription | TimelineArray | TrialDescription
|
||||
) {
|
||||
const newChildNode = isTimelineDescription(childDescription)
|
||||
? new Timeline(this.dependencies, childDescription, this)
|
||||
: new Trial(this.dependencies, childDescription, this);
|
||||
});
|
||||
this.children.push(...newChildNodes);
|
||||
return newChildNodes;
|
||||
|
||||
this.children.push(newChildNode);
|
||||
return newChildNode;
|
||||
}
|
||||
|
||||
private currentTimelineVariables: Record<string, any>;
|
||||
|
Loading…
Reference in New Issue
Block a user