Skip to content

Commit

Permalink
Merge pull request #57 from regal/feat/48-optional-noop
Browse files Browse the repository at this point in the history
feat(event): Removes required return from EventFunctions
  • Loading branch information
jcowman2 committed Oct 31, 2018
2 parents 4cad6dd + ac75a7e commit faebc75
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 105 deletions.
3 changes: 1 addition & 2 deletions src/agents/agent-revert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Licensed under MIT License (see https://github.com/regal/regal)
*/

import { noop, on } from "../events";
import { on } from "../events";
import { InstanceAgents } from "./instance-agents";
import { StaticAgentRegistry } from "./static-agent-registry";

Expand Down Expand Up @@ -62,5 +62,4 @@ export const buildRevertFunction = (
}
}
}
return noop;
});
9 changes: 4 additions & 5 deletions src/events/event-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import GameInstance from "../game-instance";
* A function that modifies the game instance.
*
* @param game The game instance to be modified.
* @returns The next `EventFunction` to be executed.
* @returns The next `EventFunction` to be executed, or `void` if there are no more events.
*/
export type EventFunction = (game: GameInstance) => EventFunction;
export type EventFunction = (game: GameInstance) => EventFunction | void;

/**
* An `EventFunction` that is tracked by the game instance and can
Expand Down Expand Up @@ -78,11 +78,11 @@ export interface EventQueue extends TrackedEvent {

/** Ensures the object is a `TrackedEvent`. */
export const isTrackedEvent = (o: any): o is TrackedEvent =>
(o as TrackedEvent).target !== undefined;
o !== undefined && (o as TrackedEvent).target !== undefined;

/** Ensures the object is an `EventQueue`. */
export const isEventQueue = (o: any): o is EventQueue =>
(o as EventQueue).nq !== undefined;
o !== undefined && (o as EventQueue).nq !== undefined;

