From c3372ba3c671263d651b87fd1dd7f6ede483ac99 Mon Sep 17 00:00:00 2001 From: Joe Cowman Date: Wed, 31 Oct 2018 12:34:40 -0500 Subject: [PATCH 1/2] fix(event): Allow EventQueues to be invoked like any other EventFunction --- src/events/event-model.ts | 27 +++++++++++++++++++++------ test/unit/events.test.ts | 14 +++++++------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/events/event-model.ts b/src/events/event-model.ts index 95f0da4..4ae4e05 100644 --- a/src/events/event-model.ts +++ b/src/events/event-model.ts @@ -82,7 +82,7 @@ export const isTrackedEvent = (o: any): o is TrackedEvent => /** Ensures the object is an `EventQueue`. */ export const isEventQueue = (o: any): o is EventQueue => - o !== undefined && (o as EventQueue).nq !== undefined; + o !== undefined && (o as EventQueue).immediateEvents !== undefined; /** "No operation" - reserved `TrackedEvent` that signals no more events. */ export const noop: TrackedEvent = (() => { @@ -96,9 +96,18 @@ export const noop: TrackedEvent = (() => { return event; })(); -/** Creates a function that returns an error upon invocation. */ -const illegalEventQueueInvocation = () => (game: GameInstance): undefined => { - throw new RegalError("Cannot invoke an EventQueue."); +/** Builds an `EventFunction` that allows an `EventQueue` to be invoked like any other `EventFunction`. */ +const queueInvocation = ( + immediateEvents: TrackedEvent[], + delayedEvents: TrackedEvent[] +): EventFunction => (game: GameInstance) => { + // Will seem like an EventQueue to the GameInstance, but has no additional methods + const fauxQueue = { + delayedEvents, + immediateEvents + } as EventQueue; + + game.events.invoke(fauxQueue); }; /** @@ -113,8 +122,14 @@ const buildEventQueue = ( immediateEvents: TrackedEvent[], delayedEvents: TrackedEvent[] ): EventQueue => { - const eq = illegalEventQueueInvocation() as EventQueue; - eq.target = illegalEventQueueInvocation(); + const queueInvocationFunction = queueInvocation( + immediateEvents, + delayedEvents + ); + + const eq = queueInvocationFunction as EventQueue; + eq.target = queueInvocationFunction; + eq.then = thenConstructor(eq); eq.thenq = (...events: TrackedEvent[]) => eq.then(enqueue(...events)); diff --git a/test/unit/events.test.ts b/test/unit/events.test.ts index 2e74a19..0ea4c00 100644 --- a/test/unit/events.test.ts +++ b/test/unit/events.test.ts @@ -364,16 +364,16 @@ describe("Events", function() { ]); }); - it("Attempting to invoke an EventQueue throws an error", function() { - const nonevent = on("FOO", game => noop); - const queue = nq(nonevent, nonevent); + it("Invoking an EventQueue does not throw an error", function() { + const spam = on("SPAM", game => { + game.output.write("Get spammed."); + }); Game.init(); - expect(() => queue(new GameInstance())).to.throw( - RegalError, - "Cannot invoke an EventQueue." - ); + const myGame = new GameInstance(); + + spam.then(spam)(myGame); }); describe("QTests", function() { From 9bfbf837ff26bb9a99d9560c067eb1658b7da71e Mon Sep 17 00:00:00 2001 From: Joe Cowman Date: Wed, 31 Oct 2018 12:37:01 -0500 Subject: [PATCH 2/2] test(event): Test is more robust --- test/unit/events.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/unit/events.test.ts b/test/unit/events.test.ts index 0ea4c00..47da270 100644 --- a/test/unit/events.test.ts +++ b/test/unit/events.test.ts @@ -364,7 +364,7 @@ describe("Events", function() { ]); }); - it("Invoking an EventQueue does not throw an error", function() { + it("Invoking an EventQueue works just like any other EventFunction", function() { const spam = on("SPAM", game => { game.output.write("Get spammed."); }); @@ -374,6 +374,11 @@ describe("Events", function() { const myGame = new GameInstance(); spam.then(spam)(myGame); + + expect(myGame.output.lines).to.deep.equal([ + { data: "Get spammed.", id: 1, type: OutputLineType.NORMAL }, + { data: "Get spammed.", id: 2, type: OutputLineType.NORMAL } + ]); }); describe("QTests", function() {