Replace when with native Promise

This commit is contained in:
Nicolas Dextraze 2016-03-09 16:09:18 -08:00
parent f948410140
commit 05e693d7b9
7 changed files with 26 additions and 34 deletions

View File

@ -23,7 +23,6 @@
"homepage": "https://github.com/nicdex/eventstore-node#readme",
"dependencies": {
"protobufjs": "^5.0.1",
"uuid": "^2.0.1",
"when": "^3.7.7"
"uuid": "^2.0.1"
}
}

View File

@ -1,5 +1,3 @@
var when = require('when');
function StaticEndpointDiscoverer(tcpEndPoint, useSsl) {
this._nodeEndpoints = {
tcpEndPoint: useSsl ? null : tcpEndPoint,
@ -8,7 +6,7 @@ function StaticEndpointDiscoverer(tcpEndPoint, useSsl) {
}
StaticEndpointDiscoverer.prototype.discover = function(failedTcpEndpoint) {
return when(this._nodeEndpoints);
return Promise.resolve(this._nodeEndpoints);
};
module.exports = StaticEndpointDiscoverer;

View File

@ -1,5 +1,4 @@
var util = require('util');
var when = require('when');
var EventStoreCatchUpSubscription = require('./eventStoreCatchUpSubscription');
var results = require('./results');
@ -26,10 +25,10 @@ EventStoreAllCatchUpSubscription.prototype._readEventsTill = function(
function processEvents(events, index) {
index = index || 0;
if (index >= events.length) return when();
if (index >= events.length) return Promise.resolve();
if (events[index].originalPosition === null) throw new Error("Subscription event came up with no OriginalPosition.");
return when.promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
self._tryProcess(events[index]);
resolve();
})
@ -48,8 +47,8 @@ EventStoreAllCatchUpSubscription.prototype._readEventsTill = function(
? slice.isEndOfStream
: slice.nextPosition.compareTo(new results.Position(lastCommitPosition, lastCommitPosition)) >= 0;
if (!done && slice.isEndOfStream)
return when(done).delay(10);
return when(done);
return Promise.resolve(done).delay(10);
return Promise.resolve(done);
});
})
.then(function(done) {

View File

@ -1,5 +1,4 @@
var util = require('util');
var when = require('when');
var SubscriptionDropReason = require('./subscriptionDropReason');
var results = require('./results');
@ -130,7 +129,7 @@ EventStoreCatchUpSubscription.prototype._runSubscription = function() {
var self = this;
this._stopped = false;
if (this._verbose) this._log.debug("Catch-up Subscription to %s: pulling events...", logStreamName);
when(this._readEventsTill(this._connection, this._resolveLinkTos, this._userCredentials, null, null))
this._readEventsTill(this._connection, this._resolveLinkTos, this._userCredentials, null, null)
.then(function() {
if (self._shouldStop) return;
if (self._verbose) self._log.debug("Catch-up Subscription to %s: subscribing...", logStreamName);

View File

@ -1,6 +1,5 @@
var util = require('util');
var uuid = require('uuid');
var when = require('when');
var EventEmitter = require('events').EventEmitter;
var ensure = require('./common/utils/ensure');
@ -70,7 +69,7 @@ Object.defineProperty(EventStoreNodeConnection.prototype, 'connectionName', {
*/
EventStoreNodeConnection.prototype.connect = function() {
var self = this;
return when.promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
function cb(err) {
if (err) return reject(err);
resolve();
@ -98,7 +97,7 @@ EventStoreNodeConnection.prototype.deleteStream = function(stream, expectedVersi
if (typeof expectedVersion !== 'number' || expectedVersion % 1 !== 0) throw new TypeError("expectedVersion must be an integer.");
var self = this;
return when.promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
function cb(err, result) {
if (err) return reject(err);
resolve(result);
@ -125,7 +124,7 @@ EventStoreNodeConnection.prototype.appendToStream = function(stream, expectedVer
if (!Array.isArray(events)) throw new TypeError("events must be an array.");
var self = this;
return when.promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
function cb(err, result) {
if (err) return reject(err);
resolve(result);
@ -146,7 +145,7 @@ EventStoreNodeConnection.prototype.appendToStream = function(stream, expectedVer
EventStoreNodeConnection.prototype.startTransaction = function(stream, expectedVersion, userCredentials) {
//TODO validations
var self = this;
return when.promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
function cb(err, result) {
if (err) return reject(err);
resolve(result);
@ -170,7 +169,7 @@ EventStoreNodeConnection.prototype.continueTransaction = function(transactionId,
EventStoreNodeConnection.prototype.transactionalWrite = function(transaction, events, userCredentials) {
var self = this;
return when.promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
function cb(err) {
if (err) return reject(err);
resolve();
@ -183,7 +182,7 @@ EventStoreNodeConnection.prototype.transactionalWrite = function(transaction, ev
EventStoreNodeConnection.prototype.commitTransaction = function(transaction, userCredentials) {
var self = this;
return when.promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
function cb(err, result) {
if (err) return reject(err);
resolve(result);
@ -210,7 +209,7 @@ EventStoreNodeConnection.prototype.readEvent = function(stream, eventNumber, res
if (resolveLinkTos && typeof resolveLinkTos !== 'boolean') throw new TypeError("resolveLinkTos must be a boolean.");
var self = this;
return when.promise(function(resolve, reject){
return new Promise(function(resolve, reject){
function cb(err, result) {
if (err) return reject(err);
resolve(result);
@ -239,7 +238,7 @@ EventStoreNodeConnection.prototype.readStreamEventsForward = function(
if (resolveLinkTos && typeof resolveLinkTos !== 'boolean') throw new TypeError("resolveLinkTos must be a boolean.");
var self = this;
return when.promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
function cb(err, result) {
if (err) return reject(err);
resolve(result);
@ -268,7 +267,7 @@ EventStoreNodeConnection.prototype.readStreamEventsBackward = function(
if (resolveLinkTos && typeof resolveLinkTos !== 'boolean') throw new TypeError("resolveLinkTos must be a boolean.");
var self = this;
return when.promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
function cb(err, result) {
if (err) return reject(err);
resolve(result);
@ -293,7 +292,7 @@ EventStoreNodeConnection.prototype.readAllEventsForward = function(
//TODO validations
var self = this;
return when.promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
function cb(err, result) {
if (err) return reject(err);
resolve(result);
@ -318,7 +317,7 @@ EventStoreNodeConnection.prototype.readAllEventsBackward = function(
//TODO validations
var self = this;
return when.promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
function cb(err, result) {
if (err) return reject(err);
resolve(result);
@ -346,7 +345,7 @@ EventStoreNodeConnection.prototype.subscribeToStream = function(
if (typeof eventAppeared !== 'function') throw new TypeError("eventAppeared must be a function.");
var self = this;
return when.promise(function(resolve,reject) {
return new Promise(function(resolve,reject) {
function cb(err, result) {
if (err) return reject(err);
resolve(result);
@ -399,7 +398,7 @@ EventStoreNodeConnection.prototype.subscribeToAll = function(
if (typeof eventAppeared !== 'function') throw new TypeError("eventAppeared must be a function.");
var self = this;
return when.promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
function cb(err, result) {
if (err) return reject(err);
resolve(result);
@ -476,7 +475,7 @@ EventStoreNodeConnection.prototype.setStreamMetadataRaw = function(
if (systemStreams.isMetastream(stream))
throw new Error(util.format("Setting metadata for metastream '%s' is not supported.", stream));
var self = this;
return when.promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
function cb(err, result) {
if (err) return reject(err);
resolve(result);

View File

@ -1,5 +1,4 @@
var util = require('util');
var when = require('when');
var EventStoreCatchUpSubscription = require('./eventStoreCatchUpSubscription');
var SliceReadStatus = require('./sliceReadStatus');
@ -27,9 +26,9 @@ EventStoreStreamCatchUpSubscription.prototype._readEventsTill = function(
function processEvents(events, index) {
index = index || 0;
if (index >= events.length) return when();
if (index >= events.length) return Promise.resolve();
return when.promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
self._tryProcess(events[index]);
resolve();
})
@ -46,7 +45,7 @@ EventStoreStreamCatchUpSubscription.prototype._readEventsTill = function(
return processEvents(slice.events)
.then(function() {
self._nextReadEventNumber = slice.nextEventNumber;
var done = when(lastEventNumber === null ? slice.isEndOfStream : slice.nextEventNumber > lastEventNumber);
var done = Promise.resolve(lastEventNumber === null ? slice.isEndOfStream : slice.nextEventNumber > lastEventNumber);
if (!done && slice.isEndOfStream)
return done.delay(10);
return done;

View File

@ -1,5 +1,4 @@
var util = require('util');
var when = require('when');
var uuid = require('uuid');
var client = require('../src/client');
var NoopLogger = require('../src/common/log/noopLogger');
@ -72,11 +71,11 @@ module.exports = {
this.conn.startTransaction(testStreamName, client.expectedVersion.any)
.then(function(trx) {
test.ok(trx, "No transaction.");
return when.join(trx, trx.write([createRandomEvent()]));
return Promise.all([trx, trx.write([createRandomEvent()])]);
})
.then(function(args) {
var trx = args[0];
return when.join(trx, trx.write([createRandomEvent()]));
return Promise.all([trx, trx.write([createRandomEvent()])]);
})
.then(function(args) {
var trx = args[0];