-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
37 changed files
with
963 additions
and
1,369 deletions.
There are no files selected for viewing
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,5 +1,5 @@ | ||
{ | ||
"printWidth": 80, | ||
"printWidth": 100, | ||
"tabWidth": 2, | ||
"semi": true, | ||
"singleQuote": true, | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import shimmer from 'shimmer'; | ||
import * as logger from './logger'; | ||
import { safeExecute } from './utils'; | ||
|
||
const noop = () => {}; | ||
|
||
const isFunctionAlreadyWrapped = fn => fn && fn.__wrapped; | ||
|
||
export const hook = (module, funcName, options = {}) => { | ||
const { beforeHook = noop, afterHook = noop } = options; | ||
const safeBeforeHook = safeExecute(beforeHook, `before hook of ${funcName} fail`); | ||
const safeAfterHook = safeExecute(afterHook, `after hook of ${funcName} fail`); | ||
const extenderContext = {}; | ||
try { | ||
const wrapper = originalFn => { | ||
if (isFunctionAlreadyWrapped(originalFn)) return originalFn; | ||
return function(...args) { | ||
safeBeforeHook.call(this, args, extenderContext); | ||
const originalFnResult = originalFn.apply(this, args); | ||
safeAfterHook.call(this, args, originalFnResult, extenderContext); | ||
return originalFnResult; | ||
}; | ||
}; | ||
shimmer.wrap(module, funcName, wrapper); | ||
} catch (e) { | ||
logger.warn(`Wrapping of function ${funcName} failed`, options); | ||
} | ||
}; |
Oops, something went wrong.