-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangeStream.js
More file actions
198 lines (169 loc) · 5.1 KB
/
Copy pathchangeStream.js
File metadata and controls
198 lines (169 loc) · 5.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
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
'use strict';
/*!
* Module dependencies.
*/
const EventEmitter = require('events').EventEmitter;
const MongooseError = require('../error/mongooseError');
/*!
* ignore
*/
const driverChangeStreamEvents = ['close', 'change', 'end', 'error', 'resumeTokenChanged'];
/*!
* ignore
*/
class ChangeStream extends EventEmitter {
constructor(changeStreamThunk, pipeline, options) {
super();
this.driverChangeStream = null;
this.closed = false;
this.bindedEvents = false;
this.pipeline = pipeline;
this.options = options;
this.errored = false;
if (options && options.hydrate && !options.model) {
throw new Error(
'Cannot create change stream with `hydrate: true` ' +
'unless calling `Model.watch()`'
);
}
let syncError = null;
this.$driverChangeStreamPromise = new Promise((resolve, reject) => {
// This wrapper is necessary because of buffering.
try {
changeStreamThunk((err, driverChangeStream) => {
if (err != null) {
this.errored = true;
this.emit('error', err);
return reject(err);
}
this.driverChangeStream = driverChangeStream;
this.emit('ready');
resolve();
});
} catch (err) {
syncError = err;
this.errored = true;
this.emit('error', err);
reject(err);
}
});
// Because a ChangeStream is an event emitter, there's no way to register an 'error' handler
// that catches errors which occur in the constructor, unless we force sync errors into async
// errors with setImmediate(). For cleaner stack trace, we just immediately throw any synchronous
// errors that occurred with changeStreamThunk().
if (syncError != null) {
throw syncError;
}
}
_bindEvents() {
if (this.bindedEvents) {
return;
}
this.bindedEvents = true;
if (this.driverChangeStream == null) {
this.$driverChangeStreamPromise.then(
() => {
this.driverChangeStream.on('close', () => {
this.closed = true;
});
driverChangeStreamEvents.forEach(ev => {
this.driverChangeStream.on(ev, data => {
if (data != null && data.fullDocument != null && this.options && this.options.hydrate) {
data.fullDocument = this.options.model.hydrate(data.fullDocument);
}
this.emit(ev, data);
});
});
},
() => {} // No need to register events if opening change stream failed
);
return;
}
this.driverChangeStream.on('close', () => {
this.closed = true;
});
driverChangeStreamEvents.forEach(ev => {
this.driverChangeStream.on(ev, data => {
if (data != null && data.fullDocument != null && this.options && this.options.hydrate) {
data.fullDocument = this.options.model.hydrate(data.fullDocument);
}
this.emit(ev, data);
});
});
}
hasNext(cb) {
if (this.errored) {
throw new MongooseError('Cannot call hasNext() on errored ChangeStream');
}
return this.driverChangeStream.hasNext(cb);
}
next(cb) {
if (this.errored) {
throw new MongooseError('Cannot call next() on errored ChangeStream');
}
if (this.options && this.options.hydrate) {
if (cb != null) {
const originalCb = cb;
cb = (err, data) => {
if (err != null) {
return originalCb(err);
}
if (data.fullDocument != null) {
data.fullDocument = this.options.model.hydrate(data.fullDocument);
}
return originalCb(null, data);
};
}
let maybePromise = this.driverChangeStream.next(cb);
if (maybePromise && typeof maybePromise.then === 'function') {
maybePromise = maybePromise.then(data => {
if (data.fullDocument != null) {
data.fullDocument = this.options.model.hydrate(data.fullDocument);
}
return data;
});
}
return maybePromise;
}
return this.driverChangeStream.next(cb);
}
addListener(event, handler) {
if (this.errored) {
throw new MongooseError('Cannot call addListener() on errored ChangeStream');
}
this._bindEvents();
return super.addListener(event, handler);
}
on(event, handler) {
if (this.errored) {
throw new MongooseError('Cannot call on() on errored ChangeStream');
}
this._bindEvents();
return super.on(event, handler);
}
once(event, handler) {
if (this.errored) {
throw new MongooseError('Cannot call once() on errored ChangeStream');
}
this._bindEvents();
return super.once(event, handler);
}
_queue(cb) {
this.once('ready', () => cb());
}
close() {
this.closed = true;
if (this.driverChangeStream) {
return this.driverChangeStream.close();
} else {
return this.$driverChangeStreamPromise.then(
() => this.driverChangeStream.close(),
() => {} // No need to close if opening the change stream failed
);
}
}
}
/*!
* ignore
*/
module.exports = ChangeStream;