Skip to content

Commit

Permalink
fix(module): remove player-actor injection symbol, ensure actor servi…
Browse files Browse the repository at this point in the history
…ce only starts once
  • Loading branch information
ssube committed May 26, 2021
1 parent ecbc828 commit 8469297
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
16 changes: 11 additions & 5 deletions src/main.ts
Expand Up @@ -2,12 +2,12 @@ import { InvalidArgumentError, isNil } from '@apextoaster/js-utils';
import { BaseOptions, Container, Module } from 'noicejs';

import { BunyanLogger } from './logger/BunyanLogger';
import { INJECT_ACTOR_PLAYER, INJECT_LOADER, INJECT_LOCALE, INJECT_PARSER, INJECT_RENDER, INJECT_STATE } from './module';
import { ActorModule } from './module/ActorModule';
import { ActorType } from './model/entity/Actor';
import { INJECT_ACTOR, INJECT_LOADER, INJECT_LOCALE, INJECT_PARSER, INJECT_RENDER, INJECT_STATE } from './module';
import { ActorLocator, ActorModule } from './module/ActorModule';
import { BrowserModule } from './module/BrowserModule';
import { LocalModule } from './module/LocalModule';
import { NodeModule } from './module/NodeModule';
import { ActorService } from './service/actor';
import { Loader } from './service/loader';
import { LocaleService } from './service/locale';
import { Parser } from './service/parser';
Expand Down Expand Up @@ -57,8 +57,14 @@ export async function main(args: Array<string>): Promise<number> {

locale.addBundle('common', config.locale);

const player = await container.create<ActorService, BaseOptions>(INJECT_ACTOR_PLAYER);
await player.start();
// start player actor
// TODO: this does not belong here
const locator = await container.create<ActorLocator, BaseOptions>(INJECT_ACTOR);
const actor = await locator.get({
id: '', // does not matter, very smelly
type: ActorType.PLAYER,
});
await actor.start();

// load data files
const loader = await container.create<Loader, BaseOptions>(INJECT_LOADER);
Expand Down
5 changes: 3 additions & 2 deletions src/module/ActorModule.ts
@@ -1,7 +1,7 @@
import { doesExist, mustExist } from '@apextoaster/js-utils';
import { Module, ModuleOptions } from 'noicejs';

import { INJECT_ACTOR, INJECT_ACTOR_PLAYER, INJECT_TOKENIZER } from '.';
import { INJECT_ACTOR, INJECT_TOKENIZER } from '.';
import { ActorType } from '../model/entity/Actor';
import { ActorService } from '../service/actor';
import { BehaviorActorService } from '../service/actor/BehaviorActor';
Expand Down Expand Up @@ -29,6 +29,7 @@ export class ActorModule extends Module {
protected actors: Map<string, ActorService>;
protected locator: ActorLocator;
protected player: Singleton<ActorService>;
protected playerStarted: boolean;
protected tokenizer: Singleton<TokenizerService>;

constructor() {
Expand All @@ -41,13 +42,13 @@ export class ActorModule extends Module {
};
this.player = new Singleton(() => mustExist(this.container).create(PlayerActorService));
this.tokenizer = new Singleton(() => mustExist(this.container).create(WordTokenizer));
this.playerStarted = false;
}

public async configure(options: ModuleOptions): Promise<void> {
await super.configure(options);

this.bind(INJECT_ACTOR).toInstance(this.locator);
this.bind(INJECT_ACTOR_PLAYER).toFactory(() => this.player.get());
this.bind(INJECT_TOKENIZER).toFactory(() => this.tokenizer.get());
}

Expand Down
5 changes: 0 additions & 5 deletions src/module/index.ts
Expand Up @@ -16,8 +16,3 @@ export const INJECT_TOKENIZER = Symbol('inject-tokenizer');
* Get the input for a particular actor.
*/
export const INJECT_ACTOR = Symbol('inject-actor');

/**
* Get the input for the player actor (shortcut to input-actor for the focused actor).
*/
export const INJECT_ACTOR_PLAYER = Symbol('inject-actor-player');
8 changes: 8 additions & 0 deletions src/service/actor/PlayerActor.ts
Expand Up @@ -25,6 +25,7 @@ export class PlayerActorService implements ActorService {
protected event: EventBus;
protected locale: LocaleService;
protected logger: Logger;
protected started: boolean;
protected tokenizer: TokenizerService;

protected history: Array<Command>;
Expand All @@ -37,14 +38,21 @@ export class PlayerActorService implements ActorService {
this.logger = mustExist(options[INJECT_LOGGER]).child({
kind: constructorName(this),
});
this.started = false;
this.tokenizer = mustExist(options[INJECT_TOKENIZER]);
}

public async start() {
if (this.started) {
return;
}

this.event.on('render-output', (event) => this.onInput(event));
this.event.on('state-output', (event) => this.onOutput(event));

await this.tokenizer.translate(COMMON_VERBS);

this.started = true;
}

public async stop() {
Expand Down
18 changes: 12 additions & 6 deletions test/module/TestActorModule.ts
Expand Up @@ -2,11 +2,11 @@ import { expect } from 'chai';
import { BaseOptions, Container, NullLogger } from 'noicejs';

import { ActorType } from '../../src/model/entity/Actor';
import { INJECT_ACTOR_PLAYER } from '../../src/module';
import { ActorModule } from '../../src/module/ActorModule';
import { INJECT_ACTOR, INJECT_LOCALE } from '../../src/module';
import { ActorLocator, ActorModule } from '../../src/module/ActorModule';
import { LocalModule } from '../../src/module/LocalModule';
import { ActorService } from '../../src/service/actor';
import { PlayerActorService } from '../../src/service/actor/PlayerActor';
import { NextLocaleService } from '../../src/service/locale/NextLocale';

describe('actor module', () => {
it('should provide a player actor service for player actors', async () => {
Expand All @@ -15,7 +15,11 @@ describe('actor module', () => {
logger: NullLogger.global,
});

const actor = await container.create<ActorService, BaseOptions>(INJECT_ACTOR_PLAYER, {
const locale = await container.create<NextLocaleService, BaseOptions>(INJECT_LOCALE);
await locale.start();

const locator = await container.create<ActorLocator, BaseOptions>(INJECT_ACTOR);
const actor = await locator.get({
id: 'foo',
type: ActorType.PLAYER,
});
Expand All @@ -29,12 +33,14 @@ describe('actor module', () => {
logger: NullLogger.global,
});

const actor = await container.create(INJECT_ACTOR_PLAYER, {
const locator = await container.create<ActorLocator, BaseOptions>(INJECT_ACTOR);

const actor = await locator.get({
id: 'foo',
type: ActorType.DEFAULT,
});

const next = await container.create(INJECT_ACTOR_PLAYER, {
const next = await locator.get({
id: 'foo',
type: ActorType.DEFAULT,
});
Expand Down
12 changes: 8 additions & 4 deletions test/service/actor/TestPlayerActor.ts
@@ -1,10 +1,10 @@
import { expect } from 'chai';
import { BaseOptions, Container, NullLogger } from 'noicejs';

import { INJECT_ACTOR_PLAYER, INJECT_EVENT, INJECT_LOCALE } from '../../../src/module';
import { ActorModule } from '../../../src/module/ActorModule';
import { ActorType } from '../../../src/model/entity/Actor';
import { INJECT_ACTOR, INJECT_EVENT, INJECT_LOCALE } from '../../../src/module';
import { ActorLocator, ActorModule } from '../../../src/module/ActorModule';
import { LocalModule } from '../../../src/module/LocalModule';
import { PlayerActorService } from '../../../src/service/actor/PlayerActor';
import { CommandEvent, EventBus } from '../../../src/service/event';
import { LocaleService } from '../../../src/service/locale';
import { onceWithRemove } from '../../../src/util/event';
Expand All @@ -21,7 +21,11 @@ describe('player actor', () => {
const locale = await container.create<LocaleService, BaseOptions>(INJECT_LOCALE);
await locale.start();

const actor = await container.create<PlayerActorService, BaseOptions>(INJECT_ACTOR_PLAYER);
const locator = await container.create<ActorLocator, BaseOptions>(INJECT_ACTOR);
const actor = await locator.get({
id: 'foo',
type: ActorType.PLAYER,
});
await actor.start();

const index = 13;
Expand Down

0 comments on commit 8469297

Please sign in to comment.