/** "No operation" - reserved `TrackedEvent` that signals no more events. */
export const noop: TrackedEvent = (() => {
Expand Down Expand Up @@ -233,7 +233,6 @@ export const on = (
// Make the TrackedEvent callable like a function.
const event = ((game: GameInstance) => {
game.events.invoke(event);
return noop;
}) as TrackedEvent;

event.eventName = eventName;
Expand Down
12 changes: 1 addition & 11 deletions test/demo.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { expect } from "chai";
import "mocha";

import {
Game,
GameInstance,
on,
Agent,
onStartCommand,
noop,
onPlayerCommand
} from "../src";
import { Game, on, Agent, onStartCommand, onPlayerCommand, noop } from "../src";
import { log, getDemoMetadata } from "./test-utils";
import { MetadataManager } from "../src/config";

Expand All @@ -25,12 +17,10 @@ it("Demo Runthrough Test 1", function() {
const consultOracle = on("CONSULT", game => {
const oracle = game.using(_oracle);
game.output.write(`${oracle.name} says: "Bow down before me!"`);
return noop;
});

const die = on("DIE", game => {
game.output.write(`${game.using(_oracle)} dies!`);
return noop;
});

const attack = (name: string) =>
Expand Down
30 changes: 1 addition & 29 deletions test/unit/agents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
DEFAULT_EVENT_ID,
DEFAULT_EVENT_NAME
} from "../../src/events/event-record";
import { on, noop } from "../../src/events";
import { on } from "../../src/events";

class Dummy extends Agent {
constructor(public name: string, public health: number) {
Expand Down Expand Up @@ -488,8 +488,6 @@ describe("Agents", function() {

parent.child = child;
game.state.parent = parent;

return noop;
});

const PARENT = new Parent(new Dummy("D0", 0));
Expand Down Expand Up @@ -530,7 +528,6 @@ describe("Agents", function() {
const myGame = new GameInstance();
on("MOD", game => {
game.state.arr = [];
return noop;
})(myGame);

expect(myGame.state.arr).to.deep.equal([]);
Expand All @@ -542,7 +539,6 @@ describe("Agents", function() {
const myGame = new GameInstance({ trackAgentChanges: true });
on("MOD", game => {
game.state.arr = [];
return noop;
})(myGame);

expect(myGame.agents).to.deep.equal({
Expand Down Expand Up @@ -605,7 +601,6 @@ describe("Agents", function() {
const myGame = new GameInstance();
on("MOD", game => {
game.state.arr = [1, false, "foo"];
return noop;
})(myGame);

expect(myGame.state.arr).to.deep.equal([1, false, "foo"]);
Expand All @@ -617,7 +612,6 @@ describe("Agents", function() {
const myGame = new GameInstance({ trackAgentChanges: true });
on("MOD", game => {
game.state.arr = [1, false, "foo"];
return noop;
})(myGame);

expect(myGame.agents).to.deep.equal({
Expand Down Expand Up @@ -733,7 +727,6 @@ describe("Agents", function() {
});
on("MOD", game => {
game.state.arr = [d1, d2, d3];
return noop;
})(myGame);

expect(myGame.state.arr).to.deep.equal([d1, d2, d3]);
Expand All @@ -750,7 +743,6 @@ describe("Agents", function() {
});
on("MOD", game => {
game.state.arr = [d1, d2, d3];
return noop;
})(myGame);

expect(myGame.agents).to.deep.equal({
Expand Down Expand Up @@ -927,7 +919,6 @@ describe("Agents", function() {
const myGame = new GameInstance();
on("MOD", game => {
game.state.arr = [new Dummy("D1", 10), new Dummy("D2", 15)];
return noop;
})(myGame);

const d1 = myGame.state.arr[0] as Dummy;
Expand All @@ -951,7 +942,6 @@ describe("Agents", function() {
const myGame = new GameInstance();
on("MOD", game => {
game.state.dummy = d2;
return noop;
})(myGame);

expect(myGame.state.dummy.dummies[0]).to.deep.equal(
Expand All @@ -974,7 +964,6 @@ describe("Agents", function() {
const myGame = new GameInstance();
on("MOD", game => {
game.state.dummies = [d1, d2];
return noop;
})(myGame);

expect(myGame.state.dummies).to.deep.equal([
Expand Down Expand Up @@ -1066,8 +1055,6 @@ describe("Agents", function() {
delete arr[0].name;
(arr[0] as any).foo = true;
game.state.arr = arr;

return noop;
})(myGame);

expect(myGame.state.arr).to.deep.equal([
Expand Down Expand Up @@ -1571,7 +1558,6 @@ describe("Agents", function() {
const myEvent = on("FOO", game => {
game.state.list = [];
game.state.list.push(new Dummy("D1", 10));
return noop;
});

Game.init();
Expand Down Expand Up @@ -1680,7 +1666,6 @@ describe("Agents", function() {
const addHealth = (dummy: Dummy) =>
on("ADD HEALTH", game => {
dummy.health += 15;
return noop;
});

addHealth(d)(myGame);
Expand Down Expand Up @@ -1727,7 +1712,6 @@ describe("Agents", function() {
const addHealth = (dummy: Dummy) =>
on("ADD HEALTH", game => {
dummy.health += 15;
return noop;
});

addHealth(d)(myGame);
Expand Down Expand Up @@ -1863,7 +1847,6 @@ describe("Agents", function() {
return on("MOD", game => {
game.state.dummy.name = "Jimmy";
game.state.foo = true;
return noop;
});
})(myGame);

Expand Down Expand Up @@ -1991,7 +1974,6 @@ describe("Agents", function() {
return on("MOD", game => {
game.state.dummy.name = "Jimmy";
game.state.foo = true;
return noop;
});
})(myGame);

Expand Down Expand Up @@ -2114,7 +2096,6 @@ describe("Agents", function() {
return on("MOD", game => {
game.state.dummy.name = "Jimmy";
game.state.foo = true;
return noop;
});
})(myGame);

Expand Down Expand Up @@ -2174,7 +2155,6 @@ describe("Agents", function() {
return on("MOD", game => {
delete game.state.dummy.name;
delete game.state.foo;
return noop;
});
})(myGame);

Expand Down Expand Up @@ -2226,7 +2206,6 @@ describe("Agents", function() {

return on("MOD", game => {
delete game.state.dummy.name;
return noop;
});
})(myGame);

Expand Down Expand Up @@ -2681,14 +2660,12 @@ describe("Agents", function() {
it("Reverting the effects of many events back to the first one", function() {
const init = on("INIT", game => {
game.state.foo = "Hello, world!";
return noop;
});

const mod = (num: number) =>
on(`MOD ${num}`, game => {
game.state.foo += `-${num}`;
game.state[num] = `Yo ${num}`;
return noop;
});

Game.init();
Expand Down Expand Up @@ -2719,14 +2696,12 @@ describe("Agents", function() {
it("Reverting the effects of many events over multiple steps", function() {
const init = on("INIT", game => {
game.state.foo = "Hello, world!";
return noop;
});

const mod = (num: number) =>
on(`MOD ${num}`, game => {
game.state.foo += `-${num}`;
game.state[num] = `Yo ${num}`;
return noop;
});

Game.init();
Expand Down Expand Up @@ -2778,7 +2753,6 @@ describe("Agents", function() {
const init = on("INIT", game => {
game.state.foo = "Hello, world!";
game.state.dummy = new Dummy("Lars", 10);
return noop;
});

Game.init();
Expand All @@ -2803,7 +2777,6 @@ describe("Agents", function() {
const dummy = game.using(staticDummy);
dummy.name = "Jimbo";
dummy["bippity"] = "boppity";
return noop;
})(myGame);

expect(myGame.agents.getAgentProperty(1, "name")).to.equal("Jimbo");
Expand Down Expand Up @@ -2832,7 +2805,6 @@ describe("Agents", function() {
on("REVERSE", game => {
const agents = myGame.state.agents as Dummy[];
agents.reverse();
return noop;
})(myGame);

expect(myGame.state.agents.length).to.equal(6);
Expand Down
Loading

0 comments on commit faebc75

Please sign in to comment.