-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core-kernel): initial draft of actions
- Loading branch information
Brian Faust
committed
Aug 26, 2019
1 parent
f407903
commit 1a823ee
Showing
14 changed files
with
376 additions
and
10 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
__tests__/unit/core-kernel/services/actions/actions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Actions } from "../../../../../packages/core-kernel/src/services/actions/actions"; | ||
import { InvalidArgumentException } from "../../../../../packages/core-kernel/src/exceptions/logic"; | ||
import { Action } from "../../../../../packages/core-kernel/src/services/actions/action"; | ||
|
||
let actions: Actions; | ||
beforeEach(() => { | ||
actions = new Actions(); | ||
}); | ||
|
||
test("binds an action with a <before> hook and executes them", async () => { | ||
let fnValue = 0; | ||
|
||
actions.bind("count", () => fnValue++).before(() => fnValue++); | ||
|
||
await actions.call("count"); | ||
|
||
expect(fnValue).toBe(2); | ||
}); | ||
|
||
test("binds an action with a <error> hook and executes them", async () => { | ||
let fnValue = 0; | ||
|
||
actions | ||
.bind("count", () => { | ||
fnValue++; | ||
|
||
throw new Error("Hello World"); | ||
}) | ||
.error(() => fnValue++); | ||
|
||
await actions.call("count"); | ||
|
||
expect(fnValue).toBe(2); | ||
}); | ||
|
||
test("binds an action with a <after> hook and executes them", async () => { | ||
let fnValue = 0; | ||
|
||
actions.bind("count", () => fnValue++).after(() => fnValue++); | ||
|
||
await actions.call("count"); | ||
|
||
expect(fnValue).toBe(2); | ||
}); | ||
|
||
test("binds an action with <before/error/after> hooks and executes them", async () => { | ||
let fnValue = 0; | ||
|
||
actions | ||
.bind("count", () => { | ||
fnValue++; | ||
|
||
throw new Error("Hello World"); | ||
}) | ||
.before(() => fnValue++) | ||
.error(() => fnValue++) | ||
.after(() => fnValue++); | ||
|
||
await actions.call("count"); | ||
|
||
expect(fnValue).toBe(4); | ||
}); | ||
|
||
test("throws an error if an action is not registered", async () => { | ||
expect(actions.call("count")).rejects.toThrowError( | ||
new InvalidArgumentException("The given action [count] is not available."), | ||
); | ||
}); | ||
|
||
test("throws an error if an action is already registered", async () => { | ||
actions.bind("duplicate", Function); | ||
|
||
expect(() => { | ||
actions.bind("duplicate", Function); | ||
}).toThrowError(new InvalidArgumentException("The given action [duplicate] is already registered.")); | ||
}); | ||
|
||
test("throws an error if an action is reserved", async () => { | ||
expect(() => { | ||
actions.bind("internal.action", Function); | ||
}).toThrowError(new InvalidArgumentException("The given action [internal.action] is reserved.")); | ||
}); | ||
|
||
describe("get", () => { | ||
test("returns an action for the given name", async () => { | ||
const fn = () => {}; | ||
|
||
actions.bind("count", fn); | ||
|
||
expect(actions.get("count")).toEqual(new Action(fn)); | ||
}); | ||
|
||
test("throws an error if an action is not registered", async () => { | ||
expect(() => actions.get("count")).toThrowError( | ||
new InvalidArgumentException("The given action [count] is not available."), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,8 +45,5 @@ | |
}, | ||
"engines": { | ||
"node": ">=10.x" | ||
}, | ||
"jest": { | ||
"preset": "../../jest-preset.json" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import * as Events from "./events"; | ||
import * as Process from "./process"; | ||
|
||
export { Events }; | ||
export { Events, Process }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* @export | ||
* @enum {number} | ||
*/ | ||
export enum ShutdownSignal { | ||
SIGHUP = "SIGHUP", | ||
SIGINT = "SIGINT", | ||
SIGQUIT = "SIGQUIT", | ||
SIGILL = "SIGILL", | ||
SIGTRAP = "SIGTRAP", | ||
SIGABRT = "SIGABRT", | ||
SIGBUS = "SIGBUS", | ||
SIGFPE = "SIGFPE", | ||
SIGSEGV = "SIGSEGV", | ||
SIGUSR2 = "SIGUSR2", | ||
SIGTERM = "SIGTERM", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { FunctionReturning } from "awilix"; | ||
|
||
export class Action<T> { | ||
/** | ||
* @private | ||
* @type {Set<FunctionReturning<void>>} | ||
* @memberof Action | ||
*/ | ||
private readonly beforeHooks: Set<FunctionReturning<void>> = new Set<FunctionReturning<void>>(); | ||
|
||
/** | ||
* @private | ||
* @type {Set<FunctionReturning<void>>} | ||
* @memberof Action | ||
*/ | ||
private readonly errorHooks: Set<FunctionReturning<void>> = new Set<FunctionReturning<void>>(); | ||
|
||
/** | ||
* @private | ||
* @type {Set<FunctionReturning<void>>} | ||
* @memberof Action | ||
*/ | ||
private readonly afterHooks: Set<FunctionReturning<void>> = new Set<FunctionReturning<void>>(); | ||
|
||
/** | ||
* @param {FunctionReturning<void>} fn | ||
* @memberof Action | ||
*/ | ||
public constructor(private readonly fn: FunctionReturning<T>) {} | ||
|
||
/** | ||
* @returns {FunctionReturning<void>} | ||
* @memberof Action | ||
*/ | ||
public execute(...args: any[]): T { | ||
return this.fn(args); | ||
} | ||
|
||
/** | ||
* @param {FunctionReturning<void>} fn | ||
* @memberof Action | ||
*/ | ||
public before(fn: FunctionReturning<void>): this { | ||
this.beforeHooks.add(fn); | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* @param {FunctionReturning<void>} fn | ||
* @memberof Action | ||
*/ | ||
public error(fn: FunctionReturning<void>): this { | ||
this.errorHooks.add(fn); | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* @param {FunctionReturning<void>} fn | ||
* @memberof Action | ||
*/ | ||
public after(fn: FunctionReturning<void>): this { | ||
this.afterHooks.add(fn); | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* @param {string} type | ||
* @returns {Set<FunctionReturning<void>>} | ||
* @memberof Action | ||
*/ | ||
public hooks(type: string): Set<FunctionReturning<void>> { | ||
return this[`${type}Hooks`]; | ||
} | ||
} |
Oops, something went wrong.