| 367 | var inprogress = false; |
| 368 | var self = this; |
| 369 | function eventFunction() { |
| 370 | if (!self._listeners[id]) { |
| 371 | return; |
| 372 | } |
| 373 | if (inprogress) { |
| 374 | inprogress = 'waiting'; |
| 375 | return; |
| 376 | } |
| 377 | inprogress = true; |
| 378 | var changesOpts = pick(opts, [ |
| 379 | 'style', 'include_docs', 'attachments', 'conflicts', 'filter', |
| 380 | 'doc_ids', 'view', 'since', 'query_params', 'binary', 'return_docs' |
| 381 | ]); |
| 382 | |
| 383 | function onError() { |
| 384 | inprogress = false; |
| 385 | } |
| 386 | |
| 387 | db.changes(changesOpts).on('change', function (c) { |
| 388 | if (c.seq > opts.since && !opts.cancelled) { |
| 389 | opts.since = c.seq; |
| 390 | opts.onChange(c); |
| 391 | } |
| 392 | }).on('complete', function () { |
| 393 | if (inprogress === 'waiting') { |
| 394 | nextTick(eventFunction); |
| 395 | } |
| 396 | inprogress = false; |
| 397 | }).on('error', onError); |
| 398 | } |
| 399 | this._listeners[id] = eventFunction; |
| 400 | this.on(dbName, eventFunction); |
| 401 | } |