1 | import extend from 'extend' |
||
2 | import path from 'path' |
||
3 | |||
4 | import hooksDefault from '../../hooks/hooks' |
||
5 | |||
6 | import { |
||
7 | config |
||
8 | ,coreUtils |
||
9 | ,abeExtend |
||
10 | } from '../' |
||
11 | |||
12 | import * as abe from '../' |
||
13 | |||
14 | let singleton = Symbol() |
||
15 | let singletonEnforcer = Symbol() |
||
16 | |||
17 | class Hooks { |
||
18 | |||
19 | constructor(enforcer) { |
||
20 | if(enforcer != singletonEnforcer) throw 'Cannot construct Json singleton' |
||
0 ignored issues
–
show
|
|||
21 | |||
22 | if(coreUtils.file.exist(path.join(config.root, config.hooks.url, 'hooks.js'))){ |
||
23 | var h = require(path.join(config.root, config.hooks.url, 'hooks.js')) |
||
24 | this.fn = extend(true, hooksDefault, h.default) |
||
25 | } |
||
26 | else{ |
||
27 | this.fn = hooksDefault |
||
28 | } |
||
29 | } |
||
30 | |||
31 | trigger() { |
||
32 | if(arguments.length > 0) { |
||
33 | var args = [].slice.call(arguments) |
||
34 | var fn = args.shift() |
||
35 | args.push(abe) |
||
36 | |||
37 | if(typeof this.fn !== 'undefined' && this.fn !== null |
||
38 | && typeof this.fn[fn] !== 'undefined' && this.fn[fn] !== null) { |
||
39 | args[0] = this.fn[fn].apply(this, args) |
||
40 | } |
||
41 | |||
42 | args[0] = abeExtend.plugins.instance.hooks.apply(abeExtend.plugins.instance, [fn].concat(args)) |
||
43 | } else { |
||
44 | args = [''] |
||
45 | } |
||
46 | |||
47 | return args[0] |
||
48 | } |
||
49 | |||
50 | static get instance() { |
||
51 | if(!this[singleton]) { |
||
52 | this[singleton] = new Hooks(singletonEnforcer) |
||
53 | } |
||
54 | return this[singleton] |
||
55 | } |
||
56 | } |
||
57 | |||
58 | export default Hooks |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.