mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 22:23:42 +01:00
Merge pull request #5088 from overleaf/em-queue-feature-refreshes
Perform some user refreshes in the background GitOrigin-RevId: 3aec73c827bf0f7de7bd9caa369dfc653eac5dd0
This commit is contained in:
@@ -4,6 +4,7 @@ const pLimit = require('p-limit')
|
||||
module.exports = {
|
||||
promisify,
|
||||
promisifyAll,
|
||||
promisifyClass,
|
||||
promisifyMultiResult,
|
||||
callbackify,
|
||||
callbackifyMultiResult,
|
||||
@@ -57,6 +58,34 @@ function promisifyAll(module, opts = {}) {
|
||||
return promises
|
||||
}
|
||||
|
||||
/**
|
||||
* Promisify all methods in a class.
|
||||
*
|
||||
* Options are the same as for promisifyAll
|
||||
*/
|
||||
function promisifyClass(cls, opts = {}) {
|
||||
const promisified = class extends cls {}
|
||||
const { without = [], multiResult = {} } = opts
|
||||
for (const propName of Object.getOwnPropertyNames(cls.prototype)) {
|
||||
if (propName === 'constructor' || without.includes(propName)) {
|
||||
continue
|
||||
}
|
||||
const propValue = cls.prototype[propName]
|
||||
if (typeof propValue !== 'function') {
|
||||
continue
|
||||
}
|
||||
if (multiResult[propName] != null) {
|
||||
promisified.prototype[propName] = promisifyMultiResult(
|
||||
propValue,
|
||||
multiResult[propName]
|
||||
)
|
||||
} else {
|
||||
promisified.prototype[propName] = promisify(propValue)
|
||||
}
|
||||
}
|
||||
return promisified
|
||||
}
|
||||
|
||||
/**
|
||||
* Promisify a function that returns multiple results via additional callback
|
||||
* parameters.
|
||||
@@ -78,7 +107,7 @@ function promisifyMultiResult(fn, resultNames) {
|
||||
function promisified(...args) {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
fn(...args, (err, ...results) => {
|
||||
fn.bind(this)(...args, (err, ...results) => {
|
||||
if (err != null) {
|
||||
return reject(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user