From c069ef65f5df96bcb966b15f4d234754edc84b7d Mon Sep 17 00:00:00 2001 From: Mark Asdoorian Date: Fri, 20 Sep 2019 00:43:57 -0400 Subject: [PATCH 1/9] fix: added support to publish the tree A TreePublisher can be registered which will be called for each handleEvent() call in Base. --- lib/index.ts | 1 + lib/nodes/Base.ts | 4 +++- lib/utils/TreePublisher.ts | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 lib/utils/TreePublisher.ts diff --git a/lib/index.ts b/lib/index.ts index 51b3d18..f7d1d7a 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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, registerTreePublisher} from './utils/TreePublisher'; import * as decorators from './nodes/decorators'; diff --git a/lib/nodes/Base.ts b/lib/nodes/Base.ts index 99bcd2a..5e84c94 100644 --- a/lib/nodes/Base.ts +++ b/lib/nodes/Base.ts @@ -3,6 +3,7 @@ */ import {BlueshellState} from './BlueshellState'; import {resultCodes as rc, ResultCode} from '../utils/resultCodes'; +import {publishTree} from '../utils/TreePublisher'; /** * Base class of all Nodes. @@ -38,8 +39,9 @@ export class Base { } try { - const result = this.onEvent(state, event); + publishTree(); + const result = this.onEvent(state, event); return this._afterEvent(result, state, event); } catch (err) { state.errorReason = err; diff --git a/lib/utils/TreePublisher.ts b/lib/utils/TreePublisher.ts new file mode 100644 index 0000000..1e5491d --- /dev/null +++ b/lib/utils/TreePublisher.ts @@ -0,0 +1,16 @@ +// @@@ probably a better way to do this than a global +let treePublisher: TreePublisher; + +export interface TreePublisher { + publishTree(): void; +} + +export function registerTreePublisher(publisher: TreePublisher): void { + treePublisher = publisher; +} + +export function publishTree() { + if (!!treePublisher) { + treePublisher.publishTree(); + } +} From 3e8ea67b290492e75a9c93a2b4c13146dc5af043 Mon Sep 17 00:00:00 2001 From: Joseph Hughes Date: Thu, 31 Oct 2019 20:21:17 -0400 Subject: [PATCH 2/9] fix: add the context with correct token --- .circleci/config.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index df4b963..b38c134 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -73,5 +73,6 @@ workflows: requires: - setup - publish: + context: 6rs-public-npm requires: - test From 556b191f2e2819d8ce424fd06a71569c5527e7f9 Mon Sep 17 00:00:00 2001 From: Jeff Kreis Date: Tue, 5 Nov 2019 08:29:25 -0500 Subject: [PATCH 3/9] feat: enhance publish and add configuration --- lib/nodes/Base.ts | 2 +- lib/utils/TreePublisher.ts | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/nodes/Base.ts b/lib/nodes/Base.ts index 5e84c94..2c177dd 100644 --- a/lib/nodes/Base.ts +++ b/lib/nodes/Base.ts @@ -39,7 +39,7 @@ export class Base { } try { - publishTree(); + publishTree(state, event); const result = this.onEvent(state, event); return this._afterEvent(result, state, event); diff --git a/lib/utils/TreePublisher.ts b/lib/utils/TreePublisher.ts index 1e5491d..0d1ac6f 100644 --- a/lib/utils/TreePublisher.ts +++ b/lib/utils/TreePublisher.ts @@ -1,16 +1,19 @@ +import {BlueshellState} from "../nodes/BlueshellState"; + // @@@ probably a better way to do this than a global -let treePublisher: TreePublisher; +let treePublisher: TreePublisher; -export interface TreePublisher { - publishTree(): void; +export interface TreePublisher { + publishTree(state: S, event: E): void; + configure(options: object): void; } -export function registerTreePublisher(publisher: TreePublisher): void { +export function registerTreePublisher(publisher: TreePublisher): void { treePublisher = publisher; } -export function publishTree() { +export function publishTree(state: S, event: E) { if (!!treePublisher) { - treePublisher.publishTree(); + treePublisher.publishTree(state, event); } } From f3ce5b2963027eaec925ad582c24452f2b74f6b7 Mon Sep 17 00:00:00 2001 From: Jeff Kreis Date: Sat, 9 Nov 2019 10:00:51 -0500 Subject: [PATCH 4/9] fix: renaming publish --- lib/nodes/Base.ts | 6 +++--- lib/utils/TreePublisher.ts | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/nodes/Base.ts b/lib/nodes/Base.ts index 2c177dd..e36e636 100644 --- a/lib/nodes/Base.ts +++ b/lib/nodes/Base.ts @@ -3,7 +3,8 @@ */ import {BlueshellState} from './BlueshellState'; import {resultCodes as rc, ResultCode} from '../utils/resultCodes'; -import {publishTree} from '../utils/TreePublisher'; +import {maybePublishTree} from '../utils/TreePublisher'; + /** * Base class of all Nodes. @@ -39,8 +40,7 @@ export class Base { } try { - publishTree(state, event); - + maybePublishTree(state, event, false); const result = this.onEvent(state, event); return this._afterEvent(result, state, event); } catch (err) { diff --git a/lib/utils/TreePublisher.ts b/lib/utils/TreePublisher.ts index 0d1ac6f..e19da12 100644 --- a/lib/utils/TreePublisher.ts +++ b/lib/utils/TreePublisher.ts @@ -1,10 +1,10 @@ -import {BlueshellState} from "../nodes/BlueshellState"; +import {BlueshellState} from '../nodes/BlueshellState'; // @@@ probably a better way to do this than a global let treePublisher: TreePublisher; export interface TreePublisher { - publishTree(state: S, event: E): void; + maybePublishTree(state: S, event: E, topLevel: boolean): void; configure(options: object): void; } @@ -12,8 +12,9 @@ export function registerTreePublisher(publisher: Tr treePublisher = publisher; } -export function publishTree(state: S, event: E) { +// toplevel is handleEvent cadence of publishing +export function maybePublishTree(state: S, event: E, topLevel: boolean) { if (!!treePublisher) { - treePublisher.publishTree(state, event); + treePublisher.maybePublishTree(state, event, topLevel); } } From 83da499225be3ac68b62f02ebe58f8f1df71d68c Mon Sep 17 00:00:00 2001 From: Jeff Kreis Date: Fri, 15 Nov 2019 15:51:05 -0500 Subject: [PATCH 5/9] fix: make it static --- lib/index.ts | 2 +- lib/nodes/Base.ts | 15 +++++++++++++-- lib/utils/TreePublisher.ts | 21 +++++++-------------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index f7d1d7a..3ae0b70 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -10,7 +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, registerTreePublisher} from './utils/TreePublisher'; +export {TreePublisher} from './utils/TreePublisher'; import * as decorators from './nodes/decorators'; diff --git a/lib/nodes/Base.ts b/lib/nodes/Base.ts index e36e636..a132779 100644 --- a/lib/nodes/Base.ts +++ b/lib/nodes/Base.ts @@ -3,7 +3,7 @@ */ import {BlueshellState} from './BlueshellState'; import {resultCodes as rc, ResultCode} from '../utils/resultCodes'; -import {maybePublishTree} from '../utils/TreePublisher'; +import {TreePublisher, TreeNonPublisher} from '../utils/TreePublisher'; /** @@ -13,6 +13,17 @@ import {maybePublishTree} from '../utils/TreePublisher'; export class Base { 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: TreeNonPublisher | TreePublisher = new TreeNonPublisher(); + + static registerTreePublisher(publisher: TreePublisher): 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. @@ -40,7 +51,7 @@ export class Base { } try { - maybePublishTree(state, event, false); + Base.treePublisher.maybePublishTree(state, event, false); const result = this.onEvent(state, event); return this._afterEvent(result, state, event); } catch (err) { diff --git a/lib/utils/TreePublisher.ts b/lib/utils/TreePublisher.ts index e19da12..4227ae3 100644 --- a/lib/utils/TreePublisher.ts +++ b/lib/utils/TreePublisher.ts @@ -1,20 +1,13 @@ import {BlueshellState} from '../nodes/BlueshellState'; -// @@@ probably a better way to do this than a global -let treePublisher: TreePublisher; - -export interface TreePublisher { - maybePublishTree(state: S, event: E, topLevel: boolean): void; +export interface TreePublisher { + maybePublishTree(state: B, event: V, topLevel: boolean): void; configure(options: object): void; } -export function registerTreePublisher(publisher: TreePublisher): void { - treePublisher = publisher; -} - -// toplevel is handleEvent cadence of publishing -export function maybePublishTree(state: S, event: E, topLevel: boolean) { - if (!!treePublisher) { - treePublisher.maybePublishTree(state, event, topLevel); - } +export class TreeNonPublisher implements TreePublisher { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + maybePublishTree(_state: BlueshellState, _event: any, _topLevel: boolean) {} + // eslint-disable-next-line @typescript-eslint/no-unused-vars + configure(_options: object) {} } From c8c2bea1a0897ccf5a860aaca25aa5aa5f151b11 Mon Sep 17 00:00:00 2001 From: Jeff Kreis Date: Fri, 15 Nov 2019 16:04:29 -0500 Subject: [PATCH 6/9] fix: public --- lib/nodes/Base.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nodes/Base.ts b/lib/nodes/Base.ts index a132779..dfa10f0 100644 --- a/lib/nodes/Base.ts +++ b/lib/nodes/Base.ts @@ -20,7 +20,7 @@ export class Base { // marginally better than a global static treePublisher: TreeNonPublisher | TreePublisher = new TreeNonPublisher(); - static registerTreePublisher(publisher: TreePublisher): void { + public static registerTreePublisher(publisher: TreePublisher): void { Base.treePublisher = publisher; } From c43559cc1c3d14781fe617c72cfcbf912c1f0a91 Mon Sep 17 00:00:00 2001 From: Jeff Kreis Date: Fri, 15 Nov 2019 16:47:08 -0500 Subject: [PATCH 7/9] fix: test --- test/nodes/Base.test.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/nodes/Base.test.ts b/test/nodes/Base.test.ts index 038cabd..eb8e912 100644 --- a/test/nodes/Base.test.ts +++ b/test/nodes/Base.test.ts @@ -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; @@ -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 + maybePublishTree(_state: any, _event: any, _topLevel: boolean) { + published = true; + }, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + configure(_options: object) {}, + } as Behavior.TreePublisher; + + Base.registerTreePublisher(publisher); + const res = action.handleEvent(new TestState(), 'testEvent'); + + assert.equal(res, rc.SUCCESS); + assert.isTrue(published); + }); + }); }); From e472d36cf53fccddd8b133a48c01730eac94bc74 Mon Sep 17 00:00:00 2001 From: Jeff Kreis Date: Mon, 18 Nov 2019 12:36:00 -0500 Subject: [PATCH 8/9] fix: renaming method and simplify typing of static --- lib/nodes/Base.ts | 4 ++-- lib/utils/TreePublisher.ts | 4 ++-- test/nodes/Base.test.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/nodes/Base.ts b/lib/nodes/Base.ts index dfa10f0..ea9098b 100644 --- a/lib/nodes/Base.ts +++ b/lib/nodes/Base.ts @@ -18,7 +18,7 @@ export class Base { // 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: TreeNonPublisher | TreePublisher = new TreeNonPublisher(); + static treePublisher: TreePublisher = new TreeNonPublisher(); public static registerTreePublisher(publisher: TreePublisher): void { Base.treePublisher = publisher; @@ -51,7 +51,7 @@ export class Base { } try { - Base.treePublisher.maybePublishTree(state, event, false); + Base.treePublisher.publishTree(state, event, false); const result = this.onEvent(state, event); return this._afterEvent(result, state, event); } catch (err) { diff --git a/lib/utils/TreePublisher.ts b/lib/utils/TreePublisher.ts index 4227ae3..41975a7 100644 --- a/lib/utils/TreePublisher.ts +++ b/lib/utils/TreePublisher.ts @@ -1,13 +1,13 @@ import {BlueshellState} from '../nodes/BlueshellState'; export interface TreePublisher { - maybePublishTree(state: B, event: V, topLevel: boolean): void; + publishTree(state: B, event: V, topLevel: boolean): void; configure(options: object): void; } export class TreeNonPublisher implements TreePublisher { // eslint-disable-next-line @typescript-eslint/no-unused-vars - maybePublishTree(_state: BlueshellState, _event: any, _topLevel: boolean) {} + publishTree(_state: BlueshellState, _event: any, _topLevel: boolean) {} // eslint-disable-next-line @typescript-eslint/no-unused-vars configure(_options: object) {} } diff --git a/test/nodes/Base.test.ts b/test/nodes/Base.test.ts index eb8e912..5f3845f 100644 --- a/test/nodes/Base.test.ts +++ b/test/nodes/Base.test.ts @@ -130,7 +130,7 @@ describe('Base', function() { let published = false; const publisher = { // eslint-disable-next-line @typescript-eslint/no-unused-vars - maybePublishTree(_state: any, _event: any, _topLevel: boolean) { + publishTree(_state: any, _event: any, _topLevel: boolean) { published = true; }, // eslint-disable-next-line @typescript-eslint/no-unused-vars From 2f04d8e85cc90889d318e3597401e09050bee5e0 Mon Sep 17 00:00:00 2001 From: Jeff Kreis Date: Mon, 18 Nov 2019 12:42:54 -0500 Subject: [PATCH 9/9] fix: rename --- lib/nodes/Base.ts | 2 +- lib/utils/TreePublisher.ts | 4 ++-- test/nodes/Base.test.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/nodes/Base.ts b/lib/nodes/Base.ts index ea9098b..a1c7679 100644 --- a/lib/nodes/Base.ts +++ b/lib/nodes/Base.ts @@ -51,7 +51,7 @@ export class Base { } try { - Base.treePublisher.publishTree(state, event, false); + Base.treePublisher.publishResult(state, event, false); const result = this.onEvent(state, event); return this._afterEvent(result, state, event); } catch (err) { diff --git a/lib/utils/TreePublisher.ts b/lib/utils/TreePublisher.ts index 41975a7..c1f24ad 100644 --- a/lib/utils/TreePublisher.ts +++ b/lib/utils/TreePublisher.ts @@ -1,13 +1,13 @@ import {BlueshellState} from '../nodes/BlueshellState'; export interface TreePublisher { - publishTree(state: B, event: V, topLevel: boolean): void; + publishResult(state: B, event: V, topLevel: boolean): void; configure(options: object): void; } export class TreeNonPublisher implements TreePublisher { // eslint-disable-next-line @typescript-eslint/no-unused-vars - publishTree(_state: BlueshellState, _event: any, _topLevel: boolean) {} + publishResult(_state: BlueshellState, _event: any, _topLevel: boolean) {} // eslint-disable-next-line @typescript-eslint/no-unused-vars configure(_options: object) {} } diff --git a/test/nodes/Base.test.ts b/test/nodes/Base.test.ts index 5f3845f..67eb380 100644 --- a/test/nodes/Base.test.ts +++ b/test/nodes/Base.test.ts @@ -130,7 +130,7 @@ describe('Base', function() { let published = false; const publisher = { // eslint-disable-next-line @typescript-eslint/no-unused-vars - publishTree(_state: any, _event: any, _topLevel: boolean) { + publishResult(_state: any, _event: any, _topLevel: boolean) { published = true; }, // eslint-disable-next-line @typescript-eslint/no-unused-vars