Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed parallel terminating one tick after its children #76

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ export abstract class Composite extends ChipBase {
super.tick(tickInfo);

this._tickChildChips();
this._terminateRequestedChildChips();
this._onAfterTick();
}

Expand Down
37 changes: 29 additions & 8 deletions tests/chip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function makeChipContext(): chip.ChipContext {

function makeTickInfo(): chip.TickInfo {
return {
timeSinceLastTick: 1 / 60,
timeSinceLastTick: 1000 / 60,
};
}

Expand Down Expand Up @@ -203,7 +203,10 @@ describe("Events", () => {

test("receives DOM-style events", () => {
class CustomEvent extends Event {
constructor(name: string, public readonly value: number) {
constructor(
name: string,
public readonly value: number,
) {
super(name);
}
}
Expand Down Expand Up @@ -570,13 +573,31 @@ describe("Parallel", () => {
// Terminate one child
children[0].requestTermination();
parent.tick(makeTickInfo());
expect(parent.chipState === "active");
expect(parent.chipState).toBe("active");

// Terminate second child
children[1].requestTermination();
parent.tick(makeTickInfo());

expect(parent.chipState === "inactive");
expect(parent.chipState).toBe("requestedTermination");
});

test("terminates on same tick as children calling _terminateSelf", () => {
// wait chip waiting exactly two makeTickInfo
const waitChip = new chip.Wait((2 * 1000) / 60);
const parent = new chip.Parallel([waitChip]);

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

expect(parent.chipState).toBe("active");
expect(waitChip.chipState).toBe("active");

// Terminate second child
parent.tick(makeTickInfo());
expect(waitChip.chipState).toBe("inactive");
expect(parent.chipState).toBe("requestedTermination");
});

test("can remove children", () => {
Expand Down Expand Up @@ -964,7 +985,7 @@ describe("StateMachine", () => {
expect(stateMachine.options.transitions.a).toBeCalledTimes(1);
expect(stateMachine.options.transitions.a).toBeCalledWith(
stateMachine.chipContext,
signal
signal,
);
});
});
Expand Down Expand Up @@ -1110,7 +1131,7 @@ describe("Hot reloading", () => {
makeTickInfo(),
makeChipContext(),
makeSignal(),
e1.makeReloadMemento()
e1.makeReloadMemento(),
);
expect(e2.value).toBe(88);
});
Expand All @@ -1126,7 +1147,7 @@ describe("Hot reloading", () => {
makeTickInfo(),
makeChipContext(),
makeSignal(),
e1.makeReloadMemento()
e1.makeReloadMemento(),
);

expect(e2.value).toBe(88);
Expand Down Expand Up @@ -1248,7 +1269,7 @@ describe("Hot reloading", () => {
makeTickInfo(),
makeChipContext(),
makeSignal(),
parentV1.makeReloadMemento()
parentV1.makeReloadMemento(),
);

// Only 2nd child should be activate and have the new value
Expand Down
Loading