ChangeStream.js
1.1 KB
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
'use strict';
/*!
* Module dependencies.
*/
const EventEmitter = require('events').EventEmitter;
/*!
* ignore
*/
class ChangeStream extends EventEmitter {
constructor(changeStreamThunk, pipeline, options) {
super();
this.driverChangeStream = null;
this.closed = false;
this.pipeline = pipeline;
this.options = options;
// This wrapper is necessary because of buffering.
changeStreamThunk((err, driverChangeStream) => {
if (err != null) {
this.emit('error', err);
return;
}
this.driverChangeStream = driverChangeStream;
this._bindEvents();
this.emit('ready');
});
}
_bindEvents() {
this.driverChangeStream.on('close', () => {
this.closed = true;
});
['close', 'change', 'end', 'error'].forEach(ev => {
this.driverChangeStream.on(ev, data => this.emit(ev, data));
});
}
_queue(cb) {
this.once('ready', () => cb());
}
close() {
this.closed = true;
if (this.driverChangeStream) {
this.driverChangeStream.close();
}
}
}
/*!
* ignore
*/
module.exports = ChangeStream;