-
Notifications
You must be signed in to change notification settings - Fork 1.8k
NODE-2579/3.6/global-promise-prereq #2340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
158c141
32eb671
9960092
d3e8428
a6bc216
75278d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
'use strict'; | ||
|
||
const PromiseProvider = require('./promise_provider'); | ||
const deprecate = require('util').deprecate; | ||
const deprecateOptions = require('./utils').deprecateOptions; | ||
const checkCollectionName = require('./utils').checkCollectionName; | ||
|
@@ -128,7 +129,7 @@ function Collection(db, topology, dbName, name, pkFactory, options) { | |
const namespace = new MongoDBNamespace(dbName, name); | ||
|
||
// Get the promiseLibrary | ||
const promiseLibrary = options.promiseLibrary || Promise; | ||
const promiseLibrary = PromiseProvider.get(options, db, topology); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's a case that I'm taking about. It's a little more comprehensive than it previously was. I read this as, check the |
||
|
||
// Set custom primary key factory if provided | ||
pkFactory = pkFactory == null ? ObjectID : pkFactory; | ||
|
@@ -443,7 +444,7 @@ Collection.prototype.find = deprecateOptions( | |
newOptions.db = this.s.db; | ||
|
||
// Add the promise library | ||
newOptions.promiseLibrary = this.s.promiseLibrary; | ||
newOptions.promiseLibrary = PromiseProvider.get(options, this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is within There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm understanding things correctly, in order to keep the behavior unchanged here for Any methods in |
||
|
||
// Set raw if available at collection level | ||
if (newOptions.raw == null && typeof this.s.raw === 'boolean') newOptions.raw = this.s.raw; | ||
|
@@ -748,13 +749,14 @@ Collection.prototype.insert = deprecate(function(docs, options, callback) { | |
* @return {Promise} returns Promise if no callback passed | ||
*/ | ||
Collection.prototype.updateOne = function(filter, update, options, callback) { | ||
const Promise = PromiseProvider.get(this); | ||
if (typeof options === 'function') (callback = options), (options = {}); | ||
options = options || {}; | ||
|
||
const err = checkForAtomicOperators(update); | ||
if (err) { | ||
if (typeof callback === 'function') return callback(err); | ||
return this.s.promiseLibrary.reject(err); | ||
return Promise.reject(err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these are frustrating.. they should really be deferred to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should that change be in this PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, just calling it out |
||
} | ||
|
||
options = Object.assign({}, options); | ||
|
@@ -827,13 +829,14 @@ Collection.prototype.replaceOne = function(filter, doc, options, callback) { | |
* @return {Promise<Collection~updateWriteOpResult>} returns Promise if no callback passed | ||
*/ | ||
Collection.prototype.updateMany = function(filter, update, options, callback) { | ||
const Promise = PromiseProvider.get(this); | ||
if (typeof options === 'function') (callback = options), (options = {}); | ||
options = options || {}; | ||
|
||
const err = checkForAtomicOperators(update); | ||
if (err) { | ||
if (typeof callback === 'function') return callback(err); | ||
return this.s.promiseLibrary.reject(err); | ||
return Promise.reject(err); | ||
} | ||
|
||
options = Object.assign({}, options); | ||
|
@@ -1754,6 +1757,7 @@ Collection.prototype.findOneAndReplace = function(filter, replacement, options, | |
* @return {Promise<Collection~findAndModifyWriteOpResultObject>} returns Promise if no callback passed | ||
*/ | ||
Collection.prototype.findOneAndUpdate = function(filter, update, options, callback) { | ||
const Promise = PromiseProvider.get(this); | ||
if (typeof options === 'function') (callback = options), (options = {}); | ||
options = options || {}; | ||
|
||
|
@@ -1766,7 +1770,7 @@ Collection.prototype.findOneAndUpdate = function(filter, update, options, callba | |
const err = checkForAtomicOperators(update); | ||
if (err) { | ||
if (typeof callback === 'function') return callback(err); | ||
return this.s.promiseLibrary.reject(err); | ||
return Promise.reject(err); | ||
} | ||
|
||
const findOneAndUpdateOperation = new FindOneAndUpdateOperation(this, filter, update, options); | ||
|
@@ -1990,7 +1994,7 @@ Collection.prototype.parallelCollectionScan = deprecate(function(options, callba | |
options.readPreference = resolveReadPreference(this, options); | ||
|
||
// Add a promiseLibrary | ||
options.promiseLibrary = this.s.promiseLibrary; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little confused about what setting |
||
options.promiseLibrary = PromiseProvider.get(this); | ||
|
||
if (options.session) { | ||
options.session = undefined; | ||
|
@@ -2171,7 +2175,7 @@ Collection.prototype.initializeUnorderedBulkOp = function(options) { | |
options.ignoreUndefined = this.s.options.ignoreUndefined; | ||
} | ||
|
||
options.promiseLibrary = this.s.promiseLibrary; | ||
options.promiseLibrary = PromiseProvider.get(this); | ||
return unordered(this.s.topology, this, options); | ||
}; | ||
|
||
|
@@ -2194,7 +2198,7 @@ Collection.prototype.initializeOrderedBulkOp = function(options) { | |
if (options.ignoreUndefined == null) { | ||
options.ignoreUndefined = this.s.options.ignoreUndefined; | ||
} | ||
options.promiseLibrary = this.s.promiseLibrary; | ||
options.promiseLibrary = PromiseProvider.get(this); | ||
return ordered(this.s.topology, this, options); | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
'use strict'; | ||
|
||
const PromiseProvider = require('../../promise_provider'); | ||
const Denque = require('denque'); | ||
const EventEmitter = require('events'); | ||
const ServerDescription = require('./server_description').ServerDescription; | ||
|
@@ -193,7 +195,7 @@ class Topology extends EventEmitter { | |
// Active client sessions | ||
sessions: new Set(), | ||
// Promise library | ||
promiseLibrary: options.promiseLibrary || Promise, | ||
promiseLibrary: PromiseProvider.get(options), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A user can't create a |
||
credentials: options.credentials, | ||
clusterTime: null, | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
'use strict'; | ||
|
||
const PromiseProvider = require('./promise_provider'); | ||
const Transform = require('stream').Transform; | ||
const PassThrough = require('stream').PassThrough; | ||
const deprecate = require('util').deprecate; | ||
|
@@ -111,7 +112,7 @@ class Cursor extends CoreCursor { | |
const currentNumberOfRetries = numberOfRetries; | ||
|
||
// Get the promiseLibrary | ||
const promiseLibrary = options.promiseLibrary || Promise; | ||
const promiseLibrary = PromiseProvider.get(options, topology); | ||
|
||
// Internal cursor state | ||
this.s = { | ||
|
@@ -727,6 +728,7 @@ class Cursor extends CoreCursor { | |
* @return {Promise} if no callback supplied | ||
*/ | ||
forEach(iterator, callback) { | ||
const Promise = PromiseProvider.get(this); | ||
// Rewind cursor state | ||
this.rewind(); | ||
|
||
|
@@ -751,7 +753,7 @@ class Cursor extends CoreCursor { | |
} | ||
}); | ||
} else { | ||
return new this.s.promiseLibrary((fulfill, reject) => { | ||
return new Promise((fulfill, reject) => { | ||
each(this, (err, doc) => { | ||
if (err) { | ||
reject(err); | ||
|
@@ -910,6 +912,7 @@ class Cursor extends CoreCursor { | |
* @return {Promise} returns Promise if no callback passed | ||
*/ | ||
close(options, callback) { | ||
const Promise = PromiseProvider.get(this); | ||
if (typeof options === 'function') (callback = options), (options = {}); | ||
options = Object.assign({}, { skipKillCursors: false }, options); | ||
|
||
|
@@ -929,17 +932,15 @@ class Cursor extends CoreCursor { | |
} | ||
|
||
// Return a Promise | ||
return new this.s.promiseLibrary(resolve => { | ||
resolve(); | ||
}); | ||
return Promise.resolve(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was an opinionated change, no reason to call new here just to resolve. |
||
}; | ||
|
||
if (this.cursorState.session) { | ||
if (typeof callback === 'function') { | ||
return this._endSession(() => completeClose()); | ||
} | ||
|
||
return new this.s.promiseLibrary(resolve => { | ||
return new Promise(resolve => { | ||
this._endSession(() => completeClose().then(resolve)); | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wanted to point out that this class has been refactored to use
maybePromise
so most of these changes will cause merge conflicts once #2333 makes its way into3.6
. It might be easier to just revert this file and add a/* es-lint-disable promise/no-native */
at the top, along with a note to update once that PR is merged in - not a big deal either way.