mirror of
https://github.com/JonasunderscoreJones/jonas_jones-api.git
synced 2025-10-24 01:29:19 +02:00
some progress
This commit is contained in:
parent
aea93a5527
commit
e3c15bd288
1388 changed files with 306946 additions and 68323 deletions
151
node_modules/mongoose/lib/cursor/ChangeStream.js
generated
vendored
Normal file
151
node_modules/mongoose/lib/cursor/ChangeStream.js
generated
vendored
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
'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.bindedEvents = false;
|
||||
this.pipeline = pipeline;
|
||||
this.options = options;
|
||||
|
||||
if (options && options.hydrate && !options.model) {
|
||||
throw new Error(
|
||||
'Cannot create change stream with `hydrate: true` ' +
|
||||
'unless calling `Model.watch()`'
|
||||
);
|
||||
}
|
||||
|
||||
// This wrapper is necessary because of buffering.
|
||||
changeStreamThunk((err, driverChangeStream) => {
|
||||
if (err != null) {
|
||||
this.emit('error', err);
|
||||
return;
|
||||
}
|
||||
|
||||
this.driverChangeStream = driverChangeStream;
|
||||
this.emit('ready');
|
||||
});
|
||||
}
|
||||
|
||||
_bindEvents() {
|
||||
if (this.bindedEvents) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.bindedEvents = true;
|
||||
|
||||
if (this.driverChangeStream == null) {
|
||||
this.once('ready', () => {
|
||||
this.driverChangeStream.on('close', () => {
|
||||
this.closed = true;
|
||||
});
|
||||
|
||||
['close', 'change', 'end', 'error'].forEach(ev => {
|
||||
this.driverChangeStream.on(ev, data => {
|
||||
// Sometimes Node driver still polls after close, so
|
||||
// avoid any uncaught exceptions due to closed change streams
|
||||
// See tests for gh-7022
|
||||
if (ev === 'error' && this.closed) {
|
||||
return;
|
||||
}
|
||||
if (data != null && data.fullDocument != null && this.options && this.options.hydrate) {
|
||||
data.fullDocument = this.options.model.hydrate(data.fullDocument);
|
||||
}
|
||||
this.emit(ev, data);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.driverChangeStream.on('close', () => {
|
||||
this.closed = true;
|
||||
});
|
||||
|
||||
['close', 'change', 'end', 'error'].forEach(ev => {
|
||||
this.driverChangeStream.on(ev, data => {
|
||||
// Sometimes Node driver still polls after close, so
|
||||
// avoid any uncaught exceptions due to closed change streams
|
||||
// See tests for gh-7022
|
||||
if (ev === 'error' && this.closed) {
|
||||
return;
|
||||
}
|
||||
this.emit(ev, data);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
hasNext(cb) {
|
||||
return this.driverChangeStream.hasNext(cb);
|
||||
}
|
||||
|
||||
next(cb) {
|
||||
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);
|
||||
}
|
||||
|
||||
on(event, handler) {
|
||||
this._bindEvents();
|
||||
return super.on(event, handler);
|
||||
}
|
||||
|
||||
once(event, handler) {
|
||||
this._bindEvents();
|
||||
return super.once(event, handler);
|
||||
}
|
||||
|
||||
_queue(cb) {
|
||||
this.once('ready', () => cb());
|
||||
}
|
||||
|
||||
close() {
|
||||
this.closed = true;
|
||||
if (this.driverChangeStream) {
|
||||
this.driverChangeStream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = ChangeStream;
|
||||
Loading…
Add table
Add a link
Reference in a new issue