Skip to content

Commit

Permalink
Merge pull request #58 from regal/fix/30-invoke-event-queue
Browse files Browse the repository at this point in the history
fix(event): EventQueues can be invoked like any other EventFunction
  • Loading branch information
jcowman2 committed Oct 31, 2018
2 parents faebc75 + 9bfbf83 commit 735c05b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
27 changes: 21 additions & 6 deletions src/events/event-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (() => {
Expand All @@ -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);
};

/**
Expand All @@ -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));

Expand Down
19 changes: 12 additions & 7 deletions test/unit/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,16 +364,21 @@ 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 works just like any other EventFunction", 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);

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() {
Expand Down

0 comments on commit 735c05b

Please sign in to comment.