forked from seigel/pouchdb-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchanges.js
113 lines (93 loc) · 3.32 KB
/
changes.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict'
import { uuid, filterChange } from 'pouchdb-utils'
import { forDocument, getSequenceKeys, toSequenceKeys } from './keys'
import inlineAttachments from './inline_attachments'
export default function (db, api, opts) {
const continuous = opts.continuous
if (continuous) {
const id = db.opts.name + ':' + uuid()
db.changes.addListener(db.opts.name, id, api, opts)
db.changes.notify(db.opts.name)
return {
cancel () {
db.changes.removeListener(db.opts.name, id)
}
}
}
// const descending = opts.descending
const lastSeq = opts.since || 0
const limit = ('limit' in opts && opts.limit >= 0)
? opts.limit
: -1
const filterDocIds = opts.doc_ids && new Set(opts.doc_ids)
const returnDocs = ('return_docs' in opts)
? opts.return_docs
: 'returnDocs' in opts
? opts.returnDocs
: true
const includeAttachments = 'attachments' in opts ? opts.attachments : false
const binaryAttachments = 'binary' in opts ? opts.binary : false
const filter = filterChange(opts)
const complete = opts.complete
const onChange = opts.onChange
const processChange = opts.processChange
db.storage.getKeys((error, keys) => {
if (error) return complete(error)
const filterSeqs = getSequenceKeys(keys).filter(seq => {
if (lastSeq) return seq > lastSeq
return true
})
if (filterSeqs.length === 0) return complete(null, {last_seq: lastSeq, results: []})
db.storage.multiGet(toSequenceKeys(filterSeqs), (error, dataDocs) => {
if (error) return complete(error)
const filterDocs = filterDocIds
? dataDocs.filter(doc => filterDocIds.has(doc._id))
: dataDocs.filter(doc => !doc._id.startsWith('_local'))
if (filterDocs.length === 0) return complete(null, {last_seq: lastSeq, results: []})
const changeDocIds = [...new Set(
filterDocs.map(data => forDocument(data._id)))]
db.storage.multiGet(changeDocIds, (error, docs) => {
const processChanges = () => {
const dataObj = filterDocs.reduce(
(res, data) => {
if (data) res[data._id] = data
return res
}, {})
const results = []
let lastChangeSeq
for (let index = 0; index < docs.length; index++) {
if (limit >= 0 && results.length > limit) break
const doc = docs[index]
const data = dataObj[doc.id]
const change = processChange(data, doc, opts)
change.seq = doc.seq
change.rev = doc.rev
const filtered = filter(change)
if (typeof filtered === 'object') {
return complete(filtered)
}
if (filtered) {
if (returnDocs) {
// correct Position???
change.changes[0].data = data
}
lastChangeSeq = change.seq
results.push(change)
onChange(change)
}
}
complete(null, {
results,
last_seq: lastChangeSeq
})
}
if (error) return complete(error)
if (!includeAttachments) return processChanges()
inlineAttachments(db, dataDocs, {binaryAttachments}, error => {
if (error) return complete(error)
processChanges()
})
})
})
})
}