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", "homepage": "https://github.com/nicdex/eventstore-node#readme",
"dependencies": { "dependencies": {
"protobufjs": "^5.0.1", "protobufjs": "^5.0.1",
"uuid": "^2.0.1", "uuid": "^2.0.1"
"when": "^3.7.7"
} }
} }

View File

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

View File

@ -1,5 +1,4 @@
var util = require('util'); var util = require('util');
var when = require('when');
var EventStoreCatchUpSubscription = require('./eventStoreCatchUpSubscription'); var EventStoreCatchUpSubscription = require('./eventStoreCatchUpSubscription');
var results = require('./results'); var results = require('./results');
@ -26,10 +25,10 @@ EventStoreAllCatchUpSubscription.prototype._readEventsTill = function(
function processEvents(events, index) { function processEvents(events, index) {
index = index || 0; 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."); 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]); self._tryProcess(events[index]);
resolve(); resolve();
}) })
@ -48,8 +47,8 @@ EventStoreAllCatchUpSubscription.prototype._readEventsTill = function(
? slice.isEndOfStream ? slice.isEndOfStream
: slice.nextPosition.compareTo(new results.Position(lastCommitPosition, lastCommitPosition)) >= 0; : slice.nextPosition.compareTo(new results.Position(lastCommitPosition, lastCommitPosition)) >= 0;
if (!done && slice.isEndOfStream) if (!done && slice.isEndOfStream)
return when(done).delay(10); return Promise.resolve(done).delay(10);
return when(done); return Promise.resolve(done);
}); });
}) })
.then(function(done) { .then(function(done) {

View File

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

View File

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

View File

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

View File

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