不要怂,就是干,撸起袖子干!

Commit 2eaf32a3 by overlookmotel

Static method to run function in CLS context

1 parent 000b28c7
Showing with 18 additions and 8 deletions
......@@ -975,18 +975,11 @@ class Sequelize {
// testhint argsConform.end
const transaction = new Transaction(this, options);
const ns = Sequelize._cls;
if (!autoCallback) return transaction.prepareEnvironment().return(transaction);
// autoCallback provided
const wrapper = ns ? function(fn) {
var promise;
ns.run(() => promise = fn());
return promise;
} : function(fn) { return fn(); };
return wrapper(() =>
return Sequelize._clsRun(() =>
transaction.prepareEnvironment().then(() =>
autoCallback(transaction)
).tap(() =>
......@@ -1023,6 +1016,23 @@ class Sequelize {
return this;
}
/**
* Run function in CLS context.
* If no CLS context in use, just runs the function normally
*
* @private
* @param {Function} fn Function to run
* @returns {*} Return value of function
*/
static _clsRun(fn) {
var ns = Sequelize._cls;
if (!ns) return fn();
var res;
ns.run((context) => res = fn(context));
return res;
}
static get cls() {
return this._cls;
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!