Skip to content

Commit

Permalink
Respect dynamically added trial/timeline descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoluc committed Nov 3, 2024
1 parent 06f6be0 commit 4e88af1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
24 changes: 19 additions & 5 deletions packages/jspsych/src/timeline/Timeline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
21 changes: 12 additions & 9 deletions packages/jspsych/src/timeline/Timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -151,14 +153,15 @@ export class Timeline extends TimelineNode {
}
}

private instantiateChildNodes() {
const newChildNodes = this.description.timeline.map((childDescription) => {
return isTimelineDescription(childDescription)
? new Timeline(this.dependencies, childDescription, this)
: new Trial(this.dependencies, childDescription, this);
});
this.children.push(...newChildNodes);
return newChildNodes;
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(newChildNode);
return newChildNode;
}

private currentTimelineVariables: Record<string, any>;
Expand Down

0 comments on commit 4e88af1

Please sign in to comment.