Skip to content

Commit

Permalink
Merge pull request #8573 from hugosenari/decouple
Browse files Browse the repository at this point in the history
refactor(utils): moving promiseOrCallback to helpers/promiseOrCallback
  • Loading branch information
vkarpov15 committed Feb 12, 2020
2 parents a30c8fa + a746435 commit 2cad4aa
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 83 deletions.
5 changes: 3 additions & 2 deletions lib/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const AggregationCursor = require('./cursor/AggregationCursor');
const Query = require('./query');
const applyGlobalMaxTimeMS = require('./helpers/query/applyGlobalMaxTimeMS');
const promiseOrCallback = require('./helpers/promiseOrCallback');
const util = require('util');
const utils = require('./utils');
const read = Query.prototype.read;
Expand Down Expand Up @@ -699,7 +700,7 @@ Aggregate.prototype.redact = function(expression, thenExpr, elseExpr) {
Aggregate.prototype.explain = function(callback) {
const model = this._model;

return utils.promiseOrCallback(callback, cb => {
return promiseOrCallback(callback, cb => {
if (!this._pipeline.length) {
const err = new Error('Aggregate has empty pipeline');
return cb(err);
Expand Down Expand Up @@ -954,7 +955,7 @@ Aggregate.prototype.exec = function(callback) {
return new AggregationCursor(this);
}

return utils.promiseOrCallback(callback, cb => {
return promiseOrCallback(callback, cb => {

prepareDiscriminatorPipeline(this);

Expand Down
5 changes: 3 additions & 2 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const MongooseError = require('./error/index');
const PromiseProvider = require('./promise_provider');
const TimeoutError = require('./error/timeout');
const applyPlugins = require('./helpers/schema/applyPlugins');
const promiseOrCallback = require('./helpers/promiseOrCallback');
const get = require('./helpers/get');
const immediate = require('./helpers/immediate');
const mongodb = require('mongodb');
Expand Down Expand Up @@ -469,7 +470,7 @@ function _wrapConnHelper(fn) {
Array.prototype.slice.call(arguments);
const disconnectedError = new MongooseError('Connection ' + this.id +
' was disconnected when calling `' + fn.name + '`');
return utils.promiseOrCallback(cb, cb => {
return promiseOrCallback(cb, cb => {
// Make it ok to call collection helpers before `mongoose.connect()`
// as long as `mongoose.connect()` is called on the same tick.
// Re: gh-8534
Expand Down Expand Up @@ -859,7 +860,7 @@ Connection.prototype.close = function(force, callback) {

this.$wasForceClosed = !!force;

return utils.promiseOrCallback(callback, cb => {
return promiseOrCallback(callback, cb => {
this._close(force, cb);
});
};
Expand Down
6 changes: 3 additions & 3 deletions lib/cursor/AggregationCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

const MongooseError = require('../error/mongooseError');
const Readable = require('stream').Readable;
const promiseOrCallback = require('../helpers/promiseOrCallback');
const eachAsync = require('../helpers/cursor/eachAsync');
const util = require('util');
const utils = require('../utils');

/**
* An AggregationCursor is a concurrency primitive for processing aggregation
Expand Down Expand Up @@ -164,7 +164,7 @@ AggregationCursor.prototype._markError = function(error) {
*/

AggregationCursor.prototype.close = function(callback) {
return utils.promiseOrCallback(callback, cb => {
return promiseOrCallback(callback, cb => {
this.cursor.close(error => {
if (error) {
cb(error);
Expand All @@ -187,7 +187,7 @@ AggregationCursor.prototype.close = function(callback) {
*/

AggregationCursor.prototype.next = function(callback) {
return utils.promiseOrCallback(callback, cb => {
return promiseOrCallback(callback, cb => {
_next(this, cb);
});
};
Expand Down
6 changes: 3 additions & 3 deletions lib/cursor/QueryCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
'use strict';

const Readable = require('stream').Readable;
const promiseOrCallback = require('../helpers/promiseOrCallback');
const eachAsync = require('../helpers/cursor/eachAsync');
const helpers = require('../queryhelpers');
const util = require('util');
const utils = require('../utils');

/**
* A QueryCursor is a concurrency primitive for processing query results
Expand Down Expand Up @@ -157,7 +157,7 @@ QueryCursor.prototype._markError = function(error) {
*/

QueryCursor.prototype.close = function(callback) {
return utils.promiseOrCallback(callback, cb => {
return promiseOrCallback(callback, cb => {
this.cursor.close(error => {
if (error) {
cb(error);
Expand All @@ -180,7 +180,7 @@ QueryCursor.prototype.close = function(callback) {
*/

QueryCursor.prototype.next = function(callback) {
return utils.promiseOrCallback(callback, cb => {
return promiseOrCallback(callback, cb => {
_next(this, function(error, doc) {
if (error) {
return cb(error);
Expand Down
5 changes: 3 additions & 2 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const StrictModeError = require('./error/strict');
const ValidationError = require('./error/validation');
const ValidatorError = require('./error/validator');
const VirtualType = require('./virtualtype');
const promiseOrCallback = require('./helpers/promiseOrCallback');
const cleanModifiedSubpaths = require('./helpers/document/cleanModifiedSubpaths');
const compile = require('./helpers/document/compile').compile;
const defineKey = require('./helpers/document/compile').defineKey;
Expand Down Expand Up @@ -2050,7 +2051,7 @@ Document.prototype.validate = function(pathsToValidate, options, callback) {
pathsToValidate = null;
}

return utils.promiseOrCallback(callback, cb => {
return promiseOrCallback(callback, cb => {
if (parallelValidate != null) {
return cb(parallelValidate);
}
Expand Down Expand Up @@ -3516,7 +3517,7 @@ Document.prototype.populate = function populate() {
*/

Document.prototype.execPopulate = function(callback) {
return utils.promiseOrCallback(callback, cb => {
return promiseOrCallback(callback, cb => {
this.populate(cb);
}, this.constructor.events);
};
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/cursor/eachAsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

const utils = require('../../utils');
const promiseOrCallback = require('../promiseOrCallback');

/**
* Execute `fn` for every document in the cursor. If `fn` returns a promise,
Expand Down Expand Up @@ -88,7 +88,7 @@ module.exports = function eachAsync(next, fn, options, callback) {
}
};

return utils.promiseOrCallback(callback, cb => {
return promiseOrCallback(callback, cb => {
iterate(cb);
});
};
Expand Down
6 changes: 3 additions & 3 deletions lib/helpers/model/applyHooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const symbols = require('../../schema/symbols');
const utils = require('../../utils');
const promiseOrCallback = require('../promiseOrCallback');

/*!
* ignore
Expand Down Expand Up @@ -123,10 +123,10 @@ function applyHooks(model, schema, options) {
const originalMethod = objToDecorate[method];
objToDecorate[method] = function() {
const args = Array.prototype.slice.call(arguments);
const cb = utils.last(args);
const cb = args.slice(-1).pop();
const argsWithoutCallback = typeof cb === 'function' ?
args.slice(0, args.length - 1) : args;
return utils.promiseOrCallback(cb, callback => {
return promiseOrCallback(cb, callback => {
return this[`$__${method}`].apply(this,
argsWithoutCallback.concat([callback]));
}, model.events);
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/model/applyStaticHooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const middlewareFunctions = require('../query/applyQueryMiddleware').middlewareFunctions;
const utils = require('../../utils');
const promiseOrCallback = require('../promiseOrCallback');

module.exports = function applyStaticHooks(model, hooks, statics) {
const kareemOptions = {
Expand Down Expand Up @@ -35,7 +35,7 @@ module.exports = function applyStaticHooks(model, hooks, statics) {
call(arguments, 0, cb == null ? numArgs : numArgs - 1);
// Special case: can't use `Kareem#wrap()` because it doesn't currently
// support wrapped functions that return a promise.
return utils.promiseOrCallback(cb, callback => {
return promiseOrCallback(cb, callback => {
hooks.execPre(key, model, args, function(err) {
if (err != null) {
return callback(err);
Expand Down
45 changes: 45 additions & 0 deletions lib/helpers/promiseOrCallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const PromiseProvider = require('../promise_provider');

const emittedSymbol = Symbol.for('mongoose:emitted');

module.exports = function promiseOrCallback(callback, fn, ee) {
if (typeof callback === 'function') {
return fn(function(error) {
if (error != null) {
if (ee != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
error[emittedSymbol] = true;
ee.emit('error', error);
}
try {
callback(error);
} catch (error) {
return process.nextTick(() => {
throw error;
});
}
return;
}
callback.apply(this, arguments);
});
}

const Promise = PromiseProvider.get();

return new Promise((resolve, reject) => {
fn(function(error, res) {
if (error != null) {
if (ee != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
error[emittedSymbol] = true;
ee.emit('error', error);
}
return reject(error);
}
if (arguments.length > 2) {
return resolve(Array.prototype.slice.call(arguments, 1));
}
resolve(res);
});
});
};
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const Query = require('./query');
const Model = require('./model');
const applyPlugins = require('./helpers/schema/applyPlugins');
const get = require('./helpers/get');
const promiseOrCallback = require('./helpers/promiseOrCallback');
const legacyPluralize = require('mongoose-legacy-pluralize');
const utils = require('./utils');
const pkg = require('../package.json');
Expand Down Expand Up @@ -343,7 +344,7 @@ Mongoose.prototype.connect = function(uri, options, callback) {
Mongoose.prototype.disconnect = function(callback) {
const _mongoose = this instanceof Mongoose ? this : mongoose;

return utils.promiseOrCallback(callback, cb => {
return promiseOrCallback(callback, cb => {
let remaining = _mongoose.connections.length;
if (remaining <= 0) {
return cb(null);
Expand Down

0 comments on commit 2cad4aa

Please sign in to comment.