-
Notifications
You must be signed in to change notification settings - Fork 0
/
changes.ts
31 lines (26 loc) · 964 Bytes
/
changes.ts
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
import xs, { Listener, Producer } from 'xstream'
class PouchChangeProducer implements Producer<PouchDB.Core.ChangesResponseChange<{}>> {
emitter: PouchDB.Core.Changes<{}>
constructor(private db: PouchDB.Database, private options?: PouchDB.Core.ChangesOptions) {
}
start(listener: Listener<PouchDB.Core.ChangesResponseChange<{}>>) {
this.emitter = this.db.changes(this.options)
.on('change', change => listener.next(change))
.on('complete', () => listener.complete())
.on('error', error => listener.error(error))
}
stop() {
if (this.emitter) {
this.emitter.cancel()
}
}
}
/**
* Factory for a stream of PouchDB changes.
*
* @param db Database to watch changes of
* @param options PouchDB Changes options
*/
export function changes(db: PouchDB.Database, options?: PouchDB.Core.ChangesOptions) {
return xs.create(new PouchChangeProducer(db, options))
}