-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEventStream.js
275 lines (249 loc) · 7.8 KB
/
EventStream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const stream = require('stream');
const { assert } = require('./util');
/**
* Calculate the actual version number from a possibly relative (negative) version number.
*
* @param {number} version The version to normalize.
* @param {number} length The maximum version number
* @returns {number} The absolute version number.
*/
function normalizeVersion(version, length) {
return version < 0 ? version + length + 1 : version;
}
/**
* Return the lower absolute version given a version and a maxVersion constraint.
* @param {number} version
* @param {number} maxVersion
* @returns {number}
*/
function minVersion(version, maxVersion) {
return Math.min(version, maxVersion < 0 ? version + maxVersion + 1 : maxVersion);
}
/**
* An event stream is a simple wrapper around an iterator over storage documents.
* It implements a node readable stream interface.
*/
class EventStream extends stream.Readable {
/**
* @param {string} name The name of the stream.
* @param {EventStore} eventStore The event store to get the stream from.
* @param {number} [minRevision] The minimum revision to include in the events (inclusive).
* @param {number} [maxRevision] The maximum revision to include in the events (inclusive).
*/
constructor(name, eventStore, minRevision = 1, maxRevision = -1) {
super({ objectMode: true });
assert(typeof name === 'string' && name !== '', 'Need to specify a stream name.');
assert(typeof eventStore === 'object' && eventStore !== null, `Need to provide EventStore instance to create EventStream ${name}.`);
this.name = name;
if (eventStore.streams[name]) {
this.streamIndex = eventStore.streams[name].index;
this.minRevision = normalizeVersion(minRevision, this.streamIndex.length);
this.maxRevision = normalizeVersion(maxRevision, this.streamIndex.length);
this.version = minVersion(this.streamIndex.length, maxRevision);
this._iterator = null;
this.fetch = function() {
return eventStore.storage.readRange(this.minRevision, this.maxRevision, this.streamIndex);
}
} else {
this.streamIndex = { length: 0 };
this.version = -1;
this._iterator = { next() { return { done: true }; } };
}
}
/**
* @api
* @param {number} revision The event revision to start reading from (inclusive).
* @returns {EventStream}
*/
from(revision) {
this.minRevision = normalizeVersion(revision, this.streamIndex.length);
return this;
}
/**
* @api
* @param {number} revision The event revision to read until (inclusive).
* @returns {EventStream}
*/
until(revision) {
this.maxRevision = normalizeVersion(revision, this.streamIndex.length);
this.version = minVersion(this.streamIndex.length, this.maxRevision);
return this;
}
/**
* @api
* @param {number} amount The amount of events at the start of the stream to return in chronological order.
* @returns {EventStream}
*/
first(amount) {
return this.fromStart().following(amount);
}
/**
* @api
* @param {number} amount The amount of events at the end of the stream to return in chronological order.
* @returns {EventStream}
*/
last(amount) {
return this.fromEnd().previous(amount).forwards();
}
/**
* @api
* @returns {EventStream}
*/
fromStart() {
this.minRevision = 1;
return this;
}
/**
* @api
* @returns {EventStream}
*/
fromEnd() {
this.minRevision = this.streamIndex.length;
return this;
}
/**
* @param {number} amount The amount of events to return in reverse chronological order.
* @returns {EventStream}
*/
previous(amount) {
this.maxRevision = Math.max(1, this.minRevision - amount + 1);
return this;
}
/**
* @param {number} amount The amount of events to return in chronological order.
* @returns {EventStream}
*/
following(amount) {
this.maxRevision = Math.min(this.streamIndex.length, this.minRevision + amount - 1);
return this;
}
/**
* @api
* @returns {EventStream}
*/
toEnd() {
this.maxRevision = this.version = this.streamIndex.length;
return this;
}
/**
* @api
* @returns {EventStream}
*/
toStart() {
this.maxRevision = 1;
return this;
}
/**
* Reverse the current range of events, no matter which direction it currently has.
* @returns {EventStream}
*/
reverse() {
let tmp = this.maxRevision;
this.maxRevision = this.minRevision;
this.minRevision = tmp;
this.version = minVersion(this.streamIndex.length, this.maxRevision);
return this;
}
/**
* Make the current range of events read in forward chronological order.
* @api
* @param {number} [amount] Amount of events to read forward. If not specified, will read forward until the previously set limit.
* @returns {EventStream}
*/
forwards(amount = 0) {
if (amount > 0) {
this.following(amount);
}
if (this.maxRevision < this.minRevision) {
this.reverse();
}
return this;
}
/**
* Make the current range of events read in backward chronological order.
* @api
* @param {number} [amount] Amount of events to read backward. If not specified, will read backward until the previously set limit.
* @returns {EventStream}
*/
backwards(amount = 0) {
if (amount > 0) {
this.previous(amount);
}
if (this.maxRevision > this.minRevision) {
this.reverse();
}
return this;
}
/**
* Will iterate over all events in this stream and return an array of the events.
*
* @returns {Array<object>}
*/
get events() {
if (this._events instanceof Array) {
return this._events;
}
this._events = [];
let next;
while ((next = this.next()) !== false) {
this._events.push(next.payload);
}
return this._events;
}
/**
* Iterate over the events in this stream with a callback.
* This method is useful to gain access to the event metadata.
*
* @api
* @param {function(object, object, string)} callback A callback function that will receive the event, the storage metadata and the original stream name for every event in this stream.
*/
forEach(callback) {
let next;
while ((next = this.next()) !== false) {
callback(next.payload, next.metadata, next.stream);
}
}
/**
* Iterator implementation. Iterate over the stream in a `for ... of` loop.
*/
*[Symbol.iterator]() {
let next;
while ((next = this.next()) !== false) {
yield next.payload;
}
}
/**
* Reset this stream to the start so it can be iterated again.
* @returns {EventStream}
*/
reset() {
this._iterator = null;
this._events = null;
return this;
}
/**
* @returns {object|boolean} The next event or false if no more events in the stream.
*/
next() {
if (!this._iterator) {
this._iterator = this.fetch();
}
let next;
try {
next = this._iterator.next();
} catch(e) {
return false;
}
return next.done ? false : next.value;
}
// noinspection JSUnusedGlobalSymbols
/**
* Readable stream implementation.
* @private
*/
_read() {
const next = this.next();
this.push(next ? next.payload : null);
}
}
module.exports = EventStream;