Skip to content

Commit 1bcb087

Browse files
feat(action-chain): add name of operators to events
1 parent 9b60841 commit 1bcb087

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

packages/node_modules/action-chain/src/ActionBase.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class ActionBase<Context, InitialValue, Value = InitialValue> {
3535
this.initialActionId = initialActionId
3636

3737
let currentExecutionId = 0
38-
return Object.assign(function(value) {
38+
const func = Object.assign(function(value) {
3939
const initialOperator = typeof arguments[1] === 'undefined'
4040
const newPath = typeof arguments[2] === 'undefined' ? null : arguments[2]
4141
const executionContext: ExecutionContext = initialOperator
@@ -52,28 +52,34 @@ export class ActionBase<Context, InitialValue, Value = InitialValue> {
5252
actionChain.emit('action:start', {
5353
actionId: executionContext.__execution.actionId,
5454
executionId: executionContext.__execution.executionId,
55+
actionName: func.displayName,
56+
value,
5557
})
5658
}
5759
const returnValue = runOperators
5860
? runOperators(value, executionContext, newPath)
5961
: value
6062

6163
if (initialOperator && returnValue instanceof Promise) {
62-
returnValue.then(() => {
64+
returnValue.then((promiseValue) => {
6365
actionChain.emit('action:end', {
6466
actionId: executionContext.__execution.actionId,
6567
executionId: executionContext.__execution.executionId,
68+
actionName: func.displayName,
6669
})
6770
})
6871
} else if (initialOperator) {
6972
actionChain.emit('action:end', {
7073
actionId: executionContext.__execution.actionId,
7174
executionId: executionContext.__execution.executionId,
75+
actionName: func.displayName,
7276
})
7377
}
7478

7579
return returnValue
7680
}, this) as any
81+
82+
return func
7783
}
7884
getActionChain = () => {
7985
return this.actionChain

packages/node_modules/action-chain/src/ActionChain.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ export type Execution = {
2222
export type ActionExecution = {
2323
actionId: number
2424
executionId: number
25+
actionName: string
26+
value?: any
2527
}
2628

27-
export type OperatorExecution = ActionExecution & {
29+
export type OperatorExecution = {
30+
actionId: number
31+
executionId: number
2832
operatorId: number
2933
type: string
3034
name: string

packages/node_modules/action-chain/src/index.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ beforeEach(() => {
8080
ActionBase.nextActionId = 0
8181
})
8282

83+
describe('ACTION', () => {
84+
test('should be able to give the action a name', () => {
85+
expect.assertions(1)
86+
const test = action() as any
87+
test.displayName = 'My Name'
88+
actionChain.once('action:start', (execution) => {
89+
expect(execution.actionName).toEqual('My Name')
90+
})
91+
test()
92+
})
93+
})
94+
8395
describe('VALUE', () => {
8496
test('should run and return result', () => {
8597
const test = action<string>()
@@ -91,8 +103,6 @@ describe('VALUE', () => {
91103
describe('CONTEXT', () => {
92104
test('should pass default context', () => {
93105
expect.assertions(2)
94-
const wut = action()
95-
console.log(wut.test)
96106
const test = action().test(({ __execution, __path }: any) => {
97107
expect(__execution).toBeTruthy()
98108
expect(__path).toBeTruthy()

0 commit comments

Comments
 (0)