Skip to content

Commit

Permalink
fix(events): Properly execute nested TrackedEvents (finally)
Browse files Browse the repository at this point in the history
fix #83
  • Loading branch information
jcowman2 committed Nov 7, 2019
1 parent 6c0f2b8 commit 83b9608
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/events/impl/event-builders.ts
Expand Up @@ -86,7 +86,7 @@ export const on = <StateType = any>(
event.eventName = eventName;

if (isTrackedEvent(eventFunc)) {
event.target = eventFunc.target;
event.target = () => eventFunc; // Boy oh boy this line gave me grief
} else {
event.target = eventFunc;
}
Expand Down
5 changes: 3 additions & 2 deletions test/unit/agents.test.ts
Expand Up @@ -27,7 +27,8 @@ import {
DEFAULT_EVENT_NAME,
getUntrackedEventPK,
TrackedEvent,
EventFunction
EventFunction,
isEventQueue
} from "../../src/events";
import { on } from "../../src/events";
import { buildGameInstance } from "../../src/state";
Expand Down Expand Up @@ -2054,7 +2055,7 @@ describe("Agents", function() {
]);
});

it("Complicated nesting of TrackedEvents is okay", function() {
it("Setting complex tracked events as agent properties is okay", function() {
Game.init(MD);
const func = on("1", on("2", () => {}).then(on("3", () => {})));
const myGame = buildGameInstance();
Expand Down
28 changes: 28 additions & 0 deletions test/unit/events.test.ts
Expand Up @@ -483,6 +483,18 @@ describe("Events", function() {
expect(isEventQueue(eq)).to.be.true;
});

it("Complicated nesting of TrackedEvents is okay", function() {
Game.init(MD);
const bar = on("BAR", game => game.output.write("bar"));
const inner = on("INNER", bar.then(bar));
const func = on("OUTER", inner);

const myGame = buildGameInstance();
func(myGame);

expect(myGame.output.lines[0].data).to.equal("bar");
});

describe("QTests", function() {
// Start utility functions

Expand Down Expand Up @@ -1003,5 +1015,21 @@ describe("Events", function() {
expect(basePKs[0].minus(1).equals(basePKs[1])).to.be.true;
expect(basePKs[0].equals(newGame.events.history[0].id)).to.be.true;
});

it("When `on` has a TrackedEvent as its second argument, the event is considered caused by the outer", function() {
Game.init(MD);
const bar = on("BAR", game => game.output.write("bar"));
const outer = on("OUTER", bar);

const myGame = buildGameInstance();
outer(myGame);

const [event1, event2] = myGame.events.history;

expect(event1.name).to.equal("BAR");
expect(event2.name).to.equal("OUTER");
expect(event1.causedBy.equals(event2.id)).to.be.true;
expect(event2.caused[0].equals(event1.id)).to.be.true;
});
});
});

0 comments on commit 83b9608

Please sign in to comment.