Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ workflows:
requires:
- setup
- publish:
context: 6rs-public-npm
requires:
- test
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {LatchedSelector} from './nodes/LatchedSelector';
export {LatchedSequence} from './nodes/LatchedSequence';
export {IfElse} from './nodes/IfElse';
export {resultCodes, ResultCode} from './utils/resultCodes';
export {TreePublisher} from './utils/TreePublisher';

import * as decorators from './nodes/decorators';

Expand Down
15 changes: 14 additions & 1 deletion lib/nodes/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
import {BlueshellState} from './BlueshellState';
import {resultCodes as rc, ResultCode} from '../utils/resultCodes';
import {TreePublisher, TreeNonPublisher} from '../utils/TreePublisher';


/**
* Base class of all Nodes.
Expand All @@ -11,6 +13,17 @@ import {resultCodes as rc, ResultCode} from '../utils/resultCodes';
export class Base<S extends BlueshellState, E> {
private _parent: string;

// Hard to properly type this since the static can't
// inherit the types from this generic class. This static is
// here because it's difficult to inject this functionality
// into blueshell in the current form, but this is maybe
// marginally better than a global
static treePublisher: TreePublisher<any, any> = new TreeNonPublisher();

public static registerTreePublisher<S extends BlueshellState, E>(publisher: TreePublisher<S, E>): void {
Base.treePublisher = publisher;
}

/**
* @constructor
* @param name The name of the Node. If no name is given, the name of the Class will be used.
Expand Down Expand Up @@ -38,8 +51,8 @@ export class Base<S extends BlueshellState, E> {
}

try {
Base.treePublisher.publishResult(state, event, false);
const result = this.onEvent(state, event);

return this._afterEvent(result, state, event);
} catch (err) {
state.errorReason = err;
Expand Down
13 changes: 13 additions & 0 deletions lib/utils/TreePublisher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {BlueshellState} from '../nodes/BlueshellState';

export interface TreePublisher<B extends BlueshellState, V> {
publishResult(state: B, event: V, topLevel: boolean): void;
configure(options: object): void;
}

export class TreeNonPublisher implements TreePublisher<any, any> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
publishResult(_state: BlueshellState, _event: any, _topLevel: boolean) {}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
configure(_options: object) {}
}
26 changes: 26 additions & 0 deletions test/nodes/Base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {assert} from 'chai';
import * as Behavior from '../../lib';
import {BlueshellState} from '../../lib/nodes/BlueshellState';
import {resultCodes as rc} from '../../lib/utils/resultCodes';
import {TreeNonPublisher} from '../../lib/utils/TreePublisher';

const Base = Behavior.Action;
const Decorator = Behavior.Decorator;
Expand Down Expand Up @@ -118,4 +119,29 @@ describe('Base', function() {
assert.equal(child.getLastEventSeen(state), 2, 'last event seen should be updated');
});
});

describe('publisher', function() {
it('Has a non publisher by default', function() {
assert.isTrue(Base.treePublisher instanceof TreeNonPublisher);
});

it('We can make a publisher', function() {
const action = new TestAction();
let published = false;
const publisher = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
publishResult(_state: any, _event: any, _topLevel: boolean) {
published = true;
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
configure(_options: object) {},
} as Behavior.TreePublisher<any, any>;

Base.registerTreePublisher(publisher);
const res = action.handleEvent(new TestState(), 'testEvent');

assert.equal(res, rc.SUCCESS);
assert.isTrue(published);
});
});
});