This repository has been archived by the owner on Sep 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
results.go
65 lines (55 loc) · 1.45 KB
/
results.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
package pouchdb
import (
"github.com/gopherjs/gopherjs/js"
)
type resultWaiterTuple struct {
result *js.Object
err *js.Object
}
type resultWaiter struct {
resultChan chan *resultWaiterTuple
}
func NewResultWaiter() *resultWaiter {
return &resultWaiter{
make(chan *resultWaiterTuple),
}
}
// Read returns the raw results of a PouchDB callback
func (rw *resultWaiter) Read() (*js.Object, error) {
rawResult := <-rw.resultChan
if rawResult.err == nil {
return rawResult.result, nil
}
return rawResult.result, NewPouchError(&js.Error{Object: rawResult.err})
}
// Error returns just the error of a PouchDB callback, for methods
// which don't need the result value
func (rw *resultWaiter) Error() error {
_, err := rw.Read()
return err
}
func (rw *resultWaiter) ReadRev() (string, error) {
obj, err := rw.ReadResult()
if err != nil {
return "", err
}
return obj["rev"].(string), nil
}
func (rw *resultWaiter) ReadResult() (Result, error) {
result, err := rw.Read()
if err != nil {
return Result{}, err
}
return result.Interface().(map[string]interface{}), nil
}
func (rw *resultWaiter) ReadBulkResults() ([]Result, error) {
result, err := rw.Read()
results := make([]Result, result.Length())
for i := 0; i < result.Length(); i++ {
results[i] = result.Index(i).Interface().(map[string]interface{})
}
return results, err
}
func (rw *resultWaiter) Done(err *js.Object, result *js.Object) {
rw.resultChan <- &resultWaiterTuple{result, err}
}