-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmethod-wrap.js
More file actions
31 lines (25 loc) · 770 Bytes
/
method-wrap.js
File metadata and controls
31 lines (25 loc) · 770 Bytes
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
'use strict';
var debug = require('debug')('pouch-stream-server:method-wrap');
var CHANGE_EVENTS = ['error', 'change', 'complete'];
module.exports = {
_changes: _changes,
};
function _changes(fn, stream) {
return function wrappedChanges(listener, options, cb) {
var db = this;
var changes = db.changes(options);
CHANGE_EVENTS.forEach(function eachChangeEvent(event) {
changes.on(event, onEvent);
stream.once('finish', function onceFinish() {
debug('stream finished');
changes.removeListener(event, onEvent);
changes.cancel();
});
function onEvent(payload) {
debug('event %s (%j)', event, payload);
stream.push(['_event', event, [listener, payload]]);
}
});
cb();
};
}