-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support context propagation in bluebird
- Loading branch information
Showing
3 changed files
with
32 additions
and
0 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
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,30 @@ | ||
import * as shimmer from 'shimmer'; | ||
|
||
import {PluginTypes} from '..'; | ||
|
||
import {bluebird_3} from './types'; | ||
|
||
type BluebirdModule = typeof bluebird_3&{prototype: {_then: Function;}}; | ||
|
||
const plugin: PluginTypes.Plugin = [{ | ||
// Bluebird is a class. | ||
// tslint:disable-next-line:variable-name | ||
patch: (Bluebird, tracer) => { | ||
// any is a type arg; args are type checked when read directly, otherwise | ||
// passed through to a function with the same type signature. | ||
// tslint:disable:no-any | ||
const wrapIfFunction = (fn: any) => | ||
typeof fn === 'function' ? tracer.wrap(fn) : fn; | ||
shimmer.wrap(Bluebird.prototype, '_then', (thenFn: Function) => { | ||
// Inherit context from the call site of .then(). | ||
return function<T>(this: bluebird_3<T>, ...args: any[]) { | ||
return thenFn.apply(this, [ | ||
wrapIfFunction(args[0]), wrapIfFunction(args[1]), ...args.slice(2) | ||
]); | ||
}; | ||
}); | ||
// tslint:enable:no-any | ||
} | ||
} as PluginTypes.Monkeypatch<BluebirdModule>]; | ||
|
||
export = plugin; |
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