Passed
Push — main ( 2cd17f...238ec8 )
by Lorenzo
01:26 queued 16s
created

Hook.ts ➔ Hook   A

Complexity

Conditions 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 18
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.75
cc 1
crap 1
1
import { ExpressBean } from '@/ExpressBeansTypes';
2 7
import { logger, registeredMethods } from '@/core';
3 7
import { Executor, ExecutorPhase } from '@/core/executor';
4
5
/**
6
   * Hook a function to a phase.
7
   * @decorator
8
   */
9 7
export function Hook(phase: ExecutorPhase) {
10 28
  return<This>(
11
    method: () => any,
12
    context: ClassMethodDecoratorContext<This, () => any>,
13
  ) => {
14 9
    logger.debug(`Registering ${phase} function ${String(context.name)}`);
15 9
    Executor.setExecution('start', () => {
16 9
      const bean = registeredMethods.get(method) as ExpressBean;
17 9
      logger.debug(`Initializing ${bean._className}.${String(context.name)} as ${phase} function`);
18 9
      Executor.setExecution(phase, () => method.bind(bean)(), method);
19
    });
20
21 9
    return method;
22
  };
23
}
24