Skip to content

Commit

Permalink
Added method to Parallel chip to remove all children
Browse files Browse the repository at this point in the history
  • Loading branch information
drpepper committed Dec 6, 2024
1 parent 0ebc4f9 commit 4715716
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,16 @@ export class Parallel extends Composite {
}
}

/**
* Remove the child chip, by value or index.
* If the chip is running, terminate it
*/
removeAllChildChips(): void {
while (this._childChipOptions.length > 0) {
this.removeChildChip(0);
}
}

indexOfChipActivationInfo(
chip: ActivateChildChipOptions | ChipResolvable,
): number {
Expand Down
27 changes: 27 additions & 0 deletions tests/chip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,33 @@ describe("Parallel", () => {
expect(children[1]._onTick).toBeCalledTimes(1);
expect(children[2]._onTick).toBeCalledTimes(3);
});

test("can remove all children", () => {
const children = [new MockChip(), new MockChip(), new MockChip()];
const parent = new chip.Parallel(children);

// Run once
parent.activate(makeTickInfo(), makeChipContext(), makeSignal());
parent.tick(makeTickInfo());

parent.removeAllChildChips();

expect(children[0].chipState).toBe("inactive");
expect(children[1].chipState).toBe("inactive");
expect(children[2].chipState).toBe("inactive");

// @ts-ignore
expect(parent._childChipOptions.length).toBe(0);
// @ts-ignore
expect(parent._infoToChip.size).toBe(0);

// Run again
parent.tick(makeTickInfo());

expect(children[0]._onTick).toBeCalledTimes(1);
expect(children[1]._onTick).toBeCalledTimes(1);
expect(children[2]._onTick).toBeCalledTimes(1);
});
});

describe("Sequence", () => {
Expand Down

0 comments on commit 4715716

Please sign in to comment.