Skip to content

Commit

Permalink
Merge 487957a into ad4edc6
Browse files Browse the repository at this point in the history
  • Loading branch information
emonddr committed Aug 7, 2019
2 parents ad4edc6 + 487957a commit b887382
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/datasource.js
Expand Up @@ -194,6 +194,11 @@ util.inherits(DataSource, EventEmitter);
// allow child classes to supply a data access object
DataSource.DataAccessObject = DataAccessObject;

/**
* Global maximum number of event listeners
*/
DataSource.DEFAULT_MAX_OFFLINE_REQUESTS = 16;

/**
* Set up the connector instance for backward compatibility with JugglingDB schema/adapter
* @private
Expand All @@ -207,6 +212,10 @@ DataSource.prototype._setupConnector = function() {
this.connector.dataSource = this;
}
const dataSource = this;

// Set max listeners to a default/configured value
dataSource.setMaxListeners(dataSource.getMaxOfflineRequests());

this.connector.log = function(query, start) {
dataSource.log(query, start);
};
Expand Down Expand Up @@ -2699,6 +2708,27 @@ DataSource.prototype.beginTransaction = function(options) {
return Transaction.begin(this.connector, options);
};

/**
* Get the maximum number of event listeners
*/
DataSource.prototype.getMaxOfflineRequests = function() {
// Set max listeners to a default value
// Override this default value with a datasource setting
// 'maxOfflineRequests' from an application's datasources.json

let maxOfflineRequests = DataSource.DEFAULT_MAX_OFFLINE_REQUESTS;
if (
this.settings &&
this.settings.maxOfflineRequests
) {
if (!(typeof this.settings.maxOfflineRequests === 'number'))
throw new Error('maxOfflineRequests must be a number');

maxOfflineRequests = this.settings.maxOfflineRequests;
}
return maxOfflineRequests;
};

/*! The hidden property call is too expensive so it is not used that much
*/
/**
Expand Down
27 changes: 27 additions & 0 deletions test/datasource.test.js
Expand Up @@ -545,6 +545,33 @@ describe('DataSource', function() {
ds.connector.should.equal(connector);
});
});

describe('getMaxOfflineRequests', () => {
let ds;
beforeEach(() => ds = new DataSource('ds', {connector: 'memory'}));

it('sets the default maximum number of event listeners to 16', () => {
ds.getMaxOfflineRequests().should.be.eql(16);
});

it('uses the provided max number of listeners even if it is less than 16', () => {
ds.settings.maxOfflineRequests = 15;
ds.getMaxOfflineRequests().should.be.eql(15);
});

it('uses provided number of listeners if it is greater than 16', () => {
ds.settings.maxOfflineRequests = 17;
ds.getMaxOfflineRequests().should.be.eql(17);
});

it('throws an error if a non-number is provided for the max number of listeners', () => {
ds.settings.maxOfflineRequests = '17';

(function() {
return ds.getMaxOfflineRequests();
}).should.throw('maxOfflineRequests must be a number');
});
});
});

function givenMockConnector(props) {
Expand Down

0 comments on commit b887382

Please sign in to comment.