| 44 | } |
| 45 | |
| 46 | export class HookJs { |
| 47 | constructor (useProxy) { |
| 48 | this.useProxy = useProxy || false |
| 49 | this.hookPropertiesKeyName = '_hookProperties' + Date.now() |
| 50 | } |
| 51 | |
| 52 | hookJsPro () { |
| 53 | return new HookJs(true) |
| 54 | } |
| 55 | |
| 56 | _addHook (hookMethod, fn, type, classHook) { |
| 57 | const hookKeyName = type + 'Hooks' |
| 58 | const hookMethodProperties = hookMethod[this.hookPropertiesKeyName] |
| 59 | if (!hookMethodProperties[hookKeyName]) { |
| 60 | hookMethodProperties[hookKeyName] = [] |
| 61 | } |
| 62 | |
| 63 | /* Register (store) the hook function to be called while preventing repeated registration */ |
| 64 | let hasSameHook = false |
| 65 | for (let i = 0; i < hookMethodProperties[hookKeyName].length; i++) { |
| 66 | if (fn === hookMethodProperties[hookKeyName][i]) { |
| 67 | hasSameHook = true |
| 68 | break |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if (!hasSameHook) { |
| 73 | fn.classHook = classHook || false |
| 74 | hookMethodProperties[hookKeyName].push(fn) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | _runHooks (parentObj, methodName, originMethod, hookMethod, target, ctx, args, classHook, hookPropertiesKeyName) { |
| 79 | const hookMethodProperties = hookMethod[hookPropertiesKeyName] |
| 80 | const beforeHooks = hookMethodProperties.beforeHooks || [] |
| 81 | const afterHooks = hookMethodProperties.afterHooks || [] |
| 82 | const errorHooks = hookMethodProperties.errorHooks || [] |
| 83 | const hangUpHooks = hookMethodProperties.hangUpHooks || [] |
| 84 | const replaceHooks = hookMethodProperties.replaceHooks || [] |
| 85 | const execInfo = { |
| 86 | result: null, |
| 87 | error: null, |
| 88 | args: args, |
| 89 | type: '' |
| 90 | } |
| 91 | |
| 92 | function runHooks (hooks, type) { |
| 93 | let hookResult = null |
| 94 | execInfo.type = type || '' |
| 95 | if (Array.isArray(hooks)) { |
| 96 | hooks.forEach(fn => { |
| 97 | if (util.isFn(fn) && classHook === fn.classHook) { |
| 98 | hookResult = fn(args, parentObj, methodName, originMethod, execInfo, ctx) |
| 99 | } |
| 100 | }) |
| 101 | } |
| 102 | return hookResult |
| 103 | } |
nothing calls this directly
no outgoing calls
no test coverage detected