UNPKG

node-eventstore-client

Version:

A port of the EventStore .Net ClientAPI to Node.js

316 lines (267 loc) 22.2 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: eventStoreCatchUpSubscription.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: eventStoreCatchUpSubscription.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>var util = require('util'); var SubscriptionDropReason = require('./subscriptionDropReason'); const DefaultReadBatchSize = 500; const DefaultMaxPushQueueSize = 10000; const MaxReadSize = 4096; function DropSubscriptionEvent() {} /** * @param connection * @param log * @param streamId * @param resolveLinkTos * @param userCredentials * @param eventAppeared * @param liveProcessingStarted * @param subscriptionDropped * @param verboseLogging * @param readBatchSize * @param maxPushQueueSize * @constructor * @property {boolean} isSubscribedToAll * @property {string} streamId * @property {number} readBatchSize * @property {number} maxPushQueueSize */ function EventStoreCatchUpSubscription( connection, log, streamId, resolveLinkTos, userCredentials, eventAppeared, liveProcessingStarted, subscriptionDropped, verboseLogging, readBatchSize, maxPushQueueSize ) { readBatchSize = readBatchSize || DefaultReadBatchSize; maxPushQueueSize = maxPushQueueSize || DefaultMaxPushQueueSize; //Ensure.NotNull(connection, "connection"); //Ensure.NotNull(log, "log"); //Ensure.NotNull(eventAppeared, "eventAppeared"); //Ensure.Positive(readBatchSize, "readBatchSize"); //Ensure.Positive(maxPushQueueSize, "maxPushQueueSize"); if (readBatchSize > MaxReadSize) throw new Error(util.format("Read batch size should be less than %d. For larger reads you should page.", MaxReadSize)); this._connection = connection; this._log = log; this._streamId = streamId || ''; this._resolveLinkTos = resolveLinkTos; this._userCredentials = userCredentials; this._shouldStop = false; this._stopped = false; this._isDropped = false; this._subscription = null; this._liveQueue = []; this._dropData = null; this._isProcessing = false; Object.defineProperties(this, { isSubscribedToAll: { value: this._streamId === '' }, streamId: { value: this._streamId }, readBatchSize: { value: readBatchSize }, maxPushQueueSize: { value: maxPushQueueSize } }); this._eventAppeared = eventAppeared; this._liveProcessingStarted = liveProcessingStarted; this._subscriptionDropped = subscriptionDropped; this._verbose = verboseLogging; var self = this; this._onReconnect = function() { if (self._verbose) self._log.debug("Catch-up Subscription to %s: unhooking from connection.Connected.", self._streamId || '&lt;all>'); self._connection.removeListener('connected', self._onReconnect); if (self._verbose) self._log.debug("Catch-up Subscription to %s: recovering after reconnection.", self._streamId || '&lt;all>'); self._runSubscription(); } } /** * @param {EventStoreNodeConnection} connection * @param {boolean} resolveLinkTos * @param {UserCredentials} userCredentials * @param {?number} lastCommitPosition * @param {?number} lastEventNumber * @private * @abstract */ EventStoreCatchUpSubscription.prototype._readEventsTill = function( connection, resolveLinkTos, userCredentials, lastCommitPosition, lastEventNumber ) { throw new Error("EventStoreCatchUpSubscription._readEventsTill abstract method called. " + this.constructor.name); }; /** * @param {ResolvedEvent} e * @private * @abstract */ EventStoreCatchUpSubscription.prototype._tryProcess = function(e) { throw new Error("EventStoreCatchUpSubscription._tryProcess abstract method called. " + this.constructor.name); }; EventStoreCatchUpSubscription.prototype.start = function() { if (this._verbose) this._log.debug("Catch-up Subscription to %s: starting...", this._streamId || '&lt;all>'); this._runSubscription(); }; EventStoreCatchUpSubscription.prototype.stop = function() { if (this._verbose) this._log.debug("Catch-up Subscription to %s: requesting stop...", this._streamId || '&lt;all>'); if (this._verbose) this._log.debug("Catch-up Subscription to %s: unhooking from connection.Connected.", this._streamId || '&lt;all>'); this._connection.removeListener('connected', this._onReconnect); this._shouldStop = true; this._enqueueSubscriptionDropNotification(SubscriptionDropReason.UserInitiated, null); /* if (timeout) { if (this._verbose) this._log.debug("Waiting on subscription to stop"); if (!this._stopped.Wait(timeout)) throw new TimeoutException(string.Format("Could not stop {0} in time.", GetType().Name)); } */ }; EventStoreCatchUpSubscription.prototype._runSubscription = function() { var logStreamName = this._streamId || '&lt;all>'; if (this._verbose) this._log.debug("Catch-up Subscription to %s: running...", logStreamName); var self = this; this._stopped = false; this._isDropped = false; this._dropData = null; if (this._verbose) this._log.debug("Catch-up Subscription to %s: pulling events...", logStreamName); 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); if (self._streamId === '') { return self._connection.subscribeToAll(self._resolveLinkTos, self._enqueuePushedEvent.bind(self), self._serverSubscriptionDropped.bind(self), self._userCredentials); } else { return self._connection.subscribeToStream(self._streamId, self._resolveLinkTos, self._enqueuePushedEvent.bind(self), self._serverSubscriptionDropped.bind(self), self._userCredentials); } }) .then(function(subscription) { if (subscription === undefined) return; if (self._verbose) self._log.debug("Catch-up Subscription to %s: pulling events (if left)...", logStreamName); self._subscription = subscription; return self._readEventsTill(self._connection, self._resolveLinkTos, self._userCredentials, subscription.lastCommitPosition, subscription.lastEventNumber) }) .catch(function(err) { self._dropSubscription(SubscriptionDropReason.CatchUpError, err); return true; }) .then(function(faulted) { if (faulted) return; if (self._shouldStop) { self._dropSubscription(SubscriptionDropReason.UserInitiated, null); return; } if (self._verbose) self._log.debug("Catch-up Subscription to %s: processing live events...", logStreamName); if (self._liveProcessingStarted) { try { self._liveProcessingStarted(self); } catch (e) { self._log.error(e, "Catch-up Subscription to %s: liveProcessingStarted callback failed.", logStreamName); } } if (self._verbose) self._log.debug("Catch-up Subscription to %s: hooking to connection.Connected", logStreamName); self._connection.on('connected', self._onReconnect); self._allowProcessing = true; self._ensureProcessingPushQueue(); }); }; EventStoreCatchUpSubscription.prototype._enqueuePushedEvent = function(subscription, e) { if (this._verbose) { this._log.debug("Catch-up Subscription to %s: event appeared (%s, %d, %s @ %s).", this._streamId || '&lt;all>', e.originalStreamId, e.originalEventNumber, e.originalEvent.eventType, e.originalPosition); } if (this._liveQueue.length >= this.maxPushQueueSize) { this._enqueueSubscriptionDropNotification(SubscriptionDropReason.ProcessingQueueOverflow, null); subscription.unsubscribe(); return; } this._liveQueue.push(e); if (this._allowProcessing) this._ensureProcessingPushQueue(); }; EventStoreCatchUpSubscription.prototype._serverSubscriptionDropped = function(subscription, reason, err) { this._enqueueSubscriptionDropNotification(reason, err); }; EventStoreCatchUpSubscription.prototype._enqueueSubscriptionDropNotification = function(reason, error) { // if drop data was already set -- no need to enqueue drop again, somebody did that already if (this._dropData) return; this._dropData = {reason: reason, error: error}; this._liveQueue.push(new DropSubscriptionEvent()); if (this._allowProcessing) this._ensureProcessingPushQueue(); }; EventStoreCatchUpSubscription.prototype._ensureProcessingPushQueue = function() { if (this._isProcessing) return; this._isProcessing = true; setImmediate(this._processLiveQueue.bind(this)); }; EventStoreCatchUpSubscription.prototype._processLiveQueue = function() { var ev = this._liveQueue.shift(); if (!ev) { this._isProcessing = false; return; } if (ev instanceof DropSubscriptionEvent) { if (!this._dropData) this._dropData = {reason: SubscriptionDropReason.Unknown, error: new Error("Drop reason not specified.")}; this._dropSubscription(this._dropData.reason, this._dropData.error); this._isProcessing = false; return; } var promise; try { promise = this._tryProcess(ev); } catch(err) { this._dropSubscription(SubscriptionDropReason.EventHandlerException, err); this._isProcessing = false; return; } if (promise &amp;&amp; promise.then) { var self = this; promise .then(this._processLiveQueue.bind(this), function(err) { self._dropSubscription(SubscriptionDropReason.EventHandlerException, err); self._isProcessing = false; }); } else { setImmediate(this._processLiveQueue.bind(this)); } }; EventStoreCatchUpSubscription.prototype._dropSubscription = function(reason, error) { if (this._isDropped) return; this._isDropped = true; if (this._verbose) { this._log.debug("Catch-up Subscription to %s: dropping subscription, reason: %s %s.", this._streamId || '&lt;all>', reason, error); } if (this._subscription) this._subscription.unsubscribe(); if (this._subscriptionDropped) { try { this._subscriptionDropped(this, reason, error); } catch (e) { this._log.error(e, "Catch-up Subscription to %s: subscriptionDropped callback failed.", this._streamId || '&lt;all>'); } } this._stopped = true; }; module.exports = EventStoreCatchUpSubscription;</code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Namespaces</h3><ul><li><a href="EventStore.html">EventStore</a></li><li><a href="EventStore.Client.html">Client</a></li><li><a href="EventStore.Client.Messages.html">Messages</a></li></ul><h3>Classes</h3><ul><li><a href="AllEventsSlice.html">AllEventsSlice</a></li><li><a href="ClusterDiscoverer.html">ClusterDiscoverer</a></li><li><a href="DeleteResult.html">DeleteResult</a></li><li><a href="EventReadResult.html">EventReadResult</a></li><li><a href="EventStore.Client.Messages.CheckpointReached.html">CheckpointReached</a></li><li><a href="EventStore.Client.Messages.ClientIdentified.html">ClientIdentified</a></li><li><a href="EventStore.Client.Messages.ConnectToPersistentSubscription.html">ConnectToPersistentSubscription</a></li><li><a href="EventStore.Client.Messages.CreatePersistentSubscription.html">CreatePersistentSubscription</a></li><li><a href="EventStore.Client.Messages.CreatePersistentSubscriptionCompleted.html">CreatePersistentSubscriptionCompleted</a></li><li><a href="EventStore.Client.Messages.DeletePersistentSubscription.html">DeletePersistentSubscription</a></li><li><a href="EventStore.Client.Messages.DeletePersistentSubscriptionCompleted.html">DeletePersistentSubscriptionCompleted</a></li><li><a href="EventStore.Client.Messages.DeleteStream.html">DeleteStream</a></li><li><a href="EventStore.Client.Messages.DeleteStreamCompleted.html">DeleteStreamCompleted</a></li><li><a href="EventStore.Client.Messages.EventRecord.html">EventRecord</a></li><li><a href="EventStore.Client.Messages.Filter.html">Filter</a></li><li><a href="EventStore.Client.Messages.FilteredReadAllEvents.html">FilteredReadAllEvents</a></li><li><a href="EventStore.Client.Messages.FilteredReadAllEventsCompleted.html">FilteredReadAllEventsCompleted</a></li><li><a href="EventStore.Client.Messages.FilteredSubscribeToStream.html">FilteredSubscribeToStream</a></li><li><a href="EventStore.Client.Messages.IdentifyClient.html">IdentifyClient</a></li><li><a href="EventStore.Client.Messages.NewEvent.html">NewEvent</a></li><li><a href="EventStore.Client.Messages.NotHandled.html">NotHandled</a></li><li><a href="EventStore.Client.Messages.NotHandled.LeaderInfo.html">LeaderInfo</a></li><li><a href="EventStore.Client.Messages.PersistentSubscriptionAckEvents.html">PersistentSubscriptionAckEvents</a></li><li><a href="EventStore.Client.Messages.PersistentSubscriptionConfirmation.html">PersistentSubscriptionConfirmation</a></li><li><a href="EventStore.Client.Messages.PersistentSubscriptionNakEvents.html">PersistentSubscriptionNakEvents</a></li><li><a href="EventStore.Client.Messages.PersistentSubscriptionStreamEventAppeared.html">PersistentSubscriptionStreamEventAppeared</a></li><li><a href="EventStore.Client.Messages.ReadAllEvents.html">ReadAllEvents</a></li><li><a href="EventStore.Client.Messages.ReadAllEventsCompleted.html">ReadAllEventsCompleted</a></li><li><a href="EventStore.Client.Messages.ReadEvent.html">ReadEvent</a></li><li><a href="EventStore.Client.Messages.ReadEventCompleted.html">ReadEventCompleted</a></li><li><a href="EventStore.Client.Messages.ReadStreamEvents.html">ReadStreamEvents</a></li><li><a href="EventStore.Client.Messages.ReadStreamEventsCompleted.html">ReadStreamEventsCompleted</a></li><li><a href="EventStore.Client.Messages.ResolvedEvent.html">ResolvedEvent</a></li><li><a href="EventStore.Client.Messages.ResolvedIndexedEvent.html">ResolvedIndexedEvent</a></li><li><a href="EventStore.Client.Messages.ScavengeDatabase.html">ScavengeDatabase</a></li><li><a href="EventStore.Client.Messages.ScavengeDatabaseResponse.html">ScavengeDatabaseResponse</a></li><li><a href="EventStore.Client.Messages.StreamEventAppeared.html">StreamEventAppeared</a></li><li><a href="EventStore.Client.Messages.SubscribeToStream.html">SubscribeToStream</a></li><li><a href="EventStore.Client.Messages.SubscriptionConfirmation.html">SubscriptionConfirmation</a></li><li><a href="EventStore.Client.Messages.SubscriptionDropped.html">SubscriptionDropped</a></li><li><a href="EventStore.Client.Messages.TransactionCommit.html">TransactionCommit</a></li><li><a href="EventStore.Client.Messages.TransactionCommitCompleted.html">TransactionCommitCompleted</a></li><li><a href="EventStore.Client.Messages.TransactionStart.html">TransactionStart</a></li><li><a href="EventStore.Client.Messages.TransactionStartCompleted.html">TransactionStartCompleted</a></li><li><a href="EventStore.Client.Messages.TransactionWrite.html">TransactionWrite</a></li><li><a href="EventStore.Client.Messages.TransactionWriteCompleted.html">TransactionWriteCompleted</a></li><li><a href="EventStore.Client.Messages.UnsubscribeFromStream.html">UnsubscribeFromStream</a></li><li><a href="EventStore.Client.Messages.UpdatePersistentSubscription.html">UpdatePersistentSubscription</a></li><li><a href="EventStore.Client.Messages.UpdatePersistentSubscriptionCompleted.html">UpdatePersistentSubscriptionCompleted</a></li><li><a href="EventStore.Client.Messages.WriteEvents.html">WriteEvents</a></li><li><a href="EventStore.Client.Messages.WriteEventsCompleted.html">WriteEventsCompleted</a></li><li><a href="EventStoreCatchUpSubscription.html">EventStoreCatchUpSubscription</a></li><li><a href="EventStoreNodeConnection.html">EventStoreNodeConnection</a></li><li><a href="EventStoreTransaction.html">EventStoreTransaction</a></li><li><a href="PersistentSubscriptionCreateResult.html">PersistentSubscriptionCreateResult</a></li><li><a href="PersistentSubscriptionDeleteResult.html">PersistentSubscriptionDeleteResult</a></li><li><a href="PersistentSubscriptionUpdateResult.html">PersistentSubscriptionUpdateResult</a></li><li><a href="Position.html">Position</a></li><li><a href="ProjectionsManager.html">ProjectionsManager</a></li><li><a href="RawStreamMetadataResult.html">RawStreamMetadataResult</a></li><li><a href="RecordedEvent.html">RecordedEvent</a></li><li><a href="ResolvedEvent.html">ResolvedEvent</a></li><li><a href="StreamEventsSlice.html">StreamEventsSlice</a></li><li><a href="UserCredentials.html">UserCredentials</a></li><li><a href="WriteResult.html">WriteResult</a></li></ul><h3>Interfaces</h3><ul><li><a href="EventStore.Client.Messages.ICheckpointReached.html">ICheckpointReached</a></li><li><a href="EventStore.Client.Messages.IClientIdentified.html">IClientIdentified</a></li><li><a href="EventStore.Client.Messages.IConnectToPersistentSubscription.html">IConnectToPersistentSubscription</a></li><li><a href="EventStore.Client.Messages.ICreatePersistentSubscription.html">ICreatePersistentSubscription</a></li><li><a href="EventStore.Client.Messages.ICreatePersistentSubscriptionCompleted.html">ICreatePersistentSubscriptionCompleted</a></li><li><a href="EventStore.Client.Messages.IDeletePersistentSubscription.html">IDeletePersistentSubscription</a></li><li><a href="EventStore.Client.Messages.IDeletePersistentSubscriptionCompleted.html">IDeletePersistentSubscriptionCompleted</a></li><li><a href="EventStore.Client.Messages.IDeleteStream.html">IDeleteStream</a></li><li><a href="EventStore.Client.Messages.IDeleteStreamCompleted.html">IDeleteStreamCompleted</a></li><li><a href="EventStore.Client.Messages.IEventRecord.html">IEventRecord</a></li><li><a href="EventStore.Client.Messages.IFilter.html">IFilter</a></li><li><a href="EventStore.Client.Messages.IFilteredReadAllEvents.html">IFilteredReadAllEvents</a></li><li><a href="EventStore.Client.Messages.IFilteredReadAllEventsCompleted.html">IFilteredReadAllEventsCompleted</a></li><li><a href="EventStore.Client.Messages.IFilteredSubscribeToStream.html">IFilteredSubscribeToStream</a></li><li><a href="EventStore.Client.Messages.IIdentifyClient.html">IIdentifyClient</a></li><li><a href="EventStore.Client.Messages.INewEvent.html">INewEvent</a></li><li><a href="EventStore.Client.Messages.INotHandled.html">INotHandled</a></li><li><a href="EventStore.Client.Messages.IPersistentSubscriptionAckEvents.html">IPersistentSubscriptionAckEvents</a></li><li><a href="EventStore.Client.Messages.IPersistentSubscriptionConfirmation.html">IPersistentSubscriptionConfirmation</a></li><li><a href="EventStore.Client.Messages.IPersistentSubscriptionNakEvents.html">IPersistentSubscriptionNakEvents</a></li><li><a href="EventStore.Client.Messages.IPersistentSubscriptionStreamEventAppeared.html">IPersistentSubscriptionStreamEventAppeared</a></li><li><a href="EventStore.Client.Messages.IReadAllEvents.html">IReadAllEvents</a></li><li><a href="EventStore.Client.Messages.IReadAllEventsCompleted.html">IReadAllEventsCompleted</a></li><li><a href="EventStore.Client.Messages.IReadEvent.html">IReadEvent</a></li><li><a href="EventStore.Client.Messages.IReadEventCompleted.html">IReadEventCompleted</a></li><li><a href="EventStore.Client.Messages.IReadStreamEvents.html">IReadStreamEvents</a></li><li><a href="EventStore.Client.Messages.IReadStreamEventsCompleted.html">IReadStreamEventsCompleted</a></li><li><a href="EventStore.Client.Messages.IResolvedEvent.html">IResolvedEvent</a></li><li><a href="EventStore.Client.Messages.IResolvedIndexedEvent.html">IResolvedIndexedEvent</a></li><li><a href="EventStore.Client.Messages.IScavengeDatabase.html">IScavengeDatabase</a></li><li><a href="EventStore.Client.Messages.IScavengeDatabaseResponse.html">IScavengeDatabaseResponse</a></li><li><a href="EventStore.Client.Messages.IStreamEventAppeared.html">IStreamEventAppeared</a></li><li><a href="EventStore.Client.Messages.ISubscribeToStream.html">ISubscribeToStream</a></li><li><a href="EventStore.Client.Messages.ISubscriptionConfirmation.html">ISubscriptionConfirmation</a></li><li><a href="EventStore.Client.Messages.ISubscriptionDropped.html">ISubscriptionDropped</a></li><li><a href="EventStore.Client.Messages.ITransactionCommit.html">ITransactionCommit</a></li><li><a href="EventStore.Client.Messages.ITransactionCommitCompleted.html">ITransactionCommitCompleted</a></li><li><a href="EventStore.Client.Messages.ITransactionStart.html">ITransactionStart</a></li><li><a href="EventStore.Client.Messages.ITransactionStartCompleted.html">ITransactionStartCompleted</a></li><li><a href="EventStore.Client.Messages.ITransactionWrite.html">ITransactionWrite</a></li><li><a href="EventStore.Client.Messages.ITransactionWriteCompleted.html">ITransactionWriteCompleted</a></li><li><a href="EventStore.Client.Messages.IUnsubscribeFromStream.html">IUnsubscribeFromStream</a></li><li><a href="EventStore.Client.Messages.IUpdatePersistentSubscription.html">IUpdatePersistentSubscription</a></li><li><a href="EventStore.Client.Messages.IUpdatePersistentSubscriptionCompleted.html">IUpdatePersistentSubscriptionCompleted</a></li><li><a href="EventStore.Client.Messages.IWriteEvents.html">IWriteEvents</a></li><li><a href="EventStore.Client.Messages.IWriteEventsCompleted.html">IWriteEventsCompleted</a></li><li><a href="EventStore.Client.Messages.NotHandled.ILeaderInfo.html">ILeaderInfo</a></li></ul><h3>Global</h3><ul><li><a href="global.html#createConnection">createConnection</a></li><li><a href="global.html#createEventData">createEventData</a></li><li><a href="global.html#createJsonEventData">createJsonEventData</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.0</a> on Mon Jan 30 2023 16:18:36 GMT-0500 (Eastern Standard Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>