-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplicationEvents.go
142 lines (128 loc) · 3.53 KB
/
replicationEvents.go
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
138
139
140
141
142
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package pouchdb
import (
"fmt"
"io"
"sync"
"time"
"github.com/gopherjs/gopherjs/js"
"github.com/gopherjs/jsbuiltin"
"github.com/go-kivik/pouchdb/v4/bindings"
)
type replicationState struct {
*js.Object
startTime time.Time `js:"start_time"`
endTime time.Time `js:"end_time"`
DocsRead int64 `js:"docs_read"`
DocsWritten int64 `js:"docs_written"`
DocWriteFailures int64 `js:"doc_write_failures"`
LastSeq string `js:"last_seq"`
}
func (rs *replicationState) StartTime() time.Time {
value := rs.Get("start_time")
if jsbuiltin.InstanceOf(value, js.Global.Get("Date")) {
return rs.startTime
}
t, err := convertTime(value)
if err != nil {
panic("start time: " + err.Error())
}
return t
}
func (rs *replicationState) EndTime() time.Time {
value := rs.Get("end_time")
if jsbuiltin.InstanceOf(value, js.Global.Get("Date")) {
return rs.endTime
}
t, err := convertTime(value)
if err != nil {
panic("end time: " + err.Error())
}
return t
}
func convertTime(value fmt.Stringer) (time.Time, error) {
if value == js.Undefined {
return time.Time{}, nil
}
switch jsbuiltin.TypeOf(value) {
case jsbuiltin.TypeString:
return time.Parse(time.RFC3339, value.String())
}
return time.Time{}, fmt.Errorf("unsupported type")
}
type replicationHandler struct {
event *string
state *replicationState
mu sync.Mutex
wg sync.WaitGroup
complete bool
obj *js.Object
}
func (r *replicationHandler) Cancel() {
r.obj.Call("cancel")
}
// Status returns the last-read status. If the last-read status was already read,
// this blocks until the next event. If the replication is complete, it will
// return io.EOF immediately.
func (r *replicationHandler) Status() (string, *replicationState, error) {
if r.complete && r.event == nil {
return "", nil, io.EOF
}
r.mu.Lock()
if r.event == nil {
r.mu.Unlock()
// Wait for an event to be ready to read
r.wg.Wait()
r.mu.Lock()
}
event, state := r.event, r.state
r.event = nil
r.mu.Unlock()
r.wg.Add(1)
return *event, state, nil
}
func (r *replicationHandler) handleEvent(event string, info *js.Object) {
if r.complete {
panic(fmt.Sprintf("Unexpected replication event after complete. %v %v", event, info))
}
r.mu.Lock()
defer r.mu.Unlock()
r.event = &event
switch event {
case bindings.ReplicationEventDenied, bindings.ReplicationEventError, bindings.ReplicationEventComplete:
r.complete = true
}
if info != nil && info != js.Undefined {
r.state = &replicationState{Object: info}
}
r.wg.Done()
}
func newReplicationHandler(rep *js.Object) *replicationHandler {
r := &replicationHandler{obj: rep}
for _, event := range []string{
bindings.ReplicationEventChange,
bindings.ReplicationEventComplete,
bindings.ReplicationEventPaused,
bindings.ReplicationEventActive,
bindings.ReplicationEventDenied,
bindings.ReplicationEventError,
} {
func(e string) {
rep.Call("on", e, func(info *js.Object) {
r.handleEvent(e, info)
})
}(event)
}
r.wg.Add(1)
return r
}