Skip to content

Commit

Permalink
feat: added this.break to bypass advice stack when needed
Browse files Browse the repository at this point in the history
16
  • Loading branch information
k1r0s committed Aug 11, 2017
1 parent 8cc000c commit 032aa3f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/core/AdvicePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
export abstract class AdvicePool {
static stopped: boolean
static next () {}
static break () {}
static stop () {}
}
21 changes: 19 additions & 2 deletions src/core/CallStackIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class CallStackIterator {

private index: number = -1
private stopped: boolean = false
private broken: boolean = false

/**
* {constructor}
Expand All @@ -34,7 +35,7 @@ export class CallStackIterator {
this.index++

// asign currentEntry to the following entry
let currentEntry = this.stack[this.index]
let currentEntry = this.broken ? null : this.stack[this.index]

// currentEntry evals to 'null' meaning that the next entry is invoke main method.
// but it will be skipped if 'this.stopped' evals to 'true' (this.stop was invoked)
Expand All @@ -56,7 +57,9 @@ export class CallStackIterator {
}
}
}
this.next()
if (!this.broken) {
this.next()
}
}

// if currentEntry has a value, it must be an entry/advice descriptor
Expand All @@ -74,6 +77,20 @@ export class CallStackIterator {
this.stopped = true
}

/**
* this method will prevent all following advices to be called by
* removing them from call stack queue
*/
private break () {
this.broken = true
}

/**
* @private executeAdvice
* this method executes de current advice on the current context, if the advice is not
* `asynchronous`, it will call next() on finish.
* @param currentEntry
*/
private executeAdvice (currentEntry: IStackEntry) {
currentEntry.adviceFn.apply(this, this.transformArguments(currentEntry))
if (!this.isAsync(currentEntry.adviceFn)) {
Expand Down
1 change: 1 addition & 0 deletions src/interface/IAdviceContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
export interface IAdviceContext {
stopped: boolean
next (): void
break (): void
stop (): void
}
21 changes: 21 additions & 0 deletions test/demos/breakCallStack.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { beforeMethod, afterMethod } from "../../src/kaop-ts"

class YetAnotherDummyTest {

static something: number = 1

@beforeMethod(function (meta) { this.break() })
@beforeMethod(function (meta) { meta.target.something++ })
@beforeMethod(function (meta) { throw new Error(`Shouldn't be called here!!!`) })
@beforeMethod(function (meta) { meta.target.something++ })
@afterMethod(function (meta) { throw new Error(`I also shouldn't be called!!!11`) })
@afterMethod(function (meta) { meta.target.something++ })
static some () { console.log('hi') }
}

describe("kaop-ts demo -> break call stack if needed", () => {
it("advices can determine if the following call stack must be break apart", () => {
YetAnotherDummyTest.some()
expect(YetAnotherDummyTest.something).toBeLessThan(2)
})
})

0 comments on commit 032aa3f

Please sign in to comment.