/node_modules/mongoose/lib/cursor/ChangeStream.js

https://bitbucket.org/coleman333/smartsite · JavaScript · 48 lines · 28 code · 10 blank · 10 comment · 2 complexity · 118a72586732ab43c54453dbd01acd75 MD5 · raw file

  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const EventEmitter = require('events').EventEmitter;
  6. /*!
  7. * ignore
  8. */
  9. class ChangeStream extends EventEmitter {
  10. constructor(model, pipeline, options) {
  11. super();
  12. this.driverChangeStream = null;
  13. // This wrapper is necessary because of buffering.
  14. if (model.collection.buffer) {
  15. model.collection.addQueue(() => {
  16. this.driverChangeStream = model.collection.watch(pipeline, options);
  17. this._bindEvents();
  18. this.emit('ready');
  19. });
  20. } else {
  21. this.driverChangeStream = model.collection.watch(pipeline, options);
  22. this._bindEvents();
  23. this.emit('ready');
  24. }
  25. }
  26. _bindEvents() {
  27. ['close', 'change', 'end', 'error'].forEach(ev => {
  28. this.driverChangeStream.on(ev, data => this.emit(ev, data));
  29. });
  30. }
  31. _queue(cb) {
  32. this.once('ready', () => cb());
  33. }
  34. }
  35. /*!
  36. * ignore
  37. */
  38. module.exports = ChangeStream;