Respect dynamically added trial/timeline descriptions

This commit is contained in:
bjoluc 2024-11-03 19:13:24 +01:00
parent 06f6be0961
commit 4e88af18c1
2 changed files with 31 additions and 14 deletions

View File

@ -58,6 +58,23 @@ describe("Timeline", () => {
expect((children[1] as Timeline).children.map((child) => child.index)).toEqual([1, 2]); 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`", () => { describe("with `pause()` and `resume()` calls`", () => {
beforeEach(() => { beforeEach(() => {
TestPlugin.setManualFinishTrialMode(); TestPlugin.setManualFinishTrialMode();
@ -84,12 +101,9 @@ describe("Timeline", () => {
await TestPlugin.finishTrial(); await TestPlugin.finishTrial();
expect(timeline.children[1].getStatus()).toBe(TimelineNodeStatus.COMPLETED); 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. // The timeline is paused, so it shouldn't have instantiated the next child node yet.
await TestPlugin.finishTrial(); expect(timeline.children.length).toEqual(2);
expect(timeline.children[2].getStatus()).toBe(TimelineNodeStatus.PENDING);
timeline.resume(); timeline.resume();
await flushPromises(); await flushPromises();

View File

@ -75,7 +75,9 @@ export class Timeline extends TimelineNode {
for (const timelineVariableIndex of timelineVariableOrder) { for (const timelineVariableIndex of timelineVariableOrder) {
this.setCurrentTimelineVariablesByIndex(timelineVariableIndex); 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; const previousChild = this.currentChild;
this.currentChild = childNode; this.currentChild = childNode;
childNode.index = previousChild childNode.index = previousChild
@ -151,14 +153,15 @@ export class Timeline extends TimelineNode {
} }
} }
private instantiateChildNodes() { private instantiateChildNode(
const newChildNodes = this.description.timeline.map((childDescription) => { childDescription: TimelineDescription | TimelineArray | TrialDescription
return isTimelineDescription(childDescription) ) {
? new Timeline(this.dependencies, childDescription, this) const newChildNode = isTimelineDescription(childDescription)
: new Trial(this.dependencies, childDescription, this); ? 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>; private currentTimelineVariables: Record<string, any>;