-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathwatch.js
137 lines (107 loc) · 2.81 KB
/
watch.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const tape = require('tape')
const create = require('./helpers/create')
tape('basic watch', function (t) {
const db = create()
db.watch(function () {
t.pass('watch triggered')
t.end()
})
db.put('hello', 'world')
})
tape('watch prefix', function (t) {
const db = create()
var changed = false
db.watch('foo', function () {
t.ok(changed)
t.end()
})
db.put('hello', 'world', function (err) {
t.error(err)
setImmediate(function () {
changed = true
db.put('foo/bar', 'baz')
})
})
})
tape('recursive watch', function (t) {
t.plan(20)
const db = create()
var i = 0
db.watch('foo', function () {
if (i === 20) return
t.pass('watch triggered')
db.put('foo', 'bar-' + (++i))
})
db.put('foo', 'bar')
})
tape('watch and stop watching', function (t) {
const db = create()
var once = true
const w = db.watch('foo', function () {
t.ok(once)
once = false
w.destroy()
db.put('foo/bar/baz', 'qux', function () {
t.end()
})
})
db.put('foo/bar', 'baz')
})
tape('watch, watch and stop watch first one', function (t) {
t.plan(5 + 1)
const db = create()
var bs = 5
db.ready(function () {
const clone = create(db.key, { sparse: true })
const a = clone.watch('foo', function () {
t.pass('got a update')
a.destroy()
})
const b = clone.watch('foo', function () {
t.pass('got b update')
if (!--bs) {
b.destroy()
t.end()
return
}
db.put('foo/bar', 'baz')
})
db.put('foo/bar', 'baz')
const stream = db.replicate(true, { live: true })
stream.pipe(clone.replicate(false, { live: true })).pipe(stream)
})
})
tape('remote watch', function (t) {
const db = create()
db.ready(function () {
const clone = create(db.key)
for (var i = 0; i < 100; i++) db.put('hello-' + i, 'world-' + i)
db.put('flush', 'flush', function () {
clone.watch(function () {
t.pass('remote watch triggered')
t.end()
})
const stream = db.replicate(true)
stream.pipe(clone.replicate(false)).pipe(stream)
})
})
})
tape('remote watch with sparse mode and live replication', function (t) {
const db = create(null, { sparse: true })
db.ready(function () {
const clone = create(db.key, { alwaysUpdate: true, sparse: true })
// The ready triggers an update, which we do not want to trigger a watch event, so we must call this first.
clone.ready(function () {
const stream = db.replicate(true, { live: true })
stream.pipe(clone.replicate(false, { live: true })).pipe(stream)
var watcher = clone.watch(function () {
t.pass('remote watch triggered')
watcher.destroy()
t.end()
})
setTimeout(function () {
db.put('hello', 'world')
}, 50)
})
})
})