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

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 { ...@@ -975,18 +975,11 @@ class Sequelize {
// testhint argsConform.end // testhint argsConform.end
const transaction = new Transaction(this, options); const transaction = new Transaction(this, options);
const ns = Sequelize._cls;
if (!autoCallback) return transaction.prepareEnvironment().return(transaction); if (!autoCallback) return transaction.prepareEnvironment().return(transaction);
// autoCallback provided // autoCallback provided
const wrapper = ns ? function(fn) { return Sequelize._clsRun(() =>
var promise;
ns.run(() => promise = fn());
return promise;
} : function(fn) { return fn(); };
return wrapper(() =>
transaction.prepareEnvironment().then(() => transaction.prepareEnvironment().then(() =>
autoCallback(transaction) autoCallback(transaction)
).tap(() => ).tap(() =>
...@@ -1023,6 +1016,23 @@ class Sequelize { ...@@ -1023,6 +1016,23 @@ class Sequelize {
return this; 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() { static get cls() {
return this._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!