Skip to content

Commit

Permalink
feat(state): Add ignore method for state to bypass all processing
Browse files Browse the repository at this point in the history
  • Loading branch information
timkinnane committed Aug 12, 2018
1 parent 7223e0c commit e736722
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/lib/state.ts
Expand Up @@ -7,6 +7,7 @@ import * as bot from '..'
*/
export interface IState {
done?: boolean
exit?: boolean
[key: string]: any
}

Expand Down Expand Up @@ -41,13 +42,20 @@ export class State implements IState {
listeners?: bot.Listener[]
envelopes?: bot.Envelope[]
method?: string
exit?: boolean
[key: string]: any

/** Create new state, usually assigned as `b` in middleware callbacks. */
constructor (startingState: IDispatchState) {
for (let key in startingState) this[key] = startingState[key]
}

/** Indicate that no more thought processes should look at this state */
ignore () {
this.exit = true
return this
}

/** Indicate that no other listener should be called for the state */
finish () {
this.done = true
Expand Down
27 changes: 27 additions & 0 deletions src/lib/thought.spec.ts
Expand Up @@ -289,6 +289,33 @@ describe('thought', () => {
await new bot.Thoughts(b).start('receive')
expect(b.processed).to.not.include.keys('listen')
})
it('calls post-process action if interrupted, not ignored', async () => {
bot.hearMiddleware((_: any, __: any, done: any) => {
done()
})
const b = new bot.State({ message })
const thoughts = new bot.Thoughts(b)
let listenActioned = false
thoughts.processes.listen.action = async function () {
listenActioned = true
}
await thoughts.start('receive')
expect(listenActioned).to.equal(true)
})
it('exits before post-process action if ignored', async () => {
bot.hearMiddleware((_: any, __: any, done: any) => {
b.ignore()
done()
})
const b = new bot.State({ message })
const thoughts = new bot.Thoughts(b)
let listenActioned = false
thoughts.processes.listen.action = async function () {
listenActioned = true
}
await thoughts.start('receive')
expect(listenActioned).to.equal(false)
})
it('does understand when listeners unmatched', async () => {
bot.listenCustom(() => false, () => null)
bot.understandCustom(() => true, () => null)
Expand Down
21 changes: 13 additions & 8 deletions src/lib/thought.ts
Expand Up @@ -49,16 +49,21 @@ export class Thought implements IThought {
* If process succeeds, timestamp is added to state.
*/
async process () {
// let isPending = true
if (this.b.exit) {
bot.logger.debug(`[thought] state ignored, exit processing`)
return Promise.resolve()
}
return new Promise((resolve, reject) => {
const { b, name, validate, middleware, listeners } = this
if (listeners && Object.keys(listeners).length === 0) {
bot.logger.debug(`[thought] skip ${name}, no listeners to process`)
return reject()
}
if (listeners && b.done) {
bot.logger.debug(`[thought] skip ${name}, listener processing is done`)
return reject()
if (typeof listeners !== 'undefined') {
if (Object.keys(listeners).length === 0) {
bot.logger.debug(`[thought] skip ${name}, no listeners to process`)
return reject()
}
if (b.done) {
bot.logger.debug(`[thought] skip ${name}, listener processing is done`)
return reject()
}
}
Promise.resolve(validate())
.then(async (valid) => {
Expand Down

0 comments on commit e736722

Please sign in to comment.