mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-08 06:55:46 +01:00
Async refactor in place (#7736)
* feat: allow both callback&and await
* feat: ignore async key
* feat: callbackify and promisify in same file
* Revert "feat: callbackify and promisify in same file"
This reverts commit cea206a9b8.
* feat: no need to store .callbackify
* feat: change getTopics to async
* feat: remove .async
* fix: byScore
* feat: rewrite topics/index and social with async/await
* fix: rewrite topics/data.js
fix issue with async.waterfall, only pass result if its not undefined
* feat: add callbackify to redis/psql
* feat: psql use await
* fix: redis 🌋
* feat: less returns
* feat: more await rewrite
* fix: redis tests
* feat: convert sortedSetAdd
rewrite psql transaction to async/await
* feat: 🐶
* feat: test
* feat: log client and query
* feat: log bind
* feat: more logs
* feat: more logs
* feat: check perform
* feat: dont callbackify transaction
* feat: remove logs
* fix: main functions
* feat: more logs
* fix: increment
* fix: rename
* feat: remove cls
* fix: remove console.log
* feat: add deprecation message to .async usage
* feat: update more dbal methods
* fix: redis :voodoo:
* feat: fix redis zrem, convert setObject
* feat: upgrade getObject methods
* fix: psql getObjectField
* fix: redis tests
* feat: getObjectKeys
* feat: getObjectValues
* feat: isObjectField
* fix: add missing return
* feat: delObjectField
* feat: incrObjectField
* fix: add missing await
* feat: remove exposed helpers
* feat: list methods
* feat: flush/empty
* feat: delete
* fix: redis delete all
* feat: get/set
* feat: incr/rename
* feat: type
* feat: expire
* feat: setAdd
* feat: setRemove
* feat: isSetMember
* feat: getSetMembers
* feat: setCount, setRemoveRandom
* feat: zcard,zcount
* feat: sortedSetRank
* feat: isSortedSetMember
* feat: zincrby
* feat: sortedSetLex
* feat: processSortedSet
* fix: add mising await
* feat: debug psql
* fix: psql test
* fix: test
* fix: another test
* fix: test fix
* fix: psql tests
* feat: remove logs
* feat: user arrow func
use builtin async promises
* feat: topic bookmarks
* feat: topic.delete
* feat: topic.restore
* feat: topics.purge
* feat: merge
* feat: suggested
* feat: topics/user.js
* feat: topics modules
* feat: topics/follow
* fix: deprecation msg
* feat: fork
* feat: topics/posts
* feat: sorted/recent
* feat: topic/teaser
* feat: topics/tools
* feat: topics/unread
* feat: add back node versions
disable deprecation notice
wrap async controllers in try/catch
* feat: use db directly
* feat: promisify in place
* fix: redis/psql
* feat: deprecation message
logs for psql
* feat: more logs
* feat: more logs
* feat: logs again
* feat: more logs
* fix: call release
* feat: restore travis, remove logs
* fix: loops
* feat: remove .async. usage
This commit is contained in:
committed by
GitHub
parent
43ce5f8af3
commit
805dcd7ca2
@@ -12,23 +12,93 @@ module.exports = function (theModule, ignoreKeys) {
|
||||
var str = func.toString().split('\n')[0];
|
||||
return str.includes('callback)');
|
||||
}
|
||||
function promisifyRecursive(module) {
|
||||
|
||||
function isAsyncFunction(fn) {
|
||||
return fn && fn.constructor && fn.constructor.name === 'AsyncFunction';
|
||||
}
|
||||
|
||||
var parts = [];
|
||||
function promisifyRecursive(module, key) {
|
||||
if (!module) {
|
||||
return;
|
||||
}
|
||||
if (key) {
|
||||
parts.push(key);
|
||||
}
|
||||
var keys = Object.keys(module);
|
||||
keys.forEach(function (key) {
|
||||
if (ignoreKeys.includes(key)) {
|
||||
return;
|
||||
}
|
||||
if (isCallbackedFunction(module[key])) {
|
||||
module[key] = util.promisify(module[key]);
|
||||
if (isAsyncFunction(module[key])) {
|
||||
module[key] = wrapIt(module[key], util.callbackify(module[key]));
|
||||
} else if (isCallbackedFunction(module[key])) {
|
||||
module[key] = wrapTwo(module[key], util.promisify(module[key]));
|
||||
} else if (typeof module[key] === 'object') {
|
||||
promisifyRecursive(module[key]);
|
||||
promisifyRecursive(module[key], key);
|
||||
}
|
||||
});
|
||||
parts.pop();
|
||||
}
|
||||
|
||||
function wrapTwo(origFn, promiseFn) {
|
||||
return function wrapper2(...args) {
|
||||
if (arguments.length && typeof arguments[arguments.length - 1] === 'function') {
|
||||
return origFn.apply(null, args);
|
||||
}
|
||||
|
||||
return promiseFn.apply(null, arguments);
|
||||
};
|
||||
}
|
||||
|
||||
function wrapIt(origFn, callbackFn) {
|
||||
return async function wrapper(...args) {
|
||||
if (arguments.length && typeof arguments[arguments.length - 1] === 'function') {
|
||||
const cb = args.pop();
|
||||
args.push(function (err, res) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
// fixes callbackified functions used in async.waterfall
|
||||
if (res !== undefined) {
|
||||
return cb(err, res);
|
||||
}
|
||||
return cb(err);
|
||||
});
|
||||
return callbackFn.apply(null, args);
|
||||
}
|
||||
return origFn.apply(null, arguments);
|
||||
};
|
||||
}
|
||||
|
||||
function deprecateRecursive(module, key) {
|
||||
if (!module) {
|
||||
return;
|
||||
}
|
||||
if (key) {
|
||||
parts.push(key);
|
||||
}
|
||||
var keys = Object.keys(module);
|
||||
keys.forEach(function (key) {
|
||||
if (ignoreKeys.includes(key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof module[key] === 'object') {
|
||||
deprecateRecursive(module[key], key);
|
||||
}
|
||||
|
||||
if (typeof module[key] === 'function') {
|
||||
module[key] = require('util').deprecate(module[key], '.async.' + (parts.concat([key]).join('.')) + ' usage is deprecated use .' + (parts.concat([key]).join('.')) + ' directly!');
|
||||
}
|
||||
});
|
||||
parts.pop();
|
||||
}
|
||||
|
||||
promisifyRecursive(theModule);
|
||||
const asyncModule = _.cloneDeep(theModule);
|
||||
promisifyRecursive(asyncModule);
|
||||
deprecateRecursive(asyncModule);
|
||||
|
||||
return asyncModule;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user