Skip to content

Commit

Permalink
Fix: applied David’s ResultCode type safety to the synchronous implem…
Browse files Browse the repository at this point in the history
…entation

Also updated commitment rules
  • Loading branch information
jbcpollak committed Feb 19, 2019
1 parent b485bf5 commit 4a86a1d
Show file tree
Hide file tree
Showing 17 changed files with 936 additions and 92 deletions.
1 change: 1 addition & 0 deletions commitlint.config.js
@@ -0,0 +1 @@
module.exports = {extends: ['@6river/commitlint-config-6river']};
8 changes: 4 additions & 4 deletions lib/nodes/Base.ts
Expand Up @@ -2,7 +2,7 @@
* Created by josh on 1/10/16.
*/
import {BlueshellState} from './BlueshellState';
import {resultCodes as rc} from '../utils/resultCodes';
import {resultCodes as rc, ResultCode} from '../utils/resultCodes';

/**
* Base class of all Nodes.
Expand All @@ -29,7 +29,7 @@ export class Base<S extends BlueshellState, E> {
* @param event The event to handle.
* @protected
*/
handleEvent(state: S, event: E): string {
handleEvent(state: S, event: E): ResultCode {
this._beforeEvent(state, event);

const passed = this.precondition(state, event);
Expand Down Expand Up @@ -83,7 +83,7 @@ export class Base<S extends BlueshellState, E> {
* @param state
* @param event
*/
_afterEvent(res: string, state: S, event: E): string {
_afterEvent(res: ResultCode, state: S, event: E): ResultCode {
if (this.getDebug(state)) {
console.log(this.path, ' => ', event, ' => ', res); // eslint-disable-line no-console
}
Expand Down Expand Up @@ -113,7 +113,7 @@ export class Base<S extends BlueshellState, E> {
* @return Result. Must be rc.SUCCESS, rc.FAILURE, or rc.RUNNING
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onEvent(state: S, event: E): string {
onEvent(state: S, event: E): ResultCode {
return rc.SUCCESS;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/nodes/Composite.ts
@@ -1,5 +1,6 @@
import {Base} from './Base';
import {BlueshellState} from './BlueshellState';
import {ResultCode} from '../utils/resultCodes';

/**
* Base class for all Composite Nodes (nodes which have children).
Expand Down Expand Up @@ -48,7 +49,7 @@ export abstract class Composite<S extends BlueshellState, E> extends Base<S, E>
* @param state
* @param event
*/
onEvent(state: S, event: E): string {
onEvent(state: S, event: E): ResultCode {
const storage = this.getNodeStorage(state);

let firstChild = 0;
Expand All @@ -71,7 +72,7 @@ export abstract class Composite<S extends BlueshellState, E> extends Base<S, E>
* @param event
* @param i
*/
abstract handleChild(state: S, event: E, i: number): string;
abstract handleChild(state: S, event: E, i: number): ResultCode;

/**
* Resets Node Storage for this node and all children.
Expand Down
3 changes: 2 additions & 1 deletion lib/nodes/Decorator.ts
@@ -1,6 +1,7 @@
import {BlueshellState} from './BlueshellState';
import {Base} from './Base';
import {Composite} from './Composite';
import {ResultCode} from '../utils/resultCodes';

/**
* Base Class for all Decorator Nodes. Can only have one child.
Expand All @@ -27,7 +28,7 @@ export class Decorator<S extends BlueshellState, E> extends Composite<S, E> {
* @param state
* @param event
*/
handleChild(state: S, event: E): string {
handleChild(state: S, event: E): ResultCode {
// Passthrough
return this.child.handleEvent(state, event);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/nodes/Selector.ts
@@ -1,7 +1,7 @@
import {Composite} from './Composite';
import {BlueshellState} from './BlueshellState';

import {resultCodes as rc} from '../utils/resultCodes';
import {resultCodes as rc, ResultCode} from '../utils/resultCodes';

/**
* Selector Node (a.k.a. Fallback)
Expand All @@ -18,7 +18,7 @@ export class Selector<S extends BlueshellState, E> extends Composite<S, E> {
* @param event The event to handle.
* @param i The child index.
*/
handleChild(state: S, event: E, i: number): string {
handleChild(state: S, event: E, i: number): ResultCode {
const storage = this.getNodeStorage(state);

// If we finished all processing without success return failure.
Expand Down Expand Up @@ -50,7 +50,7 @@ export class Selector<S extends BlueshellState, E> extends Composite<S, E> {
* @param state
* @param event
*/
_afterChild(res: string, state: S, event: E) {
_afterChild(res: ResultCode, state: S, event: E) {
return {res, state, event};
}

Expand Down
6 changes: 3 additions & 3 deletions lib/nodes/Sequence.ts
@@ -1,6 +1,6 @@
import {Composite} from './Composite';
import {BlueshellState} from './BlueshellState';
import {resultCodes as rc} from '../utils/resultCodes';
import {resultCodes as rc, ResultCode} from '../utils/resultCodes';

/**
* Sends an event to each child until one of the returns `FAILURE`, or `RUNNING`, then returns that value.
Expand All @@ -16,7 +16,7 @@ export class Sequence<S extends BlueshellState, E> extends Composite<S, E> {
* @param event The event to handle.
* @param i The child index.
*/
handleChild(state: S, event: E, i: number): string {
handleChild(state: S, event: E, i: number): ResultCode {
const storage = this.getNodeStorage(state);

// If we finished all processing without failure return success.
Expand Down Expand Up @@ -48,7 +48,7 @@ export class Sequence<S extends BlueshellState, E> extends Composite<S, E> {
* @param state
* @param event
*/
_afterChild(res: string, state: S, event: E) {
_afterChild(res: ResultCode, state: S, event: E) {
return {res, state, event};
}

Expand Down
4 changes: 2 additions & 2 deletions lib/nodes/decorators/Not.ts
@@ -1,6 +1,6 @@
import {BlueshellState} from '../BlueshellState';

import {resultCodes as rc} from '../../utils/resultCodes';
import {resultCodes as rc, ResultCode} from '../../utils/resultCodes';
import {Decorator} from '../Decorator';

/**
Expand All @@ -17,7 +17,7 @@ export class Not<S extends BlueshellState, E> extends Decorator<S, E> {
* @param state The state when the event occured.
* @param event The event to handle.
*/
onEvent(state: S, event: E): string {
onEvent(state: S, event: E): ResultCode {
let res = this.child.handleEvent(state, event);

switch (res) {
Expand Down
3 changes: 2 additions & 1 deletion lib/nodes/decorators/RepeatWhen.ts
@@ -1,6 +1,7 @@
import {BlueshellState} from '../BlueshellState';
import {Base} from '../Base';
import {Decorator} from '../Decorator';
import {ResultCode} from '../../utils/resultCodes';

/**
* Given a state, event, and result code (from a child Node), return a boolean.
Expand Down Expand Up @@ -30,7 +31,7 @@ export class RepeatWhen<S extends BlueshellState, E> extends Decorator<S, E> {
* @param state The state when the event occured.
* @param event The event to handle.
*/
onEvent(state: S, event: E): string {
onEvent(state: S, event: E): ResultCode {
const res = this.child.handleEvent(state, event);

if (this.conditional(state, event, res)) {
Expand Down
5 changes: 3 additions & 2 deletions lib/nodes/decorators/ResultSwap.ts
@@ -1,6 +1,7 @@
import {BlueshellState} from '../BlueshellState';
import {Base} from '../Base';
import {Decorator} from '../Decorator';
import {ResultCode} from '../../utils/resultCodes';

/**
* Swaps one result from a child node for another.
Expand All @@ -19,8 +20,8 @@ export class ResultSwap<S extends BlueshellState, E> extends Decorator<S, E> {
* @param child The child Node of the decorator.
* @param desc Optional description of the Node.
*/
constructor(private _inResult: string,
private _outResult: string,
constructor(private _inResult: ResultCode,
private _outResult: ResultCode,
child: Base<S, E>,
desc = `ResultSwap_${_inResult}-${_outResult}-${child.name}`) {
super(desc, child);
Expand Down
4 changes: 3 additions & 1 deletion lib/utils/resultCodes.ts
Expand Up @@ -2,9 +2,11 @@
* 5/30/16
* @author Joshua Chaitin-Pollak
*/
export const resultCodes = {
export const resultCodes: {[K in ResultCode]: ResultCode} = {
SUCCESS: 'SUCCESS',
FAILURE: 'FAILURE',
RUNNING: 'RUNNING',
ERROR: 'ERROR',
};

export type ResultCode = 'SUCCESS' | 'FAILURE' | 'RUNNING' | 'ERROR';

0 comments on commit 4a86a1d

Please sign in to comment.