Skip to content

Commit

Permalink
fix(*): restore ability to webpack by removing makeLazyLoader
Browse files Browse the repository at this point in the history
* fix(lib): remove makeLazyLoader

* memoize lazy loading function
  • Loading branch information
kvwalker authored and mbroadst committed Jan 20, 2019
1 parent 6e896f4 commit 050267d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
17 changes: 14 additions & 3 deletions lib/operations/collection_ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ const handleCallback = require('../utils').handleCallback;
const indexInformationDb = require('./db_ops').indexInformation;
const isObject = require('../utils').isObject;
const Long = require('mongodb-core').BSON.Long;
const makeLazyLoader = require('../utils').makeLazyLoader;
const MongoError = require('mongodb-core').MongoError;
const ReadPreference = require('mongodb-core').ReadPreference;
const toError = require('../utils').toError;

const loadCollection = makeLazyLoader(`${__dirname}/../collection`);
const loadDb = makeLazyLoader(`${__dirname}/../db`);
let collection;
function loadCollection() {
if (!collection) {
collection = require('../collection');
}
return collection;
}
let db;
function loadDb() {
if (!db) {
db = require('../db');
}
return db;
}

/**
* Group function helper
Expand Down
9 changes: 7 additions & 2 deletions lib/operations/cursor_ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
const buildCountCommand = require('./collection_ops').buildCountCommand;
const formattedOrderClause = require('../utils').formattedOrderClause;
const handleCallback = require('../utils').handleCallback;
const makeLazyLoader = require('../utils').makeLazyLoader;
const MongoError = require('mongodb-core').MongoError;
const push = Array.prototype.push;

const loadCursor = makeLazyLoader(`${__dirname}/../cursor`);
let cursor;
function loadCursor() {
if (!cursor) {
cursor = require('../cursor');
}
return cursor;
}

/**
* Get the count of documents for this cursor.
Expand Down
17 changes: 14 additions & 3 deletions lib/operations/db_ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const resolveReadPreference = require('../utils').resolveReadPreference;
const crypto = require('crypto');
const debugOptions = require('../utils').debugOptions;
const handleCallback = require('../utils').handleCallback;
const makeLazyLoader = require('../utils').makeLazyLoader;
const MongoError = require('mongodb-core').MongoError;
const parseIndexOptions = require('../utils').parseIndexOptions;
const ReadPreference = require('mongodb-core').ReadPreference;
Expand All @@ -18,8 +17,20 @@ const findOne = require('./collection_ops').findOne;
const remove = require('./collection_ops').remove;
const updateOne = require('./collection_ops').updateOne;

const loadCollection = makeLazyLoader(`${__dirname}/../collection`);
const loadDb = makeLazyLoader(`${__dirname}/../db`);
let collection;
function loadCollection() {
if (!collection) {
collection = require('../collection');
}
return collection;
}
let db;
function loadDb() {
if (!db) {
db = require('../db');
}
return db;
}

const debugFields = [
'authSource',
Expand Down
9 changes: 7 additions & 2 deletions lib/operations/mongo_client_ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const authenticate = require('../authenticate');
const deprecate = require('util').deprecate;
const Logger = require('mongodb-core').Logger;
const makeLazyLoader = require('../utils').makeLazyLoader;
const MongoError = require('mongodb-core').MongoError;
const Mongos = require('../topologies/mongos');
const parse = require('mongodb-core').parseConnectionString;
Expand All @@ -12,7 +11,13 @@ const ReplSet = require('../topologies/replset');
const Server = require('../topologies/server');
const ServerSessionPool = require('mongodb-core').Sessions.ServerSessionPool;

const loadClient = makeLazyLoader(`${__dirname}/../mongo_client`);
let client;
function loadClient() {
if (!client) {
client = require('../mongo_client');
}
return client;
}

const monitoringEvents = [
'timeout',
Expand Down
13 changes: 1 addition & 12 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,16 +694,6 @@ function deprecateOptions(config, fn) {
return deprecated;
}

function makeLazyLoader(modulePath) {
let mod;
return function lazyLoad() {
if (!mod) {
mod = require(modulePath);
}
return mod;
};
}

module.exports = {
filterOptions,
mergeOptions,
Expand All @@ -729,6 +719,5 @@ module.exports = {
isPromiseLike,
decorateWithCollation,
decorateWithReadConcern,
deprecateOptions,
makeLazyLoader
deprecateOptions
};

0 comments on commit 050267d

Please sign in to comment.