PageRenderTime 16ms CodeModel.GetById 9ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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