Skip to content

Commit

Permalink
refactor(keys): Remove FK and PK.ref()
Browse files Browse the repository at this point in the history
  • Loading branch information
jcowman2 committed Sep 14, 2019
1 parent 7257f80 commit 7d1f3e0
Show file tree
Hide file tree
Showing 17 changed files with 37 additions and 68 deletions.
3 changes: 1 addition & 2 deletions src/agents/agent-array-reference.ts
Expand Up @@ -5,7 +5,6 @@
* Licensed under MIT License (see https://github.com/regal/regal)
*/

import { FK } from "../common";
import { AgentId } from "./agent-meta";

/** Whether the given object is an `AgentArrayReference`. */
Expand All @@ -20,5 +19,5 @@ export class AgentArrayReference {
* Constructs a new `AgentReference` in place of an active agent array.
* @param arRefId The agent array's numeric id.
*/
constructor(public arRefId: FK<AgentId>) {}
constructor(public arRefId: AgentId) {}
}
5 changes: 2 additions & 3 deletions src/agents/agent-manager.ts
Expand Up @@ -5,7 +5,6 @@
* Licensed under MIT License (see https://github.com/regal/regal)
*/

import { FK } from "../common";
import { GameInstance } from "../state";
import { AgentId, AgentMeta, AgentProtoId } from "./agent-meta";
import { PropertyChange } from "./agent-properties";
Expand All @@ -19,7 +18,7 @@ import { PropertyChange } from "./agent-properties";
*/
export interface AgentManager {
/** The managed agent's id. */
readonly id: FK<AgentId>;
readonly id: AgentId;

/** The managed agent's metadata. */
meta: AgentMeta;
Expand Down Expand Up @@ -70,7 +69,7 @@ export interface AgentManager {
* Sets the agent manager's `AgentProtoId`
* @param protoId The prototype id
*/
setProtoId(protoId: FK<AgentProtoId>): void;
setProtoId(protoId: AgentProtoId): void;
}

/** Determines whether an object is an `AgentManager`. */
Expand Down
5 changes: 2 additions & 3 deletions src/agents/agent-properties.ts
Expand Up @@ -5,7 +5,6 @@
* Licensed under MIT License (see https://github.com/regal/regal)
*/

import { FK } from "../common";
import { AgentId } from "./agent-meta";

/** Type of modification done to an agent's property. */
Expand All @@ -25,13 +24,13 @@ export enum PropertyOperation {
*/
export interface PropertyChange {
/** The id of the `TrackedEvent` during which the change took place (optional). */
eventId?: FK<AgentId>;
eventId?: AgentId;

/** The name of the `TrackedEvent` during which the change took place (optional). */
eventName?: string;

/** The id of the registered `Agent` (optional). */
agentId?: FK<AgentId>;
agentId?: AgentId;

/** The operation performed on the agent's property. */
op: PropertyOperation;
Expand Down
5 changes: 2 additions & 3 deletions src/agents/agent-reference.ts
Expand Up @@ -5,7 +5,6 @@
* Licensed under MIT License (see https://github.com/regal/regal)
*/

import { FK } from "../common";
import { AgentId } from "./agent-meta";

/** Whether the given object is an `AgentReference`. */
Expand All @@ -18,7 +17,7 @@ export const isAgentReference = (o: any): o is AgentReference =>
export class AgentReference {
/**
* Constructs a new `AgentReference` in place of an active agent.
* @param refId The mocked agent's numeric id.
* @param refId The mocked agent's id.
*/
constructor(public refId: FK<AgentId>) {}
constructor(public refId: AgentId) {}
}
17 changes: 8 additions & 9 deletions src/agents/impl/agent-manager-impl.ts
Expand Up @@ -5,7 +5,6 @@
* Licensed under MIT License (see https://github.com/regal/regal)
*/

import { FK } from "../../common";
import { RegalError } from "../../error";
import { EventRecord, getUntrackedEventPK } from "../../events";
import { GameInstanceInternal } from "../../state";
Expand All @@ -29,17 +28,17 @@ import {
export const buildAgentManager = (
id: AgentId,
game: GameInstanceInternal
): AgentManager => new AgentManagerImpl(id.ref(), game);
): AgentManager => new AgentManagerImpl(id, game);

/** Implementation of `AgentManager`. */
class AgentManagerImpl implements AgentManager {
public meta: AgentMeta;

constructor(id: FK<AgentId>, public game: GameInstanceInternal) {
constructor(id: AgentId, public game: GameInstanceInternal) {
this.meta = agentMetaWithID(id)(defaultAgentMeta());
}

public get id(): FK<AgentId> {
public get id(): AgentId {
return this.meta.id;
}

Expand Down Expand Up @@ -116,8 +115,8 @@ class AgentManagerImpl implements AgentManager {
const event = this.game.events.current;

const propChange: PropertyChange = {
agentId: this.id.ref(),
eventId: event.id.ref(),
agentId: this.id,
eventId: event.id,
eventName: event.name,
final: value,
init: initValue,
Expand Down Expand Up @@ -154,8 +153,8 @@ class AgentManagerImpl implements AgentManager {
const event = this.game.events.current;

const propChange: PropertyChange = {
agentId: this.id.ref(),
eventId: event.id.ref(),
agentId: this.id,
eventId: event.id,
eventName: event.name,
final: undefined,
init: initValue,
Expand All @@ -166,7 +165,7 @@ class AgentManagerImpl implements AgentManager {
this.recordChange(event, propChange, history);
}

public setProtoId(protoId: FK<AgentProtoId>): void {
public setProtoId(protoId: AgentProtoId): void {
this.meta = agentMetaWithProtoID(protoId)(this.meta);
}

Expand Down
6 changes: 1 addition & 5 deletions src/common/impl/pk-impl.ts
Expand Up @@ -5,7 +5,7 @@
* Licensed under MIT License (see https://github.com/regal/regal)
*/

import { FK, PK } from "../keys";
import { PK } from "../keys";

/**
* Current implementation of `PK`. Uses a numeric key.
Expand All @@ -25,10 +25,6 @@ export class NumericPKImpl<T> implements PK<T> {
return this === key || this.value() === key.value();
}

public ref(): FK<PK<T>> {
return this;
}

public value(): string {
return String(this.internalValue);
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/impl/pk-provider-impl.ts
Expand Up @@ -6,7 +6,7 @@
*/

import { RegalError } from "../../error";
import { FK, PK, PKProvider, ReservedPKSet } from "../keys";
import { PK, PKProvider, ReservedPKSet } from "../keys";

export class PKProviderImpl<PKClass> implements PKProvider<PKClass> {
public static readonly START_VALUE = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/common/index.ts
Expand Up @@ -5,6 +5,6 @@
* Licensed under MIT License (see https://github.com/regal/regal)
*/

export { PK, FK, ReservedPKSet, PKProvider } from "./keys";
export { PK, ReservedPKSet, PKProvider } from "./keys";
export { buildPKProvider } from "./impl";
export { Mutable } from "./type-utils";
11 changes: 1 addition & 10 deletions src/common/keys.ts
Expand Up @@ -29,9 +29,6 @@ export interface PK<T> {
/** Whether this key is equivalent to the given one. */
equals(key: PK<T>): boolean;

/** Generates a foreign key that references this key. */
ref(): FK<PK<T>>;

/**
* Generates a string value representative of this key.
* This is used for the `equals` method, which is strongly preferred
Expand Down Expand Up @@ -123,12 +120,6 @@ export interface PKProvider<T> {
/** Returns the key that was last generated. */
previous(): PK<T>;

/** Returns the key that is associated with the given value. */
keyFromValue(value: string): PK<T>;
}

/**
* A foreign key, which is a reference to another object's primary key.
*
* @template T The class referenced by this foreign key type.
*/
export interface FK<T extends PK<any>> extends PK<T> {}
8 changes: 4 additions & 4 deletions src/events/event-record.ts
Expand Up @@ -6,7 +6,7 @@
*/

import { PropertyChange } from "../agents";
import { FK, PK } from "../common";
import { PK } from "../common";
import { OutputLine, OutputLineId } from "../output";
import { RandomRecord } from "../random";
import { TrackedEvent } from "./event-types";
Expand All @@ -28,13 +28,13 @@ export interface EventRecord {
func: TrackedEvent;

/** The IDs of the `OutputLine`s emitted by the event. */
output?: Array<FK<OutputLineId>>;
output?: OutputLineId[];

/** The ID of the event that caused this event. */
causedBy?: FK<EventId>;
causedBy?: EventId;

/** The IDs of the events that were caused by this event. */
caused?: Array<FK<EventId>>;
caused?: EventId[];

/** The records of all changes to registered agents that were caused by this event. */
changes?: PropertyChange[];
Expand Down
13 changes: 6 additions & 7 deletions src/events/impl/event-record-impl.ts
Expand Up @@ -6,7 +6,6 @@
*/

import { PropertyChange } from "../../agents";
import { FK } from "../../common";
import { OutputLine, OutputLineId } from "../../output";
import { RandomRecord } from "../../random";
import { EventId, EventRecord } from "../event-record";
Expand Down Expand Up @@ -36,9 +35,9 @@ export const buildEventRecord = (
};

class EventRecordImpl implements EventRecord {
public output?: Array<FK<OutputLineId>>;
public causedBy?: FK<EventId>;
public caused?: Array<FK<EventId>>;
public output?: OutputLineId[];
public causedBy?: EventId;
public caused?: EventId[];
public changes?: PropertyChange[];
public randoms?: RandomRecord[];

Expand All @@ -52,15 +51,15 @@ class EventRecordImpl implements EventRecord {
if (this.output === undefined) {
this.output = [];
}
this.output.push(line.id.ref());
this.output.push(line.id);
}

public trackCausedEvent(...events: EventRecord[]): void {
if (this.caused === undefined) {
this.caused = [];
}
this.caused.push(...events.map(e => e.id.ref()));
events.forEach(e => (e.causedBy = this.id.ref()));
this.caused.push(...events.map(e => e.id));
events.forEach(e => (e.causedBy = this.id));
}

public trackChange(propChange: PropertyChange): void {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -32,4 +32,4 @@ export { GameOptions, GameMetadata, InstanceOptions } from "./config";
export { GameInstance } from "./state";
export { OutputLineType, OutputLine, InstanceOutput } from "./output";
export { InstanceRandom, Charsets } from "./random";
export { FK, PK } from "./common";
export { PK } from "./common";
3 changes: 1 addition & 2 deletions src/random/instance-random-internal.ts
Expand Up @@ -5,7 +5,6 @@
* Licensed under MIT License (see https://github.com/regal/regal)
*/

import { FK } from "../common";
import { GameInstanceInternal } from "../state";
import { InstanceRandom } from "./instance-random";
import { RandomRecordId } from "./random-record";
Expand All @@ -29,7 +28,7 @@ export interface InstanceRandomInternal extends InstanceRandom {
* The key of the last `RandomRecord` that was generated, or the default key if
* none have been.
*/
readonly lastKey: FK<RandomRecordId>;
readonly lastKey: RandomRecordId;

/**
* Generates a new `InstanceRandomInternal` for the new `GameInstance`, preserving
Expand Down
3 changes: 1 addition & 2 deletions src/state/game-instance-internal.ts
Expand Up @@ -6,7 +6,6 @@
*/

import { InstanceAgentsInternal } from "../agents";
import { FK } from "../common";
import { GameOptions, InstanceOptionsInternal } from "../config";
import { EventId, InstanceEventsInternal } from "../events";
import { InstanceOutputInternal } from "../output";
Expand Down Expand Up @@ -54,5 +53,5 @@ export interface GameInstanceInternal<StateType = any>
* Defaults to `EVENT_RESERVED_KEYS.UNTRACKED`. If an argument is given and the
* `trackAgentChanges` option is disabled, an error will throw.
*/
revert(revertTo?: FK<EventId>): GameInstanceInternal<StateType>;
revert(revertTo?: EventId): GameInstanceInternal<StateType>;
}
10 changes: 4 additions & 6 deletions src/state/impl/game-instance-impl.ts
Expand Up @@ -17,7 +17,7 @@ import {
import { isAgentArrayReference } from "../../agents/agent-array-reference";
import { ReservedAgentProperty } from "../../agents/agent-meta";
import { isAgentReference } from "../../agents/agent-reference";
import { buildPKProvider, FK } from "../../common";
import { buildPKProvider } from "../../common";
import {
buildInstanceOptions,
GameOptions,
Expand Down Expand Up @@ -143,9 +143,7 @@ class GameInstanceImpl<StateType = any>
return returnObj;
}

public revert(
revertTo: FK<EventId> = getUntrackedEventPK()
): GameInstanceImpl {
public revert(revertTo: EventId = getUntrackedEventPK()): GameInstanceImpl {
if (!revertTo.equals(getUntrackedEventPK())) {
if (revertTo.index() < getUntrackedEventPK().index()) {
throw new RegalError(
Expand Down Expand Up @@ -194,7 +192,7 @@ class GameInstanceImpl<StateType = any>
* Internal helper that builds an `InstanceRandom` constructor with its `numGenerations`
* set to the appropriate revert event.
*/
private _buildRandomRevertCtor(revertTo: FK<EventId>) {
private _buildRandomRevertCtor(revertTo: EventId) {
return (game: GameInstanceImpl) => {
let lastKey = this.random.lastKey;

Expand Down Expand Up @@ -228,7 +226,7 @@ class GameInstanceImpl<StateType = any>
}

/** Internal helper that builds a `TrackedEvent` to revert agent changes */
private _buildAgentRevertFunc(revertTo: FK<EventId>): TrackedEvent {
private _buildAgentRevertFunc(revertTo: EventId): TrackedEvent {
return on("REVERT", (game: GameInstanceInternal) => {
const target = game.agents;

Expand Down
2 changes: 1 addition & 1 deletion test/tsconfig.json
Expand Up @@ -3,5 +3,5 @@
"extends": "../tsconfig.json",
"include": [
"./**/*.ts"
],
]
}
8 changes: 0 additions & 8 deletions test/unit/keys.test.ts
Expand Up @@ -121,14 +121,6 @@ describe("Keys", function() {
);
});

it("PK.ref() creates an equivalent key", function() {
const prov = buildPKProvider(RESERVED_KEYS);
const pk = prov.next();
const fk = pk.ref();
expect(pk.equals(fk));
expect(pk.plus(1).equals(fk.plus(1))).to.be.true;
});

it("A forked PK provider generates keys just like the original would", function() {
const original = buildPKProvider(RESERVED_KEYS);
const pkFirst = original.next();
Expand Down

0 comments on commit 7d1f3e0

Please sign in to comment.