UNPKG

node-eventstore-client

Version:

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

238 lines (197 loc) 18.8 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: eventStorePersistentSubscriptionBase.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: eventStorePersistentSubscriptionBase.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>var util = require('util'); var ensure = require('./common/utils/ensure'); var PersistentSubscriptionNakEventAction = require('./persistentSubscriptionNakEventAction'); var SubscriptionDropReason = require('./subscriptionDropReason'); function DropSubscriptionEvent() {} function EventStorePersistentSubscriptionBase( subscriptionId, streamId, eventAppeared, subscriptionDropped, userCredentials, log, verboseLogging, settings, bufferSize, autoAck ) { bufferSize = bufferSize === undefined ? 10 : bufferSize; autoAck = autoAck === undefined ? true : autoAck; this._subscriptionId = subscriptionId; this._streamId = streamId; this._eventAppeared = eventAppeared; this._subscriptionDropped = subscriptionDropped; this._userCredentials = userCredentials; this._log = log; this._verbose = verboseLogging; this._settings = settings; this._bufferSize = bufferSize; this._autoAck = autoAck; this._subscription = null; this._dropData = null; this._queue = []; this._isProcessing = false; this._isDropped = false; } EventStorePersistentSubscriptionBase.prototype.start = function() { this._stopped = false; var self = this; return this._startSubscription(this._subscriptionId, this._streamId, this._bufferSize, this._userCredentials, this._onEventAppeared.bind(this), this._onSubscriptionDropped.bind(this), this._settings) .then(function(subscription) { self._log.debug('Subscription started.'); self._subscription = subscription; return self; }); }; EventStorePersistentSubscriptionBase.prototype._startSubscription = function() { throw new Error("EventStorePersistentSubscriptionBase._startSubscription abstract method called." + this.constructor.name); }; /** * @param {ResolvedEvent[]|ResolvedEvent} events */ EventStorePersistentSubscriptionBase.prototype.acknowledge = function(events) { ensure.notNull(events, "events"); if (this._subscription === null) throw new Error("Invalid operation. Subscription is not active yet."); if (!Array.isArray(events)) events = [events]; var ids = events.map(function(x) { return x.originalEvent.eventId; }); this._subscription.notifyEventsProcessed(ids); }; /** * @param {ResolvedEvent[]|ResolvedEvent} events * @param {number} action One of PersistentSubscriptionNakEventAction * @param {string} reason */ EventStorePersistentSubscriptionBase.prototype.fail = function(events, action, reason) { ensure.notNull(events, "events"); PersistentSubscriptionNakEventAction.isValid(action); ensure.notNull(reason, "reason"); if (this._subscription === null) throw new Error("Invalid operation. Subscription is not active yet."); if (!Array.isArray(events)) events = [events]; var ids = events.map(function(x) { return x.originalEvent.eventId; }); this._subscription.notifyEventsFailed(ids, action, reason); }; //TODO: this should return a promise EventStorePersistentSubscriptionBase.prototype.stop = function() { if (this._verbose) this._log.debug("Persistent Subscription to %s: requesting stop...", this._streamId); this._enqueueSubscriptionDropNotification(SubscriptionDropReason.UserInitiated, null); //TODO figure out timeout with Promise still running //if (!_stopped.Wait(timeout)) //throw new TimeoutException(string.Format("Could not stop {0} in time.", GetType().Name)); }; EventStorePersistentSubscriptionBase.prototype._enqueueSubscriptionDropNotification = function(reason, error) { // if drop data was already set -- no need to enqueue drop again, somebody did that already if (!this._dropData) { this._dropData = {reason: reason, error: error}; this._enqueue(new DropSubscriptionEvent()); } }; EventStorePersistentSubscriptionBase.prototype._onSubscriptionDropped = function(subscription, reason, exception) { this._enqueueSubscriptionDropNotification(reason, exception); }; EventStorePersistentSubscriptionBase.prototype._onEventAppeared = function(subscription, resolvedEvent) { this._enqueue(resolvedEvent); }; EventStorePersistentSubscriptionBase.prototype._enqueue = function(resolvedEvent) { this._queue.push(resolvedEvent); if (!this._isProcessing) { this._isProcessing = true; setImmediate(this._processQueue.bind(this)); } }; function runAsync(fn) { try { return Promise.resolve(fn()); } catch(e) { return Promise.reject(e); } } EventStorePersistentSubscriptionBase.prototype._processQueue = function() { var ev = this._queue.shift(); if (!ev) { this._isProcessing = false; return; } if (ev instanceof DropSubscriptionEvent) // drop subscription artificial ResolvedEvent { if (this._dropData === null) throw new Error("Drop reason not specified."); this._dropSubscription(this._dropData.reason, this._dropData.error); this._isProcessing = false; return; } if (this._dropData !== null) { this._dropSubscription(this._dropData.reason, this._dropData.error); this._isProcessing = false; return; } var self = this; runAsync(function() { return self._eventAppeared(self, ev); }) .then(function() { if(self._autoAck) self._subscription.notifyEventsProcessed([ev.originalEvent.eventId]); if (self._verbose) { self._log.debug("Persistent Subscription to %s: processed event (%s, %d, %s @ %d).", self._streamId, ev.originalEvent.eventStreamId, ev.originalEvent.eventNumber, ev.originalEvent.eventType, ev.originalEventNumber); } return false; }, function(err) { //TODO GFY should we autonak here? self._dropSubscription(SubscriptionDropReason.EventHandlerException, err); self._isProcessing = false; return true; }) .then(function (faulted) { if (faulted) return; self._processQueue(); }); }; EventStorePersistentSubscriptionBase.prototype._dropSubscription = function(reason, error) { if (!this._isDropped) { this._isDropped = true; if (this._verbose) { this._log.debug("Persistent Subscription to %s: dropping subscription, reason: %s %s.", this._streamId, reason, error); } if (this._subscription !== null) this._subscription.unsubscribe(); if (this._subscriptionDropped !== null) { try { this._subscriptionDropped(this, reason, error); } catch (e) { this._log.error(e, "Persistent Subscription to %s: subscriptionDropped callback failed.", this._streamId); } } this._stopped = true; } }; module.exports = EventStorePersistentSubscriptionBase; </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